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
close icon

PDF Viewer and source for PDF

The PDF Viewer seems to require a web service to supply PDF files from the server. 

first question

We have an ASPX page in place already that returns a PDF in binary format. Is there any way we can use this with the PDFViewer? Same for simple PDF pages that we already have a direct URL for

second question

Do you have any more information on how to handle things such as filenames, etc in the web service?


13 Replies

MS Mohan Selvaraj Syncfusion Team October 6, 2017 09:44 AM UTC

Hi Mike, 
 
Thank you for contacting Syncfusion support. 
 
Kindly refer the details below. 
 
Query 
Details 
We have an ASPX page in place already that returns a PDF in binary format. Is there any way we can use this with the PDFViewer? Same for simple PDF pages that we already have a direct URL for 
 
we can load the PDF document as the base64 string in the client side of the PDF viewer control. We have created a sample for loading the PDF document by base64 string in the client side (ASPX page) of the PDF viewer control ASP.NET. Kindly download the sample from the below link. 
 
 
If the provided sample is different from your requirement, kindly provide us more specific details that illustrates your exact requirement. It will be helpful for us to analyze more on your requirement and could provide you the better solution. 
 
Do you have any more information on how to handle things such as filenames, etc in the web service? 
Are you need to get the PDF document file name in the client side or server side of the PDF viewer control? Can you please provide more details about this query? So that we can analyze and provide, the more details about this query. 
 
 
Regards, 
Mohan S 



MS Michael Salzlechner October 7, 2017 09:16 PM UTC

What i would like to be able to do is to simply load a PDF file from a static url like the following

http:\\myserver.com\myfolder\mydcoument.pdf

or from a url like this 

http:\\myserver.com\myfolder\getmydocumen.aspx

both these return a binary file with a mime type of application/PDF

 




MS Mohan Selvaraj Syncfusion Team October 9, 2017 12:11 PM UTC

Hi Mike, 
 
Thank you for your update. 
 
We can load the PDF File from the static URL in the PDF viewer control. We have provided the sample for the same. Kindly download the sample from the below link. 
 
 
Note: In the provided sample we have loaded the tutorial points PDF document. You can change the static URL in the PdfViewerController.cs class in your end. Kindly refer the code snippet below for changing the URL link in the provided sample. 
 
public object DocumentLoad(Dictionary<string, string> jsonResult) 
{ 
  string staticurl = http:\\myserver.com\myfolder\mydcoument.pdf” ; 
   string  filename = “pdfDownlaoded.pdf”; 
        using (WebClient client = new WebClient()) 
                { 
                    client.DownloadFile(staticurl + path + "\\" + filename); 
                } 
} 
 
 
 
If the provided sample is different from your requirement, kindly provide us more specific details that illustrates your exact requirement. It will be helpful for us to analyze more on your requirement and could provide you the better solution.  
 
Regards, 
Mohan S 



MS Michael Salzlechner October 9, 2017 02:14 PM UTC

i would like to download a PDF from a static URL but without having to write an intermediate web service to do so.

seems overkill to have to write a web service to just display a static PDF file



MS Mohan Selvaraj Syncfusion Team October 10, 2017 11:06 AM UTC

Hi Mike, 
 
Thank you for your update. 
 
We can retrieve and load the PDF file from the static URL as base64 string in the client side of the PDF viewer control. The PDF viewer control requires ASP.NET Web API controller or web service to process the PDF document. We process the PDF documents in the PdfViewerHelper class of the EJ.PdfViewer assembly on server side and send the processed PDF document data to the client side for rendering. We have provided the sample for the same. Kindly download the sample from the below link.  
 
 
Note: In the provided sample, we have loaded the tutorial points PDF document from the URL link. You can change the static URL in the client side (index.aspx) file at your end. Kindly refer the code snippet below for retrieving and loading the PDF document from the URL link in the sample. 
  
var staticUrl= , http:\\myserver.com\myfolder\mydcoument.pdf; 
var xhr = new XMLHttpRequest(); 
            xhr.open('GET', staticUrl ,true); 
            xhr.responseType = 'blob'; 
            xhr.onload = function(e) { 
                if (this.status == 200) { 
                    var myBlob = this.response; 
                    var reader = new window.FileReader(); 
                    reader.readAsDataURL(myBlob); 
                    reader.onloadend = function () { 
                        base64data = reader.result; 
                        $('#<%= PdfViewer1.ClientID %>').css("visibility", "visible"); 
                        var pdfviewerObject = $('#<%= PdfViewer1.ClientID %>').data('ejPdfViewer'); 
                        pdfviewerObject.load(base64data); 
                    } 
                } 
            }; 
            xhr.send(); 
 
If the provided sample is different from your requirement, kindly provide us more specific details that illustrates your exact requirement. It will be helpful for us to analyze more on your requirement and could provide you the better solution.   
 
Regards, 
Mohan S 



MS Michael Salzlechner October 14, 2017 01:08 PM UTC

thanks for the example but i cannot get it to work

it downloads the PDF properly but after calling load nothing displays in the PDF control

i am attaching a simple static HTML file that doesnt need any of the server side parts



Attachment: pdfviewertest_2ab11465.zip


MS Mohan Selvaraj Syncfusion Team October 16, 2017 06:26 AM UTC

Hi Mike, 
 
Thank you for your update. 
 
We have analyzed the provided HTML file and we found you missed to set the serviceUrl for PDF viewer control. The serviceUrl is required to parse and load the PDF documents in PDF viewer control. So we need to call the Web API controller(Service side) method before calling the load method on the client side. Kindly refer the below code snippet. 
 
   function buttonClick() { 
            $("#PdfViewer1").ejPdfViewer({}); 
            var viewer = $("#PdfViewer1").data("ejPdfViewer"); 
            viewer.model.serviceUrl="../api/PdfViewer/";  //Serviceurl required to parsing and rendering the document in PDF viewer control. 
            var staticUrl = 'https://www.tutorialspoint.com/w3css/w3css_tutorial.pdf'; 
            var xhr = new XMLHttpRequest(); 
            xhr.open('GET', staticUrl, true); 
            xhr.responseType = 'blob'; 
            xhr.onload = function (e) { 
                if (this.status == 200) { 
                    var myBlob = this.response; 
                    var reader = new window.FileReader(); 
                    reader.readAsDataURL(myBlob); 
                    reader.onloadend = function () { 
                        base64data = reader.result; 
                        viewer.load(base64data); 
                    } 
                } 
            }; 
            xhr.send(); 
        } 
 
 
We have also created the sample for the same, Kindly download the sample from the below link. 
 
 
Note: PDF viewer control required the Web API controller action methods to parse, load and download the PDF documents.  
 
If the provided sample is different from your requirement, kindly provide us more specific details that illustrates your exact requirement. It will be helpful for us to analyze more on your requirement and could provide you the better solution.    
 
Regards, 
Mohan S 



MS Michael Salzlechner October 17, 2017 03:08 PM UTC

not quite sure why it would call and even more so require a server function to display a PDF file from a static location

as i see this right now if i try to display a PDF file on the client it automatically tries to call an upload function on the server to upload the document

Why would it do that?




AA Akshaya Arivoli Syncfusion Team October 18, 2017 12:54 PM UTC

Hi Michael, 
 
Thank you for your update. 

The PDF Viewer control consist of the both server side and the client-side control, not fully client-side control. So, as mentioned earlier the PDF Viewer control requires the Web API controller action methods to parse, load and download and upload the PDF documents. 

Please let us know if you need further assistance on this. 

Regards, 
Akshaya 



MS Michael Salzlechner October 25, 2017 04:03 PM UTC

Ok i got this to work now but i am running into one issue

we will be showing different PDF files dynamically 

I noticed that the control immediately calls the Load function to load a PDF file. At that point we may not know yet what file to display until we load it later

i cant seem to figure out how to make this work. If the initial call to load fails the PDF viewer doesnt work well afterwards






MS Mohan Selvaraj Syncfusion Team October 26, 2017 12:39 PM UTC

Hi Michael, 
 
Thank you for your update. 
 
Are you need to get the PDF document file name in the initial loading of the PDF viewer control on client side? Can you please provide more details about the query or provide the sample for reproducing the issue. It will be helpful for us to proceed further and provide a solution for it. 
 
Regards, 
Mohan S 



MS Michael Salzlechner October 26, 2017 12:46 PM UTC

We have a web page that shows the user a list of documents to view. Until the user selects a document we have nothing to show. Once the user selects a document we load it into the PDF control.

Problem is that the PDFViewer immediately calls the web services load function when it is instantiated. At that time we don't know yet which document the user will select

Also it would be good to be able to 'clear' the PDFviewer so no document is shown any longer




MS Mohan Selvaraj Syncfusion Team October 27, 2017 06:32 AM UTC

Hi Michael, 
 
Thank you for your update. 
 
We can initialize the PDF viewer control without loading the initial PDF document by passing the file name has empty string, Kindly refer the below code snippet in the Load method of the Web API controller. 
 
  public object Load(Dictionary<string, string> jsonResult) 
        { 
            PdfViewerHelper helper = new PdfViewerHelper(); 
            if (jsonResult.ContainsKey("newFileName")) 
            { 
                var name = jsonResult["newFileName"]; 
                var pdfName = name.ToString() + ".pdf"; 
                helper.Load(HttpContext.Current.Server.MapPath("~/Data/" + pdfName)); 
            } 
            else 
            { 
                if (jsonResult.ContainsKey("isInitialLoading")) 
                { 
                    if (jsonResult.ContainsKey("file")) 
                    { 
                        var name = jsonResult["file"]; 
                        var pdfName = name.ToString(); 
                        helper.Load((pdfName)); 
                    } 
                    else 
                    { 
 
                        helper.Load("");//PDF is passed as empty string. 
                    } 
                } 
            } 
            string output = JsonConvert.SerializeObject(helper.ProcessPdf(jsonResult)); 
            return output; 
        } 
 
We have also shared the sample for the same, Kindly download the sample from the below link. 
 
 
Regards, 
Mohan S. 


Loader.
Live Chat Icon For mobile
Up arrow icon