Client-Side HTML to PDF Conversion
html2pdf.js is a JavaScript library designed for client-side conversion of HTML elements or entire webpages into printable PDF documents. It leverages `html2canvas` for rendering HTML to a canvas and `jsPDF` for generating the PDF output. The current stable version is 0.14.0, released in January 2026. This library is strictly browser-based and does not run in Node.js environments. It offers a promise-based API for advanced workflow customization, allowing developers to chain operations and insert custom functions between steps of the conversion process. Key differentiators include its entirely client-side operation, eliminating server-side rendering, and its modular, configurable workflow that provides fine-grained control over the PDF generation process, including page-break handling, image quality, and progress tracking.
Common errors
-
ReferenceError: html2pdf is not defined
cause Attempting to use `html2pdf` in a Node.js environment, or the script/module was not loaded correctly in the browser.fixEnsure the code is running in a browser. If using npm, verify your bundler (e.g., Webpack, Rollup) correctly includes `html2pdf.js`. If using CDN, confirm the script tag is present and loaded before `html2pdf` is called. -
TypeError: Cannot read properties of undefined (reading 'Worker')
cause Incorrectly trying to destructure `Worker` as a named import (e.g., `import { Worker } from 'html2pdf.js'`) when it's a property of the default export.fixImport the default export first, then access `Worker` as a property: `import html2pdf from 'html2pdf.js'; const worker = new html2pdf.Worker();`. -
Some HTML elements or styles are missing/incorrect in the generated PDF.
cause Limitations of `html2canvas` in accurately replicating all browser rendering features, especially for advanced CSS or dynamic content not fully stable at the time of canvas capture.fixAdjust `html2canvas` options (e.g., `scale`, `useCORS`, `logging`). Ensure images are loaded and fonts are available. For complex layouts, try simplifying the HTML/CSS structure specifically for the PDF conversion target, or delay conversion until all dynamic content has settled.
Warnings
- gotcha html2pdf.js is designed exclusively for client-side browser environments and cannot be run in Node.js. Attempts to use it server-side will result in errors.
- breaking Version 0.13.0 updated the underlying jspdf dependency from 3.0.3 to 4.0.0. This major version bump in jspdf may introduce breaking changes or API alterations that could affect advanced custom configurations or integrations directly interacting with jspdf objects.
- gotcha When manually including html2pdf.js via script tags, differentiate between `html2pdf.bundle.min.js` and `html2pdf.min.js`. The `bundle` version includes its dependencies (`html2canvas` and `jsPDF`), while the unbundled version requires these dependencies to be loaded separately.
- gotcha The quality of PDF output can be highly dependent on the complexity of HTML and CSS. Certain CSS properties (e.g., `flexbox`, `grid`, `z-index` with complex stacking contexts) may not render perfectly due to the underlying `html2canvas` limitations in faithfully replicating browser rendering.
Install
-
npm install html2pdf.js -
yarn add html2pdf.js -
pnpm add html2pdf.js
Imports
- html2pdf
const html2pdf = require('html2pdf.js');import html2pdf from 'html2pdf.js';
- html2pdf.Worker
import html2pdf from 'html2pdf.js'; const worker = new html2pdf.Worker();
- Global `html2pdf`
import html2pdf from 'https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.14.0/html2pdf.bundle.min.js';
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.14.0/html2pdf.bundle.min.js"></script> // Then access directly: html2pdf(element);
Quickstart
import html2pdf from 'html2pdf.js';
const content = document.createElement('div');
content.innerHTML = `
<h1>Hello, PDF!</h1>
<p>This is some sample content that will be converted to a PDF document.</p>
<img src="https://via.placeholder.com/150" alt="Placeholder Image">
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
`;
// Append content to body for demonstration, normally it would be an existing element
document.body.appendChild(content);
// Generate PDF from the content element
html2pdf(content, {
margin: 1,
filename: 'document.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, logging: true, dpi: 192, letterRendering: true },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
}).then(() => {
console.log('PDF generation complete!');
// Optionally remove the appended content if it was temporary
document.body.removeChild(content);
}).catch(error => {
console.error('PDF generation failed:', error);
});