javascriptpdfgoogle-docsdocumentviewer

Google docs viewer with files requiring authorization?


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 :)


Solution

  • 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`;