I'm investigating how to use the pdfjs viewer to serve a PDF that is behind a protected resource.
From my understanding, this would work if the resource allowed anonymous access:
https://app.com/pdf.js/web/viewer.html?file=https://app.com/pdf/{id}
The resource https://app.com/pdf/{id}
returns a response with content type application/pdf
.
However, that resource requres a OAuth2 token to be present in the authorization header. So is it possible to modify the headers created by the viewer, to include a authorization header and pass the token of the user?
Right-click on the link in the href attribute and click Copy link address. Create a new Text content block. Click the Embed Media icon and embed the HTML code in an iframe that points to the PDF URL you noted in step 3. Click the checkmark to see the PDF displayed in the newly created iframe.
Today, PDF. js has the best performance, reliability, and accuracy on the latest versions of Firefox and Chrome.
PDF.js can read file in Base64 format (example). So You can use Ajax / HTTP Client to download binary data with authorization header, convert to Base64 string then embed into PDF
Edit: You can set HTTP headers to PDF getDocument function. So you can store access token in Web Storage, then get it in pdf viewer's page
var loadingTask = pdfjsLib.getDocument({
url,
withCredentials,
httpHeaders: {
authentication: "abcxyz",
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With