forked from eKoopmans/html2pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagebreaks.js
More file actions
35 lines (32 loc) · 1.56 KB
/
pagebreaks.js
File metadata and controls
35 lines (32 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Worker from '../worker.js';
var orig = {
toContainer: Worker.prototype.toContainer
};
Worker.prototype.toContainer = function toContainer() {
return orig.toContainer.call(this).then(function toContainer_pagebreak() {
// Enable page-breaks.
var pageBreaks = this.prop.container.querySelectorAll('.html2pdf__page-break');
var pxPageHeight = this.prop.pageSize.inner.px.height;
Array.prototype.forEach.call(pageBreaks, function pageBreak_loop(el) {
el.style.display = 'block';
var clientRect = el.getBoundingClientRect();
el.style.height = pxPageHeight - (clientRect.top % pxPageHeight) + 'px';
}, this);
// Shim some padding before elements that would otherwise intersect a page break
pageBreaks = this.prop.container.querySelectorAll('.html2pdf__avoid-page-break');
Array.prototype.forEach.call(pageBreaks, function pageBreak_loop(el) {
var clientRect = el.getBoundingClientRect();
var startPage = Math.floor(clientRect.top / pxPageHeight);
var endPage = Math.floor(clientRect.bottom / pxPageHeight);
if (endPage > startPage && Math.abs(endPage - startPage) === 1) {
// the element is straddling a single page break.
// insert a padding div to push the element to the next page.
var currentDocHeight = (startPage + 1) * pxPageHeight;
var pad = document.createElement('div');
pad.style.display = 'block';
pad.style.height = currentDocHeight - clientRect.top % currentDocHeight + 'px';
el.parentNode.insertBefore(pad, el);
}
}, this);
});
};