We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Use pdf viewer with a document stored in a database

I would like to use the pdf viewer 

 @Html.EJS().PdfViewer("pdfviewer").ServiceUrl(VirtualPathUtility.ToAbsolute("~/PdfViewer/")).Render()

But the document is not stored in a folder in the webserver, it is stored in a database, so I need to retrieve the document from the database (not a problem) and load the document in the pdfviewer component.

Could you provide an example? 

Thank you.

I'm using the controller and razor view from your website: 

https://ej2.syncfusion.com/aspnetmvc/documentation/pdfviewer/getting-started/



5 Replies

RT Ramya Thirugnanam Syncfusion Team February 4, 2019 12:22 PM UTC

Hi Guillermo, 
 
Thanks for contacting Syncfusion support. 
  
PDF viewer supports to load the PDF document from database as base64 string using the documentPath API.  
  
The below code example will get the PDF document as byte array using its name from the Database and converts the same into base64 string. 
 
public object GetDocument() 
        { 
            string documentID = "Xyz.pdf"; 
            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["PdfDocument"].ConnectionString; 
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr); 
            //Searches for the PDF document from the Database 
            var query = "select Data from PdfDocuments where DocumentName = '" + documentID + "'"; 
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query); 
            cmd.Connection = con; 
            con.Open(); 
            System.Data.SqlClient.SqlDataReader read = cmd.ExecuteReader(); 
            read.Read(); 
            // Reads the PDF document data as byte array from the database 
            byte[] byteArray = (byte[])read["Data"]; 
            //converts byte array into base64 string 
            return "data:application/pdf;base64," + Convert.ToBase64String(byteArray); 
  
        }  
  
Note: Kindly modify the Database name and their entries as per your database entries. 
  
We have created the sample to load the base64 string in PDF viewer and shared the same in the following link,  
  
  
In the preceding sample, clicking on the ‘Load PDF document as Base64String’ will get PDF document from the local disk as base64 string in GetDocument() and load it in PDF viewer using documentPath. Similarly, you can get the PDF document from database and load it into the PDF Viewer. 
  
Regards, 
Ramya T 



GG Guillermo Goberna February 6, 2019 06:25 AM UTC

Thank you very much,

I left here also the code in case is useful for anyone else: 

View: 
<div style="height:800px;width:100%;">
       @Html.EJS().PdfViewer("pdfviewer").ServiceUrl(VirtualPathUtility.ToAbsolute("~/PDF/")).DocumentPath(ViewBag.Documento).Render()
</div>

Controller:
        public ActionResult Visor(int id)
        {
            // Reads the PDF document data as byte array from the database (DBtm is the dbml class that connects to the Documents database)
            byte[] byteArray = DBtm.Documents.First(x => x.IdDocument == id).Document.ToArray();

            //converts byte array into base64 string
            ViewBag.Documento = "data:application/pdf;base64," + Convert.ToBase64String(byteArray);
            return View();
        }


RT Ramya Thirugnanam Syncfusion Team February 6, 2019 06:43 AM UTC

Hi Guillermo,  
 
Thanks for your update. 
 
Regards,   
Ramya T   



MI MIMS IT November 26, 2019 11:16 AM UTC

Hello, 

Can you help with loading the pdf from azure blob storage url.

MIMS IT


AA Akshaya Arivoli Syncfusion Team November 27, 2019 07:07 AM UTC

Hi ,
Thank you for contacting Syncfusion support. 

We can load the PDF document’s blob URL in PDF Viewer, by converting the URL to base64 string using the client-side load() API in the sample level. Please find the below link for reference, 


Please try this and revert us if you have any concerns on this. 

Regards, 
Akshaya 


Loader.
Live Chat Icon For mobile
Up arrow icon