0

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.

pghcpa
  • 833
  • 11
  • 25

1 Answers1

0

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).
Kay
  • 41
  • 6
  • Thanks for nicely (!) written post. Requirement of question was to not use querystrings or iframes, a requirement of my scenario. I worked around by converting file to bytearray as described at below link, but that's not an answer to this posted question. https://stackoverflow.com/questions/28430724/how-to-open-base64-string-response-from-server-in-external-viewer-viewer-html – pghcpa Apr 21 '22 at 19:38
  • Glad you found a way. I found no direct workaround to the parameter issue. thanks for linking the stackoverflow post. Unfortunately, beyond my capabilities. This is why I gave up and wrote a javascript function that meant I only had to use a parameter in one spot, and could change that if my site structure changed. Glad you found a solution that didn't involve giving up! – Kay Apr 22 '22 at 21:03