2
votes

I have demo project of PDFTron and also have Booking.pdf on same project location on my local.

I write following code for load PDF into PDFTron

<html>
<head>
  <script src="jquery-1.7.2.min.js"></script>
  <script src="lib/WebViewer.js"></script>

    <style>
        #viewer {
            width: 1024px;
            height: 600px;
        }
    </style>
 </head>
<body class="page-reader">
  <div id="viewer"></div>
</body>
<script>

    $(function() {
        var viewerElement = document.getElementById("viewer");
        var myWebViewer = new PDFTron.WebViewer({
            path: "lib",
            type: "html5",
            documentType: "pdf",
            initialDoc: "Booking.pdf"
        }, viewerElement);
    });

</script>
</html>

Now Question is when I'm trying to load pdf/xod file from local directory it's working fine, But suppose I want to get PDF file from other server e.g : http://serverURL/Booking.pdf that time I include server path into initialDoc: "http://serverURL/Booking.pdf" then it's giving error to me.

Error is network error or not found.

How can I load external pdf/xod into pdftron?

I referred following links to resolve the issue but I am not able to resolved the error.

http://www.pdftron.com/webviewer/demo/documentation.html

http://www.pdftron.com/webviewer/demo/doc/WebViewer_Developer_Guide.pdf

Please help me to resolve this issue.

1
Hi Deepak as per documentation initialDoc should be absolute path initialDoc the URL path to a xod document to load on startup, it is recommended to use absolute paths. try with another option - pramod kadam

1 Answers

1
votes

The InitialDoc doesn't have to be absolute path, it can be some token i.e. something in your session etc. Which you can read and get the file binary. In my case asp.net Web-forms ASMX service, i create one web-method with GET, all PDFViewere does is making HTTP GET with specified intialDoc value. You just need to capture like that:

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public string GetFile(string token)
{
    byte[] fileBinary = null;// Read file here from DB or Service.

    if (fileBinary != null)
    {
        var response = HttpContext.Current.Response;
        response.Clear();
        response.ContentType = "application/pdf";
        response.AddHeader("Content-Length", fileBinary.Length.ToString());
        response.OutputStream.Write(fileBinary, 0, fileBinary.Length);
        response.Flush();
        response.End();
    } 
    //We need to return this for no apparent reason (PDFViewer get action need that).
    return token;
}


 $(function () {
    var customData = { serviceUrl: 'services/PDFWebService.asmx', token: '<%=initialDoc.Value %>', isReadonly: '<%=IsReadonly?"yes":"no" %>' };
    var myWebViewer = new PDFTron.WebViewer({
        path: "Resources/js/PDFTron",
        mobileRedirect: false,
        stream: true,
        config: 'Resources/js/PDFViewerConfig.js',
        documentType: "pdf",
        custom: JSON.stringify(customData),
        l: '<%=LicenseKey%>',
        initialDoc: customData.serviceUrl + '/GetFile?token=' + customData.token
    }, document.getElementById('viewer'));
});