Internet Explorer fails opening a pdf string file
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/
![Marta](https://lh3.googleusercontent.com/-CTU7Oj0uGwE/AAAAAAAAAAI/AAAAAAAB1S4/WSQ0vPrbCLY/photo.jpg?sz=256)
Marta
Updated on June 09, 2022Comments
-
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 about 7 yearsIt 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 almost 7 yearsWhat the hex? Please read stackoverflow.com/help/how-to-answer to make a good and clear answer.
-
alphiii almost 7 yearsThis is pure awesomeness :) I would give you +10
-
Daniel L. VanDenBosch almost 7 yearsHow could I integrate this solution using the following file. drive.google.com/file/d/0B-umo7G978pdalJrdVhibHU4Y0k/…
-
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 about 6 yearsWould 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 over 5 yearsThank you very much for this. This is the only correct answer I could find.