Client-Side HTML to PDF Conversion

0.14.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import `html2pdf.js` as an ES module, create an HTML element dynamically, and then use the `html2pdf` function with common options to generate and save a PDF document from that element.

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);
});

view raw JSON →