-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Added more than option for page breaks #138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| if ((((clientRect.bottom + margin) / this.prop.container.offsetHeight) * pxPageHeight) > pxPageHeight) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should subtract |
||
| }, 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'; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise, |
||
| }, this); | ||
| }); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
pxPageHeightusespageSize.inner(the "inner" size), none of the other calculations should need to account formargin.