Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/plugin/pagebreaks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,46 @@ var orig = {
toContainer: Worker.prototype.toContainer
};

/**
* html2pdf__page-break-before - Page break Before the Element the pagebreak is on.
* html2pdf__page-break-after - Page break After the Element the pagebreak is on.
* html2pdf__page-break - Same behaviour as html2pdf__page-break-after.
* html2pdf__page-break-auto - Automatically determine if the page break is necessary,
* this is usefull on elements like images that you
* do not want to break.
*/
Worker.prototype.toContainer = function toContainer() {
return orig.toContainer.call(this).then(function toContainer_pagebreak() {
// Enable page-breaks.
var pageBreaksBefore = this.prop.container.querySelectorAll('.html2pdf__page-break-before');
var pageBreaksAfter = this.prop.container.querySelectorAll('.html2pdf__page-break-after');
var pageBreaks = this.prop.container.querySelectorAll('.html2pdf__page-break');
var pageBreakAvoid = this.prop.container.querySelectorAll('.html2pdf__page-break-auto');
var pxPageHeight = this.prop.pageSize.inner.px.height;
function toPx(val, k) {
return Math.floor(val * k / 72 * 96);
}
Array.prototype.forEach.call(pageBreakAvoid, function pageBreak_loop(el) {
el.style.display = 'block';
var clientRect = el.getBoundingClientRect();
var margin = toPx(this.opt.margin[0], this.prop.pageSize.k);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since pxPageHeight uses pageSize.inner (the "inner" size), none of the other calculations should need to account for margin.

if ((((clientRect.bottom + margin) / this.prop.container.offsetHeight) * pxPageHeight) > pxPageHeight) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the offsetHeight is for here...

el.style.marginTop = pxPageHeight - (clientRect.top - margin) % pxPageHeight + 'px';
}
}, this);
Array.prototype.forEach.call(pageBreaksBefore, function pageBreak_loop(el) {
el.style.display = 'block';
var clientRect = el.getBoundingClientRect();
el.style.marginTop = pxPageHeight - (clientRect.top - margin) % pxPageHeight + 'px';
}, this);
Array.prototype.forEach.call(pageBreaksAfter, function pageBreak_loop(el) {
el.style.display = 'block';
var clientRect = el.getBoundingClientRect();
el.style.marginBottom = pxPageHeight - (clientRect.top - margin) % pxPageHeight + 'px';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should subtract clientRect.bottom, not top.

}, this);
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';
el.style.marginBottom = pxPageHeight - (clientRect.top - margin) % pxPageHeight + 'px';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, clientRect.bottom.

}, this);
});
};