html2pdf.js: rasterize the DOM, write the PDF, all before leaving the browser
Client-side HTML-to-PDF rendering using pure JS.
At a glance
- What is it?
- html2pdf.js is Erik Koopmans' MIT-licensed client-side converter that turns any webpage or element into a printable PDF using html2canvas and jsPDF, exposed through a one-line call and a chainable Worker API built as a Promise subclass. It deliberately runs only in browsers, never Node.js, and installs via CDN, npm, Bower or a raw script tag.
- Who is it for?
- Use html2pdf.js when PDF generation must happen in the browser without a server round trip, the document is DOM-renderable, and screenshot fidelity is an acceptable representation of the layout. Choose a headless browser or a server-side typesetting engine when the output must contain selectable text, since the html2canvas path is fundamentally rasterization, or when generation must run in Node.js, which the project explicitly does not support.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository received new commits within the last day.
- What is it written in?
- Mainly JavaScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on September 30, 2026, and from our analysis. They are not legal advice.
Editorial analysis
Two libraries, one pipeline
html2pdf.js converts any webpage or element into a printable PDF entirely client-side, and it does so by orchestrating two existing libraries rather than implementing either half itself. html2canvas rasterizes the DOM into a canvas image, and jsPDF wraps that image into a PDF document. The value added is everything around that pipeline, source handling for strings, elements, canvases and images, page-break control, image type and quality options, progress tracking, and a Promise-based API over the whole sequence. The dependencies in package.json name exactly those pieces, html2canvas at 1.x and jspdf at 4.x, joined more recently by dompurify at 3.x. The author is Erik Koopmans, the license is MIT, and the current release is v0.14.0 from 2026-01-12, with the repository last pushed on 2026-09-25. The homepage for the project is hosted through GitHub Pages, and the repository carries its own demo folder rather than relying on an external site, so every claim about rendering behavior can be checked against pages that ship with the source.
Four install paths and one hard rule
Getting started offers a path for every kind of page. The CDN route embeds cdnjs with an integrity hash and cross-origin attribute, locking to a specific version, which the README argues ensures stability and gives you control over when versions change, with all past versions available on cdnjs. The raw route downloads dist/html2pdf.bundle.min.js directly and includes it with a plain script tag. NPM installs with npm install --save html2pdf.js, with a warning to make sure to include the .js in the package name, and Bower mirrors the same command. The hard rule follows immediately, you can use NPM to create your project, but html2pdf.js will not run in Node.js, it must be run in a browser. The unbundled variant splits the dependencies out, and the README's dependencies note covers when that matters for the html2canvas file.
The console trick for pages you cannot edit
The most unusual getting started section is the console path, for a webpage you cannot modify directly and wish to capture as a screenshot. The recipe is three steps, open the browser console, paste a small loader, and then run commands. The loader is a function that creates a script element, sets its source to the cdnjs bundle URL, and appends it to the document head:
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');After that, html2pdf.js commands run directly from the console, with html2pdf(document.body) producing a default PDF of the entire page. It is effectively a bookmarklet workflow, and it doubles as the fastest possible demo of the library on arbitrary real pages.
A Worker that is a Promise subclass
Basic usage is two lines:
var element = document.getElementById('element-to-print');
html2pdf(element);which generates the PDF and prompts the user to save. The advanced surface begins when html2pdf is called without arguments, returning a Worker object, var worker = html2pdf() or new html2pdf.Worker. The Worker's methods chain sequentially as each Promise resolves, and your own intermediate functions can be inserted between steps. A prerequisite system allows skipping mandatory steps like canvas creation without trouble, so html2pdf().from(element).save() implicitly creates the canvas and PDF objects before saving. The enforced workflow is a fixed pipeline:
.from() -> .toContainer() -> .toCanvas() -> .toImg() -> .toPdf() -> .save()Each stage produces what the next consumes, and the prereq system fills in any stage you omitted rather than failing on the gap.
then, thenCore, thenExternal, and four aliases
The Worker subclasses Promise, which produces an unusual three way split in its then methods. then is the standard Promise method with this re-bound to the Worker and added progress tracking, and it returns a Worker so chaining continues. thenCore is the same without progress tracking. thenExternal is a true Promise method that exits the Worker chain entirely, after which Worker methods cannot be chained, and catchExternal mirrors it for rejections. error throws inside the Worker's chain. Four aliases exist for convenience, saveAs for save, using for set, export for output, and run for then, a small vocabulary layer for code that reads better with different verbs. The get method retrieves a property as a Promise or through a callback, and set applies option objects, making configuration part of the same chain rather than a separate constructor step.
from string, element, canvas or img
Sources are not limited to DOM nodes. from takes src with an optional type of string, element, canvas or img, so an HTML string, an existing canvas, or an already loaded image can enter the pipeline directly. to converts to a target of container, canvas, img or pdf, and each target has its own toX method, toContainer, toCanvas, toImg and toPdf, callable directly. Output routing follows the same pattern, output dispatches to outputPdf or outputImg based on the src. outputPdf passes its type and options to the jsPDF object's output method, inheriting whatever output forms jsPDF supports. outputImg returns the image as a Promise in one of the supported types, img, datauristring or dataurlstring, and datauri or dataurl, the forms an image upload or preview consumes. save takes an optional filename and creates the user download prompt. The routing rule keeps the API symmetrical, whatever shape went in with from can come back out through the matching output method, and the data URI forms mean the image side of the pipeline can feed an img tag or an upload field without ever touching a file.
Types, dompurify, and a bundle versus parts choice
The package's own metadata shows its current shape. The published package ships src, dist and a type.d.ts, so TypeScript users get definitions without a separate types package, and main points at dist/html2pdf.js while the README instructs raw users to grab the bundle, dist/html2pdf.bundle.min.js, instead. The runtime dependencies are three, dompurify for sanitizing HTML strings on their way in, html2canvas and jspdf as the two engines. The keywords claim the exact territory, javascript, pdf-generation, html, client-side, canvas. Development tooling is heavier than the library, webpack for the build with an analyzer mode, lit and vue 2.7.16 and bootstrap among the test fixtures, pdfjs-dist and sinon for verification, and brightspace-ui testing components for the runner.
Visual diff tests and a pagebreaks demo
The test suite is a two part script, test:unit runs d2l-test-runner with Chrome, and test:vdiff runs visual diff testing, comparing rendered output image by image through a .vdiff.json configuration at the repository root, which is the honest way to test a library whose output is a picture of a webpage. The demo directory backs the options documentation with real pages, index, lorem-ipsum, all-tags, blank, css-selectors and a dedicated pagebreaks.html exercising the page-break modes the options section documents. Release cadence has been quiet but present, v0.12.1 in September 2025, v0.13.0 on 2026-01-07 and v0.14.0 on 2026-01-12, with continued repository activity through 2026-09-25. Progress tracking during conversion and the page-break settings round out the documented option surface that the demos make tangible.
Editorial conclusion
Use html2pdf.js when PDF generation must happen in the browser without a server round trip, the document is DOM-renderable, and screenshot fidelity is an acceptable representation of the layout. Choose a headless browser or a server-side typesetting engine when the output must contain selectable text, since the html2canvas path is fundamentally rasterization, or when generation must run in Node.js, which the project explicitly does not support. Before adopting, confirm your package name includes the .js suffix on install, verify the page-break modes and image quality options against your documents using the demo pages, and pin a CDN version rather than floating, since the documented stability argument depends on it.
Frequently asked questions
what is html2pdf js?
html2pdf.js is a MIT-licensed JavaScript library that converts any webpage or element into a printable PDF entirely client-side, using html2canvas to rasterize the DOM and jsPDF to write the document. It runs only in browsers and will not run in Node.js.
how to use html2pdf.js?
Include the bundle via cdnjs, a downloaded script tag, or npm install --save html2pdf.js making sure the package name includes .js. Then pass an element to the global, var element = document.getElementById('element-to-print'); html2pdf(element); which generates the PDF and prompts the user to save it. Advanced use chains Worker methods like from, toPdf and save.
How does html2pdf.js relate to jsPDF?
They are not competitors, jsPDF is one of the two engines html2pdf.js orchestrates. html2canvas rasterizes the HTML into a canvas image, jsPDF turns it into the PDF, and the Worker's outputPdf method passes its type and options directly to the jsPDF object's output method, so jsPDF's output forms remain available underneath.
How do you convert HTML to PDF in JavaScript?
Client-side, html2pdf.js does it by combining html2canvas and jsPDF, taking a DOM element, HTML string, canvas or image as source and walking a pipeline of container, canvas, image, PDF and save steps, all in the browser. A Worker API built as a Promise subclass lets you insert your own functions between steps and track progress.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/ekoopmans-html2pdf-js)