eKoopmans /html2pdf.js

Client-side HTML-to-PDF rendering using pure JS.

★ 4,937⑂ 1,712👁 62⑃ 352● 682▤ 10
JavaScriptMITCreated 2017-03-29
下载:v0.14.0v0.14.0 ↓0.14.02026-01-12
+0
日增长
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 befo

Top issues

Latest release

v0.14.0 v0.14.0 2026-01-12
0.14.0 (2026-01-12)


 Features

 Sanitize text sources (877) (988826e)

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON