

In the example above, you only rendered the h1 title. Choose the element and save the PDF for your user.īutton.addEventListener(' click', generatePDF) const element = document.getElementById(' invoice') Choose the element that your content will be rendered to. If you prefer this method, add a script tag to your HTML file:Ĭonst button = document.getElementById(' download-button') To get started, install the html2pdf library. jsPDF then takes the image and converts it to a PDF file.Ĭheck out How to Convert HTML to PDF Using React for a step-by-step guide on how to use jsPDF in a React app. Html2canvas renders an HTML page into a canvas element and turns it into a static image. It uses html2canvas and jsPDF under the hood.

The html2pdf library converts HTML pages to PDFs in the browser. Lastly, we’ll cover the advantages and disadvantages of both libraries. In this post, we’ll take a look at two popular JavaScript libraries for converting HTML pages to PDFs. For example, invoices, concert tickets, and flight tickets tend to be available as PDF downloads. This article was first published in March 2019 and was updated in November 2022.Ī common use case in JavaScript for HTML-to-PDF conversion is giving your website visitors the ability to download HTML content as a PDF file.
