Opening PDF String in new window with javascript Opening PDF String in new window with javascript javascript javascript

Opening PDF String in new window with javascript


Just for information, the below

window.open("data:application/pdf," + encodeURI(pdfString)); 

does not work anymore in Chrome. Yesterday, I came across with the same issue and tried this solution, but did not work (it is 'Not allowed to navigate top frame to data URL'). You cannot open the data URL directly in a new window anymore.But, you can wrap it in iframe and make it open in a new window like below. =)

let pdfWindow = window.open("")pdfWindow.document.write(    "<iframe width='100%' height='100%' src='data:application/pdf;base64, " +    encodeURI(yourDocumentBase64VarHere) + "'></iframe>")


var byteCharacters = atob(response.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 file = new Blob([byteArray], { type: 'application/pdf;base64' });var fileURL = URL.createObjectURL(file);window.open(fileURL);

You return a base64 string from the API or another source. You can also download it.


You might want to explore using the data URI. It would look something like.

window.open("data:application/pdf," + escape(pdfString));

I wasn't immediately able to get this to work, possible because formating of the binary string provided. I also usually use base64 encoded data when using the data URI. If you are able to pass the content from the backend encoded you can use..

window.open("data:application/pdf;base64, " + base64EncodedPDF);

Hopefully this is the right direction for what you need. Also note this will not work at all in IE6/7 because they do not support Data URIs.