|
| 1 | +<!DOCTYPE HTML> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>html2pdf Test - Pagebreaks</title> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + |
| 7 | + <style type="text/css"> |
| 8 | + /* Avoid unexpected sizing on all elements. */ |
| 9 | + * { |
| 10 | + box-sizing: border-box; |
| 11 | + margin: 0; |
| 12 | + padding: 0; |
| 13 | + } |
| 14 | + |
| 15 | + /* CSS styling for before/after/avoid. */ |
| 16 | + .before { |
| 17 | + page-break-before: always; |
| 18 | + } |
| 19 | + .after { |
| 20 | + page-break-after: always; |
| 21 | + } |
| 22 | + .avoid { |
| 23 | + page-break-inside: avoid; |
| 24 | + } |
| 25 | + |
| 26 | + /* Big and bigger elements. */ |
| 27 | + .big { |
| 28 | + height: 10.9in; |
| 29 | + background-color: yellow; |
| 30 | + border: 1px solid black; |
| 31 | + } |
| 32 | + .fullpage { |
| 33 | + height: 11in; |
| 34 | + background-color: fuchsia; |
| 35 | + border: 1px solid black; |
| 36 | + } |
| 37 | + .bigger { |
| 38 | + height: 11.1in; |
| 39 | + background-color: aqua; |
| 40 | + border: 1px solid black; |
| 41 | + } |
| 42 | + </style> |
| 43 | + </head> |
| 44 | + |
| 45 | + <body> |
| 46 | + <!-- Different options. --> |
| 47 | + <select id="mode"> |
| 48 | + <option value="avoid-all">Avoid-all</option> |
| 49 | + <option value="css">CSS</option> |
| 50 | + <option value="legacy">Legacy</option> |
| 51 | + <option value="specify">Specified elements (using before/after/avoid)</option> |
| 52 | + </select> |
| 53 | + |
| 54 | + <!-- Button to generate PDF. --> |
| 55 | + <button onclick="test()">Generate PDF</button> |
| 56 | + |
| 57 | + <!-- Div to capture. --> |
| 58 | + <div id="root"> |
| 59 | + <p>First line</p> |
| 60 | + <p class="before">Break before</p> |
| 61 | + <p class="after">Break after</p> |
| 62 | + <p>No effect (should be top of 3rd page, using css or specify).</p> |
| 63 | + <p class="html2pdf__page-break">Legacy (should create a break after).</p> |
| 64 | + <p>No effect (should be top of 2nd page, using legacy).</p> |
| 65 | + <p class="avoid big">Big element (should start on new page, using avoid-all/css/specify).</p> |
| 66 | + <p>No effect (should start on next page *only* using avoid-all).</p> |
| 67 | + <p>No effect (for spacing).</p> |
| 68 | + <p class="avoid fullpage">Full-page element (should start on new page using avoid-all/css/specify).</p> |
| 69 | + <p>No effect (for spacing).</p> |
| 70 | + <p class="avoid bigger">Even bigger element (should continue normally, because it's more than a page).</p> |
| 71 | + </div> |
| 72 | + |
| 73 | + <!-- Include html2pdf bundle. --> |
| 74 | + <script src="../../dist/html2pdf.bundle.js"></script> |
| 75 | + |
| 76 | + <script> |
| 77 | + // Pagebreak fields: mode, before, after, avoid |
| 78 | + // Pagebreak modes: 'avoid-all', 'css', 'legacy' |
| 79 | + |
| 80 | + function test() { |
| 81 | + // Get the element. |
| 82 | + var element = document.getElementById('root'); |
| 83 | + |
| 84 | + // Choose pagebreak options based on mode. |
| 85 | + var mode = document.getElementById('mode').value; |
| 86 | + var pagebreak = (mode === 'specify') ? |
| 87 | + { mode: '', before: '.before', after: '.after', avoid: '.avoid' } : |
| 88 | + { mode: mode }; |
| 89 | + |
| 90 | + // Generate the PDF. |
| 91 | + html2pdf().from(element).set({ |
| 92 | + filename: mode + '.pdf', |
| 93 | + pagebreak: pagebreak, |
| 94 | + jsPDF: {orientation: 'portrait', unit: 'in', format: 'letter', compressPDF: true} |
| 95 | + }).save(); |
| 96 | + } |
| 97 | + </script> |
| 98 | + </body> |
| 99 | +</html> |
0 commit comments