Internet Explorer fails opening a pdf string file

15,112

I found the solution and I want to share anyone who has the same problem. You can see the demo here : https://jsfiddle.net/quangminh_ln/hy36tnt6/

'use strict';

var data = "...Your PDF base64 string...";
var fileName = "your_file_name";
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE workaround
    var byteCharacters = atob(data);
    var byteNumbers = new Array(byteCharacters.length);
    for (var i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    var byteArray = new Uint8Array(byteNumbers);
    var blob = new Blob([byteArray], {type: 'application/pdf'});
    window.navigator.msSaveOrOpenBlob(blob, fileName);
}
else { // much easier if not IE
    window.open("data:application/pdf;base64, " + data, '', "height=600,width=800");
}

The link that I saw for my solution : https://viethoblog.wordpress.com/2016/08/30/loaddisplay-pdf-from-base64-string-bonus-ie-workaround/

Share:
15,112
Marta
Author by

Marta

Updated on June 09, 2022

Comments

  • Marta
    Marta about 2 years

    I receive (from a webservice I don't manage) a string with the content of a pdf file.

    On client's side, I use this function:

    window.open('data:application/pdf;base64,'+encodeURI(TheStringWithThePdfContent));

    As usual, it works in every browser but IE (11 in my case), which shows an alert with the message: "Do you want to allow this website to open an app on your computer?"

    If I say no, an empty white page is opened.

    If I say yes, it tries to open a "data" file (as it reads from the protocol in window.open, I guess) and, as it doesn't find any application to do that, sends me to the Microsoft application store, which just suggests me to download "iMusic"

    Completely useless, of course.

    I've changed all the Internet Options I've guessed could help, none works.

    Any suggestion?

    Thanks in advance,

  • Marta
    Marta about 7 years
    It does work! Finally I changed the server side to change the display of the page, Liferay (where I was receiving and managing the pdf stream) allows to serve a direct pdf page, but it wouldn't have been the case this solution would have been perfect! I've tested with my own data with different browsers, everything works fine. Thanks a lot for your time!
  • The Anh Nguyen
    The Anh Nguyen almost 7 years
    What the hex? Please read stackoverflow.com/help/how-to-answer to make a good and clear answer.
  • alphiii
    alphiii almost 7 years
    This is pure awesomeness :) I would give you +10
  • Daniel L. VanDenBosch
    Daniel L. VanDenBosch almost 7 years
    How could I integrate this solution using the following file. drive.google.com/file/d/0B-umo7G978pdalJrdVhibHU4Y0k/…
  • SanjiMika
    SanjiMika almost 7 years
    @DanielL.VanDenBosch Could you try to put the content of file PDF "JVBERi0xLjUNCiW1tbW1DQoxIDAgb2 ..." in the variable data. I hope that it works !
  • Mdk
    Mdk about 6 years
    Would this work for displaying a PDF embedded in a page somehow? Perhaps through an iframe? Embed only works on FF and Chrome as far as I've tried
  • Djov
    Djov over 5 years
    Thank you very much for this. This is the only correct answer I could find.