2

Google docs viewer is quite good and easy to use tool. It only requires the file's path and renders the document. It also has a lot of additional features like choosing page, rotating, etc. However is there a way to set some authorization or additional headers to the call that gets the file ?

As far as I'm aware the document url is passed like that:

http://docs.google.com/viewer?url=<URL_OF_DOCUMENT>&embedded=true

I think this is very important because most of the files are secured ..

The ng-pdf-viewer for example has the option to receive an object as source:

 this.sourceObject = {
     src: this.document.documentLink, 
     httpHeaders: { Authorization: 'Bearer ' + token }
 }

Thanks in advance :)

user3568791
  • 676
  • 1
  • 7
  • 22
  • could you give me a full example for that https://stackoverflow.com/questions/54535679/google-docs-viewer-with-files-requiring-authorization – Nael Zidan Oct 20 '19 at 19:33

1 Answers1

3

I found the solution! The files should be loaded as blobs:

new Blob([new Uint8Array(response)], { type: 'application/pdf' })

and then the blobUrl should be passed to the viewer:

this.iframeSrc = `https://docs.google.com/viewer?url=${this.document.blobUrl}&embedded=true`;
user3568791
  • 676
  • 1
  • 7
  • 22