'PDF.JS in ASP- How to Open PDF in Viewer located in a folder different than viewer.js

In PDF.js, using viewer.js and viewer.html, I can specify a PDF by assigning the filename in defaultURL.value, but the PDF must exist in the same folder as viewer.js. I want to specify the pdf file location in a parameter.

I tried setting the docBaseURL, but it wants an absolute path and will not accept http://localhost/. No matter what I put there, it looks to the baseURI of the PDF named.

I have looked and set breakpoints at all references in viewer.js for opening the document or specifying the file location, but none specify the relative links, only the variable "file". I searched the github project, but found no issues as to how to set relative links, only if the absolute URL is http://something.

I am trying to modify viewer.html for my own use. I don't want to call it from an iFrame or use a querystring. I want to set the defaultURL, then change the code so it will look for the file in the path or relative path that I specify.



Solution 1:[1]

I gave up on trying to alter pdfjs and instead wrote a javascript function that calls the viewer and constructs the path from the viewer to where I store my pdf documents.

Location of pdf documents:  example.com/doc/
Location of viewer:   example.com/libraries/pdfjs/web/
HTML calling javascript function:

<a hef="doc/example.pdf" onclick="callViewer(this.href)"></a>

Javascript function:

function callViewer(url) {
    var pdf = url.split('doc/').pop();
    var newurl = "https://example.com/libraries/pdfjs/web/viewer.html?file=../../../../doc/" + pdf + "#page=1&zoom=page-width"; 
    window.location.assign(url); 
}

Notes:

  1. An absolute file path is needed in the javascript function for newurl.
  2. All pdfs are assumed to be in the same folder (but that could be altered easily enough).
  3. A right click on the HTML link to open the pdf renders the pdf with the browser's default pdf renderer (not the viewer), which I like and use for unproblematic printing across browsers and devices and operating systems.
  4. This solution keeps the html code simple, my pdfs stored where I want them, and the direct URL of the pdf indexed by search engines (rather than the convoluted URL in newurl).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kay