eKoopmans /html2pdf.js
Client-side HTML-to-PDF rendering using pure JS.
★ 4,937⑂ 1,712👁 62⑃ 352● 682▤ 10
+0
Daily
4,937
stars
1,712
forks
62
watchers
352
prs
682
issues
10
releases
README
html2pdf.js
html2pdf.js converts any webpage or element into a printable PDF entirely client-side using html2canvas and jsPDF.
Table of contents
- Getting started
- CDN
- Raw JS
- NPM
- Bower
- Console
- Usage
- Advanced usage
- Workflow
- Worker API
- Options
- Page-breaks
- Page-break settings
- Page-break modes
- Example usage
- Image type and quality
- Progress tracking
- Dependencies
- Contributing
- Issues
- Tests
- Pull requests
- Credits
- License
Getting started
CDN
The simplest way to use html2pdf.js is to include it as a script in your HTML by using cdnjs:
html
Using a CDN URL will lock you to a specific version, which should ensure stability and give you control over when to change versions. cdnjs gives you access to all past versions of html2pdf.js.
Note: Read about dependencies for more information about using the unbundled version dist/html2canvas.min.js.
Raw JS
You may also download dist/html2pdf.bundle.min.js directly to your project folder and include it in your HTML with:
html
NPM
Install html2pdf.js and its dependencies using NPM with npm install --save html2pdf.js (make sure to include .js in the package name).
Note: You can use NPM to create your project, but html2pdf.js will not run in Node.js, it must be run in a browser.
Bower
Install html2pdf.js and its dependencies using Bower with bower install --save html2pdf.js (make sure to include .js in the package name).
Console
If you're on a webpage that you can't modify directly and wish to use html2pdf.js to capture a screenshot, you can follow these steps:
1. Open your browser's console (instructions for different browsers here).
2. Paste in this code:
js
function addScript(url) {
var script = document.createElement('script');
script.type = 'application/javascript';
script.src = url;
document.head.appendChild(script);
}
addScript('https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js');
3. You may now execute html2pdf.js commands directly from the console. To capture a default PDF of the entire page, use html2pdf(document.body).
Usage
Once installed, html2pdf.js is ready to use. The following command will generate a PDF of element-to-print and prompt the user to save the result:
js
var element = document.getElementById('element-to-print');
html2pdf(element);
Advanced usage
Every step of html2pdf.js is configurable, using its new Promise-based API. If html2pdf.js is called without arguments, it will return a Worker object:
js
var worker = html2pdf(); // Or: var worker = new html2pdf.Worker;
This worker has methods that can be chained sequentially, as each Promise resolves, and allows insertion of your own intermediate functions between steps. A prerequisite system allows you to skip over mandatory steps (like canvas creation) without any trouble:
js
// This will implicitly create the canvas and PDF objects befoTop issues
#19html2pdf failed with too many pages65 comments · 2017-07-11#152 Firefox hangs on using this29 comments · 2018-09-12#44Can't get table width to fit to page24 comments · 2017-10-29#1840.9.1: I can't make the page-break options work at all23 comments · 2018-12-04#397html2pdf.js produces a blank document on iOS21 comments · 2021-02-19#227Page break avoid-all starts failing in longer documents21 comments · 2019-05-14#219CSS rules are not getting applied21 comments · 2019-05-05#146Is there a way to print the pdf, with the real contents on html hidden?19 comments · 2018-08-28
Latest release
v0.14.0 v0.14.0 2026-01-120.14.0 (2026-01-12) Features Sanitize text sources (877) (988826e)