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="https://stackoverflow.com/questions/2805330/data:application/pdf;base64," +
    encodeURI(yourDocumentBase64VarHere) + ""></iframe>"
)

Leave a Comment