|
| 1 | +# html2pdf |
| 2 | + |
| 3 | +html2pdf converts any webpage or element into a printable PDF entirely client-side using [html2canvas](https://github.com/niklasvh/html2canvas) and [jsPDF](https://github.com/MrRio/jsPDF). |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +1. Copy `html2pdf.js` to your project directory. |
| 8 | +2. Fetch the dependencies `html2canvas` and `jsPDF`, which can be found in the `vendor` folder. |
| 9 | +3. Include the files in your HTML document (**order is important**, otherwise `jsPDF` will override `html2canvas` with its own internal implementation): |
| 10 | + |
| 11 | +```js |
| 12 | +<script src="jspdf.min.js"></script> |
| 13 | +<script src="html2canvas.min.js"></script> |
| 14 | +<script src="html2pdf.js"></script> |
| 15 | +``` |
| 16 | + |
| 17 | +**Note:** For best results, use the custom build of `html2canvas` found in the `vendor` folder or at [this repo](https://github.com/eKoopmans/html2canvas/tree/develop). |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Basic usage |
| 22 | + |
| 23 | +Including html2pdf exposes the `html2pdf` function. Calling it will create a PDF and prompt the user to save the file: |
| 24 | + |
| 25 | +```js |
| 26 | +var element = document.getElementById('element-to-print'); |
| 27 | +html2pdf(element); |
| 28 | +``` |
| 29 | + |
| 30 | +The PDF can be configured using an optional `opt` parameter: |
| 31 | + |
| 32 | +```js |
| 33 | +var element = document.getElementById('element-to-print'); |
| 34 | +html2pdf(element, { |
| 35 | + margin: 1, |
| 36 | + filename: 'myfile.pdf', |
| 37 | + image: { type: 'jpeg', quality: 0.98 }, |
| 38 | + html2canvas: { dpi: 192, letterRendering: true }, |
| 39 | + jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' } |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +The `opt` parameter has the following optional fields: |
| 44 | + |
| 45 | +|Field |Value(s) |Description | |
| 46 | +|------------|----------------|---------------------------------------------------------------------------------------------| |
| 47 | +|margin |number or array |PDF margin (default=1). Array can be either [vMargin, hMargin] or [top, left, bottom, right].| |
| 48 | +|filename |string |The default filename of the exported PDF (default='file.pdf'). | |
| 49 | +|image |object |The image type used to generate the PDF. It must have two fields: 'type', the image type ('jpeg'/'png'); and 'quality', the image quality (0-1). See [here](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL) for more info (do not include 'image/' in the 'type' field).| |
| 50 | +|html2canvas |object |Configuration options sent directly to `html2canvas` ([see here](https://github.com/niklasvh/html2canvas) for usage).| |
| 51 | +|jsPDF |object |Configuration options sent directly to `jsPDF` ([see here](https://github.com/MrRio/jsPDF) for usage. | |
| 52 | + |
| 53 | +## Dependencies |
| 54 | + |
| 55 | +html2pdf depends on the external packages [`html2canvas`](https://github.com/niklasvh/html2canvas) and [`jsPDF`](https://github.com/MrRio/jsPDF). |
| 56 | + |
| 57 | +For best results, use [this custom build](https://github.com/eKoopmans/html2canvas/tree/develop) of `html2canvas`, which adds support for box-shadows and custom resolutions (via the `dpi`/`scale` options). |
| 58 | + |
| 59 | +## Credits |
| 60 | + |
| 61 | +[Erik Koopmans](https://github.com/eKoopmans) |
| 62 | + |
| 63 | +## License |
| 64 | + |
| 65 | +[The MIT License](http://opensource.org/licenses/MIT) |
| 66 | + |
| 67 | +Copyright (c) 2017 Erik Koopmans <[http://www.erik-koopmans.com/](http://www.erik-koopmans.com/)> |
0 commit comments