Skip to content

Commit 950c355

Browse files
committed
Add manual tests (template and pagebreaks)
1 parent 9ee2f69 commit 950c355

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

test/manual/pagebreaks.html

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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>

test/manual/template.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>html2pdf Test - Template</title>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
7+
<style type="text/css">
8+
/* Basic styling for root. */
9+
#root {
10+
width: 500px;
11+
height: 700px;
12+
background-color: yellow;
13+
}
14+
</style>
15+
</head>
16+
17+
<body>
18+
<!-- Button to generate PDF. -->
19+
<button onclick="test()">Generate PDF</button>
20+
21+
<!-- Div to capture. -->
22+
<div id="root">
23+
This is a test
24+
</div>
25+
26+
<!-- Include html2pdf bundle. -->
27+
<script src="../../dist/html2pdf.bundle.js"></script>
28+
29+
<script>
30+
function test() {
31+
// Get the element.
32+
var element = document.getElementById('root');
33+
34+
// Generate the PDF.
35+
html2pdf().from(element).set({
36+
margin: 1,
37+
filename: 'test.pdf',
38+
html2canvas: { scale: 2 },
39+
jsPDF: {orientation: 'portrait', unit: 'in', format: 'letter', compressPDF: true}
40+
}).save();
41+
}
42+
</script>
43+
</body>
44+
</html>

0 commit comments

Comments
 (0)