Generating PDFs from HTML with jsPDF and javascript

What is jsPDF and how can I use it to convert HTML to PDF?
Creating downloadable PDFs from HTML is a crucial feature for many SaaS platforms—especially for invoices, reports, or summaries. One popular approach is using jsPDF, a lightweight JavaScript library capable of generating PDFs on the client side without needing a backend.
In this guide, we’ll explore how to convert HTML to PDF using jsPDF, share best practices for styling and performance, and compare jsPDF to more advanced tools like Playwright, Puppeteer, or dedicated PDF APIs.
You can also check our jsPDF full documentation here.
Alternative PDF Libraries: How jsPDF Compares to Other Tools
jsPDF is the most popular PDF library on npm for generating PDFs purely in JavaScript.

Libraries like Playwright or Puppeteer are widely used for rendering HTML into PDFs via headless browser instances. These tools offer greater accuracy in rendering CSS and are excellent for visually rich documents but come at the cost of increased resource usage.
Its client-side capabilities make it a great option for lightweight use cases where server resources are limited. But it's important to note that jsPDF is a low-level solution, like pdfmake or PDFKit.
If you want to go deep on a full comparison between pdf libraries in javascript for 2025, you can check out this guide.
Step 01: Setting Up jsPDF for HTML to PDF Conversion
You can install jsPDF via npm:
Or by including the CDN:
Here's a basic example of generating a PDF:
Step 02: Your First “Hello World” PDF
When converting HTML to PDF, keeping your HTML clean and structured is essential for accurate rendering. Here's an example of well-organized HTML and CSS that ensures clean outputs:
This minimal HTML, paired with CSS for layout and styling, ensures that the structure will remain consistent when converted into a PDF using jsPDF.
Step 03: Basic PDF Generation
Below are some jsPDF examples to create robust PDF documents:
Custom Text: Control fonts and colors:
Image Support: Embed images directly into the PDF:
Tables: Utilize the AutoTable plugin for structured data:
Adding pages: Multiple page documents
Heres an example of a complete pdf using only native jsPDF
Below is a self-contained JavaScript snippet demonstrating only jsPDF usage. It shows how to create a PDF with a heading, styled text, a table, and multiple sections (on separate pages). Make sure you have the jsPDF library included (e.g., via a <script> tag or npm import) before running this script.
Step 04: Convert HTML to PDF Using html2canvas
The html2canvas library is a powerful tool that captures HTML elements and renders them into canvas images, which can then be embedded into PDFs using jsPDF.
This approach allows for a more accurate representation of complex HTML layouts, including CSS styles and images, which jsPDF alone might not fully support. Here’s how you can use html2canvas with jsPDF to convert HTML to PDF:
In this example, html2canvas captures the HTML element with the ID content
, converts it into an image, and then jsPDF embeds that image into the PDF. This technique is ideal for preserving the exact look and feel of your HTML content, including complex layouts, images, and CSS.
Tip: Use HTML Template Engines
Using an HTML template engine like Handlebars can streamline the generation of dynamic PDFs by templating your HTML and filling it with data programmatically.
Here’s a basic example using Handlebars:
Alternative: Scaling PDF Generation with Third-Party APIs

For larger SaaS platforms requiring automated PDF generation at scale, integrating a PDF Generation API like pdforge can offload the heavy lifting. This approach is ideal for SaaS platforms with high volumes of PDF requests.
With pdforge, you can create beautiful reports with flexible layouts and complex components with an easy-to-use opinionated no-code builder. Let the AI do the heavy lifting by generating your templates, creating custom components or even filling all the variables for you.
You can handle high-volume PDF generation from a single backend call.
Here’s an example of how to generate pdf with pdforge via an API call:
You can create your account, experience our no-code builder and create your first layout template without any upfront payment clicking here.
Conclusion
jsPDF is a powerful solution to convert HTML to PDF quickly on the client side. With the help of html2canvas and best practices like image compression and smart pagination, you can generate high-quality PDFs for invoices, reports, or summaries in your SaaS platform.
For more complex use cases—like large-scale PDF generation or advanced layouts—consider tools like Playwright, Puppeteer.
If you don't want to waste time maintaining pdfs layouts and their infrastructure or if you don't want to keep track of best practices to generate PDFs at scale, third-party PDF APIs like pdforge will save you hours of work and deliver a high quality pdf layout.

Try for free
7-day free trial