How to load the PDF document from the URL in server side
Essential JS 2 PDF Viewer
The Syncfusion PDF Viewer in ASP.NET Core (Essential JS 2) is a modern enterprise UI toolkit that has been built from the ground up to be lightweight, responsive, modular, and touch-friendly. It is also available in other frameworks such as JavaScript, Angular, ASP.NET MVC, Blazor, and React.
Refer to the following UG link for getting started with the PDF Viewer Control.
https://ej2.syncfusion.com/aspnetcore/documentation/pdfviewer/getting-started/
Load the PDF document from the database into PDF Viewer
PDF Viewer support loading the PDF document from URL using the documentPath API. Refer to the following steps to view the PDF document from the URL.
Step 1:
Initialize the PDF Viewer control and provide the document name in the documentPath API.
Index.cshtml
<ejs-pdfviewer id="container" style="height:600px" serviceUrl="/api/PdfViewer" DocumentPath="<PDF Document URL>"></ejs-pdfviewer> |
Step 2:
On initializing the PDF Viewer, the Load() web action method is called and in this method using the WebClient PDF document will be downloaded the document and return a byte array to load in our PDF Viewer.
PdfViewerController.cs
[AcceptVerbs("Post")] [HttpPost] [Route("api/[controller]/Load")] public IActionResult Load([FromBody] Dictionary<string, string> jsonData) { PdfRenderer pdfviewer = new PdfRenderer(_cache); MemoryStream stream = new MemoryStream();
object jsonResult = new object(); if (jsonData != null && jsonData.ContainsKey("document")) { if (bool.Parse(jsonData["isFileName"])) { string documentPath = GetDocumentPath(jsonData["document"]);
if (!string.IsNullOrEmpty(documentPath)) { byte[] bytes = System.IO.File.ReadAllBytes(documentPath); stream = new MemoryStream(bytes); } else { string fileName = jsonData["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0]; if (fileName == "http" || fileName == "https") { WebClient WebClient = new WebClient(); byte[] pdfDoc = WebClient.DownloadData(jsonData["document"]); stream = new MemoryStream(pdfDoc); } else { return this.Content(jsonData["document"] + " is not found"); }
} } else { byte[] bytes = Convert.FromBase64String(jsonData["document"]); stream = new MemoryStream(bytes); } } jsonResult = pdfviewer.Load(stream, jsonData); return Content(JsonConvert.SerializeObject(jsonResult)); } |
Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/EJ2PDFViewer-1539197914.zip
Modify the documentPath with the correct PDF Document URL.