You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+77-17Lines changed: 77 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,31 @@
2
2
3
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
4
5
+
## Table of contents
6
+
7
+
-[Getting started](#getting-started)
8
+
-[HTML](#html)
9
+
-[NPM](#npm)
10
+
-[Bower](#bower)
11
+
-[Usage](#usage)
12
+
-[Advanced usage](#advanced-usage)
13
+
-[Workflow](#workflow)
14
+
-[Worker API](#worker-api)
15
+
-[Options](#options)
16
+
-[Page-breaks](#page-breaks)
17
+
-[Page-break settings](#page-break-settings)
18
+
-[Page-break modes](#page-break-modes)
19
+
-[Example usage](#example-usage)
20
+
-[Image type and quality](#image-type-and-quality)
21
+
-[Progress tracking](#progress-tracking)
22
+
-[Dependencies](#dependencies)
23
+
-[Contributing](#contributing)
24
+
-[Issues](#issues)
25
+
-[Tests](#tests)
26
+
-[Pull requests](#pull-requests)
27
+
-[Credits](#credits)
28
+
-[License](#license)
29
+
5
30
## Getting started
6
31
7
32
#### HTML
@@ -98,33 +123,64 @@ var opt = {
98
123
};
99
124
100
125
// New Promise-based usage:
101
-
html2pdf().from(element).set(opt).save();
126
+
html2pdf().set(opt).from(element).save();
102
127
103
128
// Old monolithic-style usage:
104
129
html2pdf(element, opt);
105
130
```
106
131
107
132
The `opt` parameter has the following optional fields:
|margin |number or array |0 |PDF margin (in jsPDF units). Can be a single number, `[vMargin, hMargin]`, or `[top, left, bottom, right]`. |
112
-
|filename |string |'file.pdf' |The default filename of the exported PDF. |
113
-
|image |object |{type: 'jpeg', quality: 0.95} |The image type and quality used to generate the PDF. See the Extra Features section below. |
114
-
|enableLinks |boolean |true |If enabled, PDF hyperlinks are automatically added ontop of all anchor tags. |
115
-
|html2canvas |object |{ } |Configuration options sent directly to `html2canvas` ([see here](https://html2canvas.hertzen.com/configuration) for usage).|
116
-
|jsPDF |object |{ } |Configuration options sent directly to `jsPDF` ([see here](http://rawgit.com/MrRio/jsPDF/master/docs/jsPDF.html) for usage).|
|margin |number or array |`0`|PDF margin (in jsPDF units). Can be a single number, `[vMargin, hMargin]`, or `[top, left, bottom, right]`. |
137
+
|filename |string |`'file.pdf'`|The default filename of the exported PDF. |
138
+
|pagebreak |object |`{mode: ['css', 'legacy']}`|Controls the pagebreak behaviour on the page. See [Page-breaks](#page-breaks) below. |
139
+
|image |object |`{type: 'jpeg', quality: 0.95}`|The image type and quality used to generate the PDF. See [Image type and quality](#image-type-and-quality) below.|
140
+
|enableLinks |boolean |`true`|If enabled, PDF hyperlinks are automatically added ontop of all anchor tags. |
141
+
|html2canvas |object |`{ }`|Configuration options sent directly to `html2canvas` ([see here](https://html2canvas.hertzen.com/configuration) for usage).|
142
+
|jsPDF |object |`{ }`|Configuration options sent directly to `jsPDF` ([see here](http://rawgit.com/MrRio/jsPDF/master/docs/jsPDF.html) for usage).|
117
143
118
144
### Page-breaks
119
145
120
-
You may add `html2pdf`-specific page-breaks to your document by adding the CSS class `html2pdf__page-break` to any element (normally an empty `div`). For React elements, use `className=html2pdf__page-break`. During PDF creation, these elements will be given a height calculated to fill the remainder of the PDF page that they are on. Example usage:
146
+
html2pdf has the ability to automatically add page-breaks to clean up your document. Page-breaks can be added by CSS styles, set on individual elements using selectors, or avoided from breaking inside all elements (`avoid-all` mode).
121
147
122
-
```html
123
-
<divid="element-to-print">
124
-
<span>I'm on page 1!</span>
125
-
<divclass="html2pdf__page-break"></div>
126
-
<span>I'm on page 2!</span>
127
-
</div>
148
+
By default, html2pdf will respect most CSS [`break-before`](https://developer.mozilla.org/en-US/docs/Web/CSS/break-before), [`break-after`](https://developer.mozilla.org/en-US/docs/Web/CSS/break-after), and [`break-inside`](https://developer.mozilla.org/en-US/docs/Web/CSS/break-inside) rules, and also add page-breaks after any element with class `html2pdf__page-break` (for legacy purposes).
|mode |string or array |`['css', 'legacy']`|The mode(s) on which to automatically add page-breaks. One or more of `'avoid-all'`, `'css'`, and `'legacy'`. |
155
+
|before |string or array |`[]`|CSS selectors for which to add page-breaks before each element. Can be a specific element with an ID (`'#myID'`), all elements of a type (e.g. `'img'`), all of a class (`'.myClass'`), or even `'*'` to match every element. |
156
+
|after |string or array |`[]`|Like 'before', but adds a page-break immediately after the element. |
157
+
|avoid |string or array |`[]`|Like 'before', but avoids page-breaks on these elements. You can enable this feature on every element using the 'avoid-all' mode. |
158
+
159
+
#### Page-break modes
160
+
161
+
| Mode | Description |
162
+
|-----------|-------------|
163
+
| avoid-all | Automatically adds page-breaks to avoid splitting any elements across pages. |
164
+
| css | Adds page-breaks according to the CSS `break-before`, `break-after`, and `break-inside` properties. Only recognizes `always/left/right` for before/after, and `avoid` for inside. |
165
+
| legacy | Adds page-breaks after elements with class `html2pdf__page-break`. This feature may be removed in the future. |
166
+
167
+
#### Example usage
168
+
169
+
```js
170
+
// Avoid page-breaks on all elements, and add one before #page2el.
@@ -138,7 +194,7 @@ You may customize the image type and quality exported from the canvas by setting
138
194
139
195
These options are limited to the available settings for [HTMLCanvasElement.toDataURL()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL), which ignores quality settings for 'png' images. To enable png image compression, try using the [canvas-png-compression shim](https://github.com/ShyykoSerhiy/canvas-png-compression), which should be an in-place solution to enable png compression via the `quality` option.
140
196
141
-
## Progress
197
+
## Progress tracking
142
198
143
199
The Worker object returned by `html2pdf()` has a built-in progress-tracking mechanism. It will be updated to allow a progress callback that will be called with each update, however it is currently a work-in-progress.
144
200
@@ -161,6 +217,10 @@ If using the unbundled `dist/html2pdf.min.js` (or its un-minified version), you
161
217
162
218
When submitting an issue, please provide reproducible code that highlights the issue, preferably by creating a fork of [this template jsFiddle](https://jsfiddle.net/u6o6ne41/) (which has html2pdf already loaded). Remember that html2pdf uses [html2canvas](https://github.com/niklasvh/html2canvas) and [jsPDF](https://github.com/MrRio/jsPDF) as dependencies, so it's a good idea to check each of those repositories' issue trackers to see if your problem has already been addressed.
163
219
220
+
### Tests
221
+
222
+
html2pdf is currently sorely lacking in unit tests. Any contributions or suggestions of automated (or manual) tests are welcome. This is high on the to-do list for this project.
223
+
164
224
### Pull requests
165
225
166
226
If you want to create a new feature or bugfix, please feel free to fork and submit a pull request! Use the [`develop`](/eKoopmans/html2pdf/tree/develop) branch, which features the latest development, and make changes to `/src/` rather than directly to `/dist/`. You can test your changes by rebuilding with `npm run build`.
0 commit comments