From b8e1d5994de4cfe7988123d577553d5767484e76 Mon Sep 17 00:00:00 2001 From: "jason.bryan" Date: Wed, 30 May 2018 16:10:44 -0400 Subject: [PATCH] Add support for page-breaks to be inserted before elements that would otherwise intersect them. Any element with class 'html2pdf__avoid-page-break' that intersects a page-break will instead be pushed to the next page. --- src/plugin/pagebreaks.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/plugin/pagebreaks.js b/src/plugin/pagebreaks.js index b6206887..e9228bcc 100644 --- a/src/plugin/pagebreaks.js +++ b/src/plugin/pagebreaks.js @@ -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); }); -}; +}; \ No newline at end of file