Sometimes, we want to open PDF string in new window with JavaScript.
In this article, we’ll look at how to open PDF string in new window with JavaScript.
How to open PDF string in new window with JavaScript?
To open PDF string in new window with JavaScript, we can open an empty window and then render an iframe in it.
For instance, we write
const pdfWindow = window.open("");
pdfWindow.document.write(
`<iframe width='100%' height='100%' src='data:application/pdf;base64,${encodeURI(
yourDocumentBase64VarHere
)}''></iframe>`
);
to call window.open
with an empty string to open an empty window.
Then we call pdfWindow.document.write
with a string with an iframe element in it.
And its src attribute is set to the base64 PDF URL.
Then the PDF will be rendered in the iframe.
Conclusion
To open PDF string in new window with JavaScript, we can open an empty window and then render an iframe in it.