Articles in this section
Category / Section

How to Retrieve a Document and Display It in Angular PDF Viewer?

3 mins read

This article outlines the steps required to fetch a document encoded as a Base64 string using the Fetch API and display it in an Angular PDF viewer.

Solution:

To retrieve a document encoded as a Base64 string using the Fetch API from the client, send a request to the server to get the document as Base64, and then load and display the document using that Base64 string in an Angular PDF viewer.

Code Snippets:

Client-Side Code Block:

app.component.ts:

ngOnInit(): void {
    this.fetchData();
  }

  async fetchData(): Promise<void> {
    try {
      const fileName = 'annotations.pdf';
      const response = await fetch(
        `https://localhost:7255/pdfviewer/GetDocument?fileName=${fileName}`
      );
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      const pdfData = await response.text();
      setTimeout(() => {
        this.pdfviewerControl.load(pdfData, null);
      }, 1000);
    } catch (error: any) {
      this.error = error.message;
    } finally {
      this.loading = false;
    }
  }
Server-Side Code Block:

PdfViewerController.cs:

    public IActionResult GetDocument(string fileName)
    {
        WebClient WebClient = new WebClient();
        string documentPath = GetDocumentPath(fileName);
        byte[] pdfDoc = System.IO.File.ReadAllBytes(documentPath);
        var stream = new MemoryStream(pdfDoc);
        var base64 = Convert.ToBase64String(stream.ToArray());
        return Content("data:application/pdf;base64," + base64);
    }
    
    //Gets the path of the PDF document
    private string GetDocumentPath(string document)
    {
        string documentPath = string.Empty;
        if (!System.IO.File.Exists(document))
        {
            var path = _hostingEnvironment.ContentRootPath;
            if (System.IO.File.Exists(path + "/Data/" + document))
                documentPath = path + "/Data/" + document;
        }
        else
        {
            documentPath = document;
        }
        return documentPath;
    }
Sample Link:

You can find the full sample in our GitHub repository.

Implementation Steps:

  1. Run the server sample to start serving the document as a Base64 string.
  2. Run the client sample to fetch the Base64 string from the server.
  3. The document will be loaded into the viewer on the client side.

Conclusion:

I hope you enjoyed learning how to retrieve a document and display it in Angular PDF Viewer.

You can refer to Angular PDF Viewer feature tour page to know about its other groundbreaking feature representations and documentation , and how to quickly get started for configuration specifications.

You can also explore our Angular PDF Viewer Example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied