Skip to content

Commit 2c36018

Browse files
committed
Update readme for new API
1 parent ecf49a9 commit 2c36018

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,67 @@ var element = document.getElementById('element-to-print');
3333
html2pdf(element);
3434
```
3535

36+
### Advanced usage
37+
38+
Every step of html2pdf is configurable, using its new Promise-based API. If html2pdf is called without arguments, it will return a `Worker` object:
39+
40+
```js
41+
var worker = html2pdf(); // Or: var worker = new html2pdf.Worker;
42+
```
43+
44+
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:
45+
46+
```js
47+
// This will implicitly create the canvas and PDF objects before saving.
48+
var worker = html2pdf().from(element).save();
49+
```
50+
51+
#### Worker API
52+
53+
| Method | Arguments | Description |
54+
|--------------|--------------------|-------------|
55+
| from | src, type | Sets the source (HTML string or element) for the PDF. Optional `type` specifies other sources: `'string'`, `'element'`, `'canvas'`, or `'img'`. |
56+
| to | target | Converts the source to the specified target (`'container'`, `'canvas'`, `'img'`, or `'pdf'`). Each target also has its own `toX` method that can be called directly: `toContainer()`, `toCanvas()`, `toImg()`, and `toPdf()`. |
57+
| output | type, options, src | Routes to the appropriate `outputPdf` or `outputImg` method based on specified `src` (`'pdf'` (default) or `'img'`). |
58+
| outputPdf | type, options | Sends `type` and `options` to the jsPDF object's `output` method, and returns the result as a Promise (use `.then` to access). See the [jsPDF source code](https://rawgit.com/MrRio/jsPDF/master/docs/jspdf.js.html#line992) for more info. |
59+
| outputImg | type, options | Returns the specified data type for the image as a Promise (use `.then` to access). Supported types: `'img'`, `'datauristring/dataurlstring'`, and `'datauri/dataurl'`. |
60+
| save | filename | Saves the PDF object with the optional filename (creates user download prompt). |
61+
| set | opt | Sets the specified properties. See [Options](#options) below for more details. |
62+
| get | key, cbk | Returns the property specified in `key`, either as a Promise (use `.then` to access), or by calling `cbk` if provided. |
63+
| then | onFulfilled, onRejected | Standard Promise method, with `this` re-bound to the Worker, and with added progress-tracking (see [Progress](#progress) below). Note that `.then` returns a `Worker`, which is a subclass of Promise. |
64+
| thenCore | onFulFilled, onRejected | Standard Promise method, with `this` re-bound to the Worker (no progress-tracking). Note that `.thenCore` returns a `Worker`, which is a subclass of Promise. |
65+
| thenExternal | onFulfilled, onRejected | True Promise method. Using this 'exits' the Worker chain - you will not be able to continue chaining Worker methods after `.thenExternal`. |
66+
| catch, catchExternal | onRejected | Standard Promise method. `catchExternal` exits the Worker chain - you will not be able to continue chaining Worker methods after `.catchExternal`. |
67+
| error | msg | Throws an error in the Worker's Promise chain. |
68+
69+
A few aliases are also provided for convenience:
70+
71+
| Method | Alias |
72+
|-----------|-----------|
73+
| save | saveAs |
74+
| set | using |
75+
| output | export |
76+
| then | run |
77+
3678
## Options
3779

3880
html2pdf can be configured using an optional `opt` parameter:
3981

4082
```js
4183
var element = document.getElementById('element-to-print');
42-
html2pdf(element, {
84+
var opt = {
4385
margin: 1,
4486
filename: 'myfile.pdf',
4587
image: { type: 'jpeg', quality: 0.98 },
4688
html2canvas: { dpi: 192, letterRendering: true },
4789
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
4890
});
91+
92+
// New Promise-based usage:
93+
html2pdf().from(element).set(opt).save();
94+
95+
// Old monolithic-style usage:
96+
html2pdf(element, opt);
4997
```
5098

5199
The `opt` parameter has the following optional fields:
@@ -82,6 +130,10 @@ You may customize the image type and quality exported from the canvas by setting
82130

83131
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.
84132

133+
## Progress
134+
135+
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.
136+
85137
## Dependencies
86138

87139
html2pdf depends on the external packages [html2canvas](https://github.com/niklasvh/html2canvas), [jsPDF](https://github.com/MrRio/jsPDF), and [es6-promise](https://github.com/stefanpenner/es6-promise). These dependencies are automatically loaded when using NPM or the bundled package.

0 commit comments

Comments
 (0)