|
1 | 1 | import Worker from '../worker.js';
|
| 2 | +import { objType, createElement } from '../utils.js'; |
2 | 3 |
|
| 4 | +/* Pagebreak plugin: |
| 5 | +
|
| 6 | + Adds page-break functionality to the html2pdf library. Page-breaks can be |
| 7 | + enabled by CSS styles, set on individual elements using selectors, or |
| 8 | + avoided from breaking inside all elements. |
| 9 | +
|
| 10 | + Options on the `opt.pagebreak` object: |
| 11 | +
|
| 12 | + mode: String or array of strings: 'avoid-all', 'css', and/or 'legacy' |
| 13 | + Default: ['css', 'legacy'] |
| 14 | +
|
| 15 | + before: String or array of CSS selectors for which to add page-breaks |
| 16 | + before each element. Can be a specific element with an ID |
| 17 | + ('#myID'), all elements of a type (e.g. 'img'), all of a class |
| 18 | + ('.myClass'), or even '*' to match every element. |
| 19 | +
|
| 20 | + after: Like 'before', but adds a page-break immediately after the element. |
| 21 | +
|
| 22 | + avoid: Like 'before', but avoids page-breaks on these elements. You can |
| 23 | + enable this feature on every element using the 'avoid-all' mode. |
| 24 | +*/ |
| 25 | + |
| 26 | +// Refs to original functions. |
3 | 27 | var orig = {
|
4 | 28 | toContainer: Worker.prototype.toContainer
|
5 | 29 | };
|
6 | 30 |
|
| 31 | +// Add pagebreak default options to the Worker template. |
| 32 | +Worker.template.opt.pagebreak = { |
| 33 | + mode: ['css', 'legacy'], |
| 34 | + before: [], |
| 35 | + after: [], |
| 36 | + avoid: [] |
| 37 | +}; |
| 38 | + |
7 | 39 | Worker.prototype.toContainer = function toContainer() {
|
8 | 40 | return orig.toContainer.call(this).then(function toContainer_pagebreak() {
|
9 |
| - // Enable page-breaks. |
10 |
| - var pageBreaks = this.prop.container.querySelectorAll('.html2pdf__page-break'); |
| 41 | + // Setup root element and inner page height. |
| 42 | + var root = this.prop.container; |
11 | 43 | var pxPageHeight = this.prop.pageSize.inner.px.height;
|
12 |
| - Array.prototype.forEach.call(pageBreaks, function pageBreak_loop(el) { |
13 |
| - el.style.display = 'block'; |
| 44 | + |
| 45 | + // Check all requested modes. |
| 46 | + var modeSrc = [].concat(this.opt.pagebreak.mode); |
| 47 | + var mode = { |
| 48 | + avoidAll: modeSrc.indexOf('avoid-all') !== -1, |
| 49 | + css: modeSrc.indexOf('css') !== -1, |
| 50 | + legacy: modeSrc.indexOf('legacy') !== -1 |
| 51 | + }; |
| 52 | + |
| 53 | + // Get arrays of all explicitly requested elements. |
| 54 | + var select = {}; |
| 55 | + var self = this; |
| 56 | + ['before', 'after', 'avoid'].forEach(function(key) { |
| 57 | + var all = mode.avoidAll && key === 'avoid'; |
| 58 | + select[key] = all ? [] : [].concat(self.opt.pagebreak[key] || []); |
| 59 | + if (select[key].length > 0) { |
| 60 | + select[key] = Array.prototype.slice.call( |
| 61 | + root.querySelectorAll(select[key].join(', '))); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + // Get all legacy page-break elements. |
| 66 | + var legacyEls = root.querySelectorAll('.html2pdf__page-break'); |
| 67 | + legacyEls = Array.prototype.slice.call(legacyEls); |
| 68 | + |
| 69 | + // Loop through all elements. |
| 70 | + var els = root.querySelectorAll('*'); |
| 71 | + Array.prototype.forEach.call(els, function pagebreak_loop(el) { |
| 72 | + // Setup pagebreak rules based on legacy and avoidAll modes. |
| 73 | + var rules = { |
| 74 | + before: false, |
| 75 | + after: mode.legacy && legacyEls.indexOf(el) !== -1, |
| 76 | + avoid: mode.avoidAll |
| 77 | + }; |
| 78 | + |
| 79 | + // Add rules for css mode. |
| 80 | + if (mode.css) { |
| 81 | + // TODO: Check if this is valid with iFrames. |
| 82 | + var style = window.getComputedStyle(el); |
| 83 | + // TODO: Handle 'left' and 'right' correctly. |
| 84 | + // TODO: Add support for 'avoid' on breakBefore/After. |
| 85 | + var breakOpt = ['always', 'page', 'left', 'right']; |
| 86 | + var avoidOpt = ['avoid', 'avoid-page']; |
| 87 | + rules = { |
| 88 | + before: rules.before || breakOpt.indexOf(style.breakBefore || style.pageBreakBefore) !== -1, |
| 89 | + after: rules.after || breakOpt.indexOf(style.breakAfter || style.pageBreakAfter) !== -1, |
| 90 | + avoid: rules.avoid || avoidOpt.indexOf(style.breakInside || style.pageBreakInside) !== -1 |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + // Add rules for explicit requests. |
| 95 | + Object.keys(rules).forEach(function(key) { |
| 96 | + rules[key] = rules[key] || select[key].indexOf(el) !== -1; |
| 97 | + }); |
| 98 | + |
| 99 | + // Get element position on the screen. |
| 100 | + // TODO: Subtract the top of the container from clientRect.top/bottom? |
14 | 101 | var clientRect = el.getBoundingClientRect();
|
15 |
| - el.style.height = pxPageHeight - (clientRect.top % pxPageHeight) + 'px'; |
16 |
| - }, this); |
| 102 | + |
| 103 | + // Avoid: Check if a break happens mid-element. |
| 104 | + if (rules.avoid && !rules.before) { |
| 105 | + var startPage = Math.floor(clientRect.top / pxPageHeight); |
| 106 | + var endPage = Math.floor(clientRect.bottom / pxPageHeight); |
| 107 | + var nPages = Math.abs(clientRect.bottom - clientRect.top) / pxPageHeight; |
| 108 | + |
| 109 | + // Turn on rules.before if the el is broken and is at most one page long. |
| 110 | + if (endPage !== startPage && nPages <= 1) { |
| 111 | + rules.before = true; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Before: Create a padding div to push the element to the next page. |
| 116 | + if (rules.before) { |
| 117 | + var pad = createElement('div', {style: { |
| 118 | + display: 'block', |
| 119 | + height: pxPageHeight - (clientRect.top % pxPageHeight) + 'px' |
| 120 | + }}); |
| 121 | + el.parentNode.insertBefore(pad, el); |
| 122 | + } |
| 123 | + |
| 124 | + // After: Create a padding div to fill the remaining page. |
| 125 | + if (rules.after) { |
| 126 | + var pad = createElement('div', {style: { |
| 127 | + display: 'block', |
| 128 | + height: pxPageHeight - (clientRect.bottom % pxPageHeight) + 'px' |
| 129 | + }}); |
| 130 | + el.parentNode.insertBefore(pad, el.nextSibling); |
| 131 | + } |
| 132 | + }); |
17 | 133 | });
|
18 | 134 | };
|
0 commit comments