Categories
JavaScript Answers

How to open generated PDF using jspdf in new window with JavaScript?

Spread the love

Sometimes, we want to open generated PDF using jspdf in new window with JavaScript.

In this article, we’ll look at how to open generated PDF using jspdf in new window with JavaScript.

How to open generated PDF using jspdf in new window with JavaScript?

To open generated PDF using jspdf in new window with JavaScript, we call the doc.output method.

For instance, we write

doc.output('dataurlnewwindow');

to open the PDF doc in a new window.

We can also write

const string = doc.output("datauristring");
const x = window.open();
x.document.open();
x.document.location = string;

to call output with 'datauristring' to return the base64 string URL version of the PDF.

Then we call window.open to open a new window.

And then we call document.open to open the document.

Finally we set document.location to the URL string to show the PDF.

Conclusion

To open generated PDF using jspdf in new window with JavaScript, we call the doc.output method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *