Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for page-breaks to be inserted before elements that would otherwise intersect them #118

Closed
wants to merge 1 commit into from
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
19 changes: 18 additions & 1 deletion src/plugin/pagebreaks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,22 @@ Worker.prototype.toContainer = function toContainer() {
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);
});
};
};