Skip to content

Commit dcfe815

Browse files
committed
Merge remote-tracking branch 'origin/new-api' into new-api
2 parents 335ed6b + 5d4243b commit dcfe815

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ This worker has methods that can be chained sequentially, as each Promise resolv
4848
var worker = html2pdf().from(element).save();
4949
```
5050

51+
#### Workflow
52+
53+
The basic workflow of html2pdf tasks (enforced by the prereq system) is:
54+
55+
```
56+
.from() -> .toContainer() -> .toCanvas() -> .toImg() -> .toPdf() -> .save()
57+
```
58+
5159
#### Worker API
5260

5361
| Method | Arguments | Description |
@@ -56,7 +64,7 @@ var worker = html2pdf().from(element).save();
5664
| 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()`. |
5765
| output | type, options, src | Routes to the appropriate `outputPdf` or `outputImg` method based on specified `src` (`'pdf'` (default) or `'img'`). |
5866
| 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'`. |
67+
| 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'`. |
6068
| save | filename | Saves the PDF object with the optional filename (creates user download prompt). |
6169
| set | opt | Sets the specified properties. See [Options](#options) below for more details. |
6270
| get | key, cbk | Returns the property specified in `key`, either as a Promise (use `.then` to access), or by calling `cbk` if provided. |

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'es6-promise/auto';
22

33
import Worker from './worker.js';
44
import './plugin/jspdf-plugin.js';
5+
import './plugin/pagebreaks.js';
56
import './plugin/hyperlinks.js';
67

78
/**

src/plugin/pagebreaks.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Worker from '../worker.js';
2+
3+
var orig = {
4+
toContainer: Worker.prototype.toContainer
5+
};
6+
7+
Worker.prototype.toContainer = function toContainer() {
8+
return orig.toContainer.call(this).then(function toContainer_pagebreak() {
9+
// Enable page-breaks.
10+
var pageBreaks = source.querySelectorAll('.html2pdf__page-break');
11+
var pxPageHeight = this.prop.pageSize.inner.px.height;
12+
Array.prototype.forEach.call(pageBreaks, function pageBreak_loop(el) {
13+
el.style.display = 'block';
14+
var clientRect = el.getBoundingClientRect();
15+
el.style.height = pxPageHeight - (clientRect.top % pxPageHeight) + 'px';
16+
}, this);
17+
});
18+
};

src/worker.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,6 @@ Worker.prototype.toContainer = function toContainer() {
122122
this.prop.container.appendChild(source);
123123
this.prop.overlay.appendChild(this.prop.container);
124124
document.body.appendChild(this.prop.overlay);
125-
126-
// Enable page-breaks.
127-
var pageBreaks = source.querySelectorAll('.html2pdf__page-break');
128-
var pxPageHeight = this.prop.pageSize.inner.px.height;
129-
Array.prototype.forEach.call(pageBreaks, function pageBreak_loop(el) {
130-
el.style.display = 'block';
131-
var clientRect = el.getBoundingClientRect();
132-
el.style.height = pxPageHeight - (clientRect.top % pxPageHeight) + 'px';
133-
}, this);
134125
});
135126
};
136127

@@ -437,7 +428,7 @@ Worker.prototype.thenCore = function thenCore(onFulfilled, onRejected, thenBase)
437428
if (onRejected) { onRejected = onRejected.bind(self); }
438429

439430
// Cast self into a Promise to avoid polyfills recursively defining `then`.
440-
var selfPromise = (Promise.toString().indexOf('[native code]') === -1) ?
431+
var selfPromise = (Promise.toString() !== 'function Promise() { [native code] }') ?
441432
Worker.convert(Object.assign({}, self), Promise.prototype) : self;
442433

443434
// Return the promise, after casting it into a Worker and preserving props.

0 commit comments

Comments
 (0)