Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 231
Marta Avatar asked Mar 28 '17 13:03

Marta


People also ask

Why can t I Open PDF in Internet Explorer?

Open Internet Explorer and choose Tools > Manage Add-ons. Under Add-on Types, select Toolbars and Extensions. In the Show menu, choose All add-ons. In the list of add-ons, select Adobe PDF Reader.

How do I open PDF files in Internet Explorer?

First, open the HTML file or load the web site page in your Internet Explorer browser window. Then select 'Print...' from the web browser's File menu. You will then be able to convert the HTML web page to a PDF copy of the page on your computer.


1 Answers

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/

like image 73
SanjiMika Avatar answered Oct 05 '22 19:10

SanjiMika