Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PDF.js viewer to display a pdf served by a protected resource

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?

like image 435
Tommy Jakobsen Avatar asked Aug 03 '18 09:08

Tommy Jakobsen


People also ask

How do I display a pdf in iframe?

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.

Does pdf JS work in Chrome?

Today, PDF. js has the best performance, reliability, and accuracy on the latest versions of Firefox and Chrome.


1 Answers

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",
  }
});
like image 188
hgiasac Avatar answered Sep 22 '22 05:09

hgiasac