Ng2-pdf-viewer: Is there any way we can read the file from server wherein an authentication is required with JWT?

Created on 13 Apr 2018  路  5Comments  路  Source: VadimDez/ng2-pdf-viewer

I saw that there is an option to share the link for the link for source path.
Now lets image that we need to read file from server which required authentication to read the file, the user is implemented with JWT i.e. token based in headers.

Now, a HTTP call or headers are required to read the PDF from server.

Is there any way we can achieve this with this package?

Most helpful comment

@xyrintech pass Object with headers to [src] instead of just an URL string:

pdfObject = {
  url: 'some url to PDF',
  httpHeaders: { Authorization: 'Bearer XYZ' },
  withCredentials: true,
}

and then in the view

<pdf-viewer [src]="pdfObject" ...

All 5 comments

Could this be achieve by having a GET request that is authorized by the token, and sends back an arraybuffer containing the pdf and then use that to have an UInt8Array as [src] ?

I might be misunderstanding the question.

Using an auth pipe could solve this:
http://blog.jsgoupil.com/request-image-files-with-angular-2-and-an-bearer-access-token/
So you have to D/L the PDF file by yourself and transform it to an arraybuffer I as gdgunnars already mentioned

@xyrintech pass Object with headers to [src] instead of just an URL string:

pdfObject = {
  url: 'some url to PDF',
  httpHeaders: { Authorization: 'Bearer XYZ' },
  withCredentials: true,
}

and then in the view

<pdf-viewer [src]="pdfObject" ...

One little problem i might add. I used this setup, works as expected but if a have a click listener on a button to change the url to a different pdf, the change occurs but the new pdf isn't rendered. What am i missing ? I also opened an issue with more details about this problem of mine.

Kind regards,

pdfObject = {
url: 'some url to PDF',
httpHeaders: { Authorization: 'Bearer XYZ' },
withCredentials: true,
}

In order to work correctly, you need to specify the type of the Object like this:

pdfObject:PDFSource = {
  url: 'some url to PDF',
  httpHeaders: { Authorization: 'Bearer XYZ' }
}

This is the only way that JWT works for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VadimDez picture VadimDez  路  5Comments

UlricW picture UlricW  路  3Comments

roginneil picture roginneil  路  3Comments

xmran picture xmran  路  4Comments

fncamm picture fncamm  路  4Comments