How to open a pdf downloaded from an API with JavaScript

On my most recent assignment I was faced with the challenge of downloading a pdf file from an API and displaying it in the browser.

Normally, the API would provide a json-object with a link to the pdf, which can then be used to open a new browser tab/window and the browser or platform takes care of the rest.

In this case however the API was designed for native mobile apps and required the presence of a custom ‘api-key’ http-header, which made it impossible to just provide the url to the browser.

To solve this it was necessary to download the file to memory with an XHR request and then get the browser to open or download it with whatever plugin/UI it normally uses for pdf file.

For the XHR request we use the Fetch API with the whatwg-fetch polyfill. In essence the Fetch API fetch() method returns a response, from which a blob can be created. This blob object can be use to create an objectURL, which can then be used as href in a link.


This solution works on Chrome, Safari, Firefox, Opera, IE11 and Edge.


  1. Habr

    Thank you soooo much! You saved my day.
    In case of IE I use window.navigator.msSaveOrOpenBlob(newBlob, filename) however would you know how to set the title of a new window or file name in “other” browsers?

  2. Habr

    …I meant in case I use instead of link

  3. Venkat

    Thanks for this, is there a way to make this work in FireFox?

  4. Venkata Reddy

    For me in Firefox the file is not getting downloaded. But working perfectly in all other browsers. Also you mentioned “This solution works on Chrome, Safari, Opera, IE11 and Edge” in this article.

    • Anders Poulsen

      You are right. I don’t know how I did not think to also test Firefox. I don’t know how to fix it right at this moment, however.

  5. Venkat

    I had to set timeout for firefox to fix it, ref: Probably you can update this article. Thanks again for this article :).

    • Anders Poulsen

      Thanks for the solution. At first I did have a timeout before I removed the resource, but it turned out not to be necessary in the browsers I remembered to test with. I’ll have to update the article. Thanks again.

  6. Viswa

    This is not working in safari, it is opening as unknown file and saving as data file. Is there any solution for that

    • Anders Poulsen

      Are you using Safari on OS X or iOS? And what version?
      Could it be that the server is not setting the correct mime-type on the http response?

  7. Raul

    Thank you. This work in FireFox with the below modification

    // For other browsers:
    // Create a link pointing to the ObjectURL containing the blob.
    const data = window.URL.createObjectURL(newBlob);
    var link = document.createElement(‘a’);
    link.href = data;”file.pdf”;
    // For Firefox it is necessary to delay revoking the ObjectURL
    , 100}

  8. Brian DeMasse

    Thank you so much for this! I had a really difficult time just showing a PDF in Vue.js. I altered it a little, below:

    const showPDF = (function () {
    return function (blob, fileName) {
    var newBlob = new Blob([blob], {type: ‘application/pdf’})
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    const data = window.URL.createObjectURL(newBlob)
    var link = document.createElement(‘a’)
    link.href = data
    setTimeout(function () {

  9. Ok, I see I’m far from the only one who is so so happy this exists but you totally saved my day and my code works perfectly now. I used this with a react application calling a API in node/express which generated PDF files with pdfkit. React routing really got in the way of my prior approach but this worked out of the box so thank you thank you thank you!!!

Leave a Reply