Search code examples
pdfbase64internet-explorer-11

Internet Explorer fails opening a pdf string file


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,


Solution

  • 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/