jspdf-utils 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/html-to-pdf.d.ts +1 -5
- package/dist/html-to-pdf.js +138 -155
- package/package.json +1 -1
package/dist/html-to-pdf.d.ts
CHANGED
|
@@ -38,11 +38,7 @@ export interface PrepareResult {
|
|
|
38
38
|
/** Compute derived layout values from options. */
|
|
39
39
|
declare function computeLayout(container: HTMLElement, opts: PageOptions): Layout;
|
|
40
40
|
/**
|
|
41
|
-
* Clone an element
|
|
42
|
-
* (Tailwind, Bootstrap, etc.) cannot interfere with PDF rendering.
|
|
43
|
-
*
|
|
44
|
-
* Only @font-face rules are copied into the iframe so custom fonts still work.
|
|
45
|
-
* The returned element's `.remove()` method cleans up the iframe automatically.
|
|
41
|
+
* Clone an element and position it off-screen at print width for measurement.
|
|
46
42
|
*/
|
|
47
43
|
declare function createPrintClone(source: HTMLElement, pageWidth?: number): HTMLElement;
|
|
48
44
|
/**
|
package/dist/html-to-pdf.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
const
|
|
1
|
+
import j from "html2canvas";
|
|
2
|
+
const T = {
|
|
3
3
|
a0: [841, 1189],
|
|
4
4
|
a1: [594, 841],
|
|
5
5
|
a2: [420, 594],
|
|
@@ -10,7 +10,7 @@ const N = {
|
|
|
10
10
|
letter: [215.9, 279.4],
|
|
11
11
|
legal: [215.9, 355.6],
|
|
12
12
|
tabloid: [279.4, 431.8]
|
|
13
|
-
},
|
|
13
|
+
}, B = {
|
|
14
14
|
a0: 40,
|
|
15
15
|
a1: 35,
|
|
16
16
|
a2: 30,
|
|
@@ -22,150 +22,133 @@ const N = {
|
|
|
22
22
|
legal: 25.4,
|
|
23
23
|
tabloid: 25
|
|
24
24
|
};
|
|
25
|
-
function
|
|
26
|
-
const n =
|
|
27
|
-
top:
|
|
28
|
-
right:
|
|
29
|
-
bottom:
|
|
30
|
-
left:
|
|
25
|
+
function v(i = {}) {
|
|
26
|
+
const n = i.format ?? "a4", [o, e] = T[n], a = i.pageWidth ?? o, t = i.pageHeight ?? e, s = B[n], r = {
|
|
27
|
+
top: s,
|
|
28
|
+
right: s,
|
|
29
|
+
bottom: s,
|
|
30
|
+
left: s
|
|
31
31
|
};
|
|
32
32
|
return {
|
|
33
|
-
unit:
|
|
33
|
+
unit: i.unit ?? "mm",
|
|
34
34
|
format: n,
|
|
35
|
-
pageWidth:
|
|
35
|
+
pageWidth: a,
|
|
36
36
|
pageHeight: t,
|
|
37
|
-
margin: { ...
|
|
37
|
+
margin: { ...r, ...i.margin }
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
|
-
function
|
|
41
|
-
const
|
|
42
|
-
return { renderedWidth:
|
|
40
|
+
function x(i, n) {
|
|
41
|
+
const o = i.offsetWidth, e = n.pageWidth - n.margin.left - n.margin.right, a = e / o, s = (n.pageHeight - n.margin.top - n.margin.bottom) / a;
|
|
42
|
+
return { renderedWidth: o, scale: a, contentWidthMm: e, pageContentPx: s };
|
|
43
43
|
}
|
|
44
|
-
function
|
|
45
|
-
const
|
|
46
|
-
Object.assign(
|
|
44
|
+
function A(i, n = 210) {
|
|
45
|
+
const o = i.cloneNode(!0);
|
|
46
|
+
return Object.assign(o.style, {
|
|
47
47
|
position: "fixed",
|
|
48
48
|
top: "0",
|
|
49
|
-
left: "
|
|
49
|
+
left: "0",
|
|
50
|
+
boxSizing: "border-box",
|
|
50
51
|
width: n + "mm",
|
|
51
|
-
height: "99999px",
|
|
52
|
-
border: "none",
|
|
53
52
|
opacity: "0.000001",
|
|
54
53
|
pointerEvents: "none"
|
|
55
|
-
}), document.body.appendChild(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
const t = [];
|
|
61
|
-
for (const c of document.styleSheets)
|
|
62
|
-
try {
|
|
63
|
-
for (const l of c.cssRules)
|
|
64
|
-
l instanceof CSSFontFaceRule && t.push(l.cssText);
|
|
65
|
-
} catch {
|
|
66
|
-
}
|
|
67
|
-
if (t.length > 0) {
|
|
68
|
-
const c = e.createElement("style");
|
|
69
|
-
c.textContent = t.join(`
|
|
70
|
-
`), e.head.appendChild(c);
|
|
71
|
-
}
|
|
72
|
-
const a = r.cloneNode(!0);
|
|
73
|
-
return Object.assign(a.style, {
|
|
74
|
-
boxSizing: "border-box",
|
|
75
|
-
width: n + "mm"
|
|
76
|
-
}), e.body.style.margin = "0", e.body.appendChild(a), a.remove = () => i.remove(), a;
|
|
54
|
+
}), document.body.appendChild(o), o;
|
|
55
|
+
}
|
|
56
|
+
function C() {
|
|
57
|
+
const i = document.createElement("style");
|
|
58
|
+
return i.setAttribute("data-jspdf-utils", ""), i.textContent = "img { display: inline !important; }", document.head.appendChild(i), () => i.remove();
|
|
77
59
|
}
|
|
78
|
-
function
|
|
79
|
-
for (const n of
|
|
80
|
-
const
|
|
81
|
-
if (
|
|
60
|
+
function W(i) {
|
|
61
|
+
for (const n of i.querySelectorAll("table")) {
|
|
62
|
+
const o = n.getAttribute("cellpadding");
|
|
63
|
+
if (o) {
|
|
82
64
|
for (const e of n.querySelectorAll("th, td"))
|
|
83
|
-
e.style.padding || (e.style.padding =
|
|
65
|
+
e.style.padding || (e.style.padding = o + "px");
|
|
84
66
|
n.removeAttribute("cellpadding");
|
|
85
67
|
}
|
|
86
68
|
}
|
|
87
69
|
}
|
|
88
|
-
function
|
|
89
|
-
for (const
|
|
90
|
-
|
|
70
|
+
function P(i, n) {
|
|
71
|
+
for (const o of Array.from(
|
|
72
|
+
i.querySelectorAll(":scope > table")
|
|
91
73
|
)) {
|
|
92
|
-
if (
|
|
93
|
-
const e = Array.from(
|
|
74
|
+
if (o.offsetHeight <= n) continue;
|
|
75
|
+
const e = Array.from(o.rows);
|
|
94
76
|
if (e.length === 0) continue;
|
|
95
|
-
const
|
|
96
|
-
let
|
|
97
|
-
for (const
|
|
98
|
-
const
|
|
99
|
-
|
|
77
|
+
const a = e[0].querySelector("th") !== null, t = a ? e[0] : null, s = a ? e.slice(1) : e, r = t ? t.offsetHeight : 0, d = n - r - 2, f = [];
|
|
78
|
+
let g = [], l = 0;
|
|
79
|
+
for (const h of s) {
|
|
80
|
+
const c = h.offsetHeight;
|
|
81
|
+
l + c > d && g.length > 0 && (f.push(g), g = [], l = 0), g.push(h), l += c;
|
|
100
82
|
}
|
|
101
|
-
|
|
102
|
-
for (const
|
|
103
|
-
const
|
|
104
|
-
t &&
|
|
105
|
-
for (const m of
|
|
106
|
-
|
|
83
|
+
g.length > 0 && f.push(g);
|
|
84
|
+
for (const h of f) {
|
|
85
|
+
const c = o.cloneNode(!1);
|
|
86
|
+
t && c.appendChild(t.cloneNode(!0));
|
|
87
|
+
for (const m of h) c.appendChild(m.cloneNode(!0));
|
|
88
|
+
o.parentNode.insertBefore(c, o);
|
|
107
89
|
}
|
|
108
|
-
|
|
90
|
+
o.remove();
|
|
109
91
|
}
|
|
110
92
|
}
|
|
111
|
-
function
|
|
112
|
-
const i
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (o.offsetHeight <= n || o.tagName === "TABLE")
|
|
93
|
+
function S(i, n) {
|
|
94
|
+
for (const o of Array.from(i.querySelectorAll(":scope > *"))) {
|
|
95
|
+
const e = o;
|
|
96
|
+
if (e.offsetHeight <= n || e.tagName === "TABLE")
|
|
116
97
|
continue;
|
|
117
|
-
const
|
|
118
|
-
d.setAttribute("style",
|
|
98
|
+
const a = e.tagName, t = e.getAttribute("style") || "", s = getComputedStyle(e).width, r = (e.textContent || "").split(/\s+/).filter(Boolean), d = document.createElement(a);
|
|
99
|
+
d.setAttribute("style", t), Object.assign(d.style, {
|
|
119
100
|
position: "absolute",
|
|
120
101
|
visibility: "hidden",
|
|
121
|
-
width:
|
|
122
|
-
}),
|
|
102
|
+
width: s
|
|
103
|
+
}), i.appendChild(d);
|
|
123
104
|
const f = [];
|
|
124
|
-
let
|
|
125
|
-
for (;
|
|
126
|
-
let
|
|
127
|
-
for (;
|
|
128
|
-
const
|
|
129
|
-
d.textContent =
|
|
105
|
+
let g = 0;
|
|
106
|
+
for (; g < r.length; ) {
|
|
107
|
+
let l = g + 1, h = r.length;
|
|
108
|
+
for (; l < h; ) {
|
|
109
|
+
const m = Math.ceil((l + h) / 2);
|
|
110
|
+
d.textContent = r.slice(g, m).join(" "), d.offsetHeight <= n ? l = m : h = m - 1;
|
|
130
111
|
}
|
|
131
|
-
const
|
|
132
|
-
|
|
112
|
+
const c = document.createElement(a);
|
|
113
|
+
c.setAttribute("style", t), c.textContent = r.slice(g, l).join(" "), f.push(c), g = l;
|
|
133
114
|
}
|
|
134
115
|
d.remove();
|
|
135
|
-
for (const
|
|
136
|
-
|
|
137
|
-
|
|
116
|
+
for (const l of f)
|
|
117
|
+
e.parentNode.insertBefore(l, e);
|
|
118
|
+
e.remove();
|
|
138
119
|
}
|
|
139
120
|
}
|
|
140
|
-
function
|
|
141
|
-
const
|
|
142
|
-
for (const
|
|
143
|
-
const
|
|
144
|
-
if (
|
|
145
|
-
const
|
|
146
|
-
|
|
121
|
+
function M(i, n) {
|
|
122
|
+
const o = Array.from(i.children);
|
|
123
|
+
for (const e of o) {
|
|
124
|
+
const a = e.offsetTop, t = a + e.offsetHeight, s = (Math.floor(a / n) + 1) * n;
|
|
125
|
+
if (t > s && e.offsetHeight <= n) {
|
|
126
|
+
const r = document.createElement("div");
|
|
127
|
+
r.style.height = s - a + 1 + "px", e.parentNode.insertBefore(r, e);
|
|
147
128
|
}
|
|
148
129
|
}
|
|
149
130
|
}
|
|
150
|
-
function
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
return
|
|
155
|
-
clone:
|
|
156
|
-
layout:
|
|
157
|
-
options:
|
|
158
|
-
cleanup: () =>
|
|
131
|
+
function k(i, n = {}) {
|
|
132
|
+
const o = v(n), e = C(), a = A(i, o.pageWidth);
|
|
133
|
+
W(a);
|
|
134
|
+
const t = x(a, o);
|
|
135
|
+
return P(a, t.pageContentPx), S(a, t.pageContentPx), M(a, t.pageContentPx), {
|
|
136
|
+
clone: a,
|
|
137
|
+
layout: t,
|
|
138
|
+
options: o,
|
|
139
|
+
cleanup: () => {
|
|
140
|
+
a.remove(), e();
|
|
141
|
+
}
|
|
159
142
|
};
|
|
160
143
|
}
|
|
161
|
-
async function F(
|
|
162
|
-
const { clone: e, layout:
|
|
144
|
+
async function F(i, n, o = {}) {
|
|
145
|
+
const { clone: e, layout: a, options: t, cleanup: s } = k(n, o);
|
|
163
146
|
try {
|
|
164
|
-
await new Promise((
|
|
165
|
-
|
|
166
|
-
callback: () =>
|
|
167
|
-
width:
|
|
168
|
-
windowWidth:
|
|
147
|
+
await new Promise((r) => {
|
|
148
|
+
i.html(e, {
|
|
149
|
+
callback: () => r(),
|
|
150
|
+
width: a.contentWidthMm,
|
|
151
|
+
windowWidth: a.renderedWidth,
|
|
169
152
|
margin: [
|
|
170
153
|
t.margin.top,
|
|
171
154
|
t.margin.right,
|
|
@@ -175,75 +158,75 @@ async function F(r, n, i = {}) {
|
|
|
175
158
|
});
|
|
176
159
|
});
|
|
177
160
|
} finally {
|
|
178
|
-
|
|
161
|
+
s();
|
|
179
162
|
}
|
|
180
|
-
return
|
|
163
|
+
return i;
|
|
181
164
|
}
|
|
182
|
-
async function
|
|
183
|
-
const { imageFormat:
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
165
|
+
async function O(i, n = {}) {
|
|
166
|
+
const { imageFormat: o = "JPEG", imageQuality: e = 1, scale: a = 2 } = n, t = v(n), s = C(), r = A(i, t.pageWidth);
|
|
167
|
+
r.style.opacity = "1", r.style.left = "-99999px", W(r);
|
|
168
|
+
const d = x(r, t);
|
|
169
|
+
P(r, d.pageContentPx), S(r, d.pageContentPx), M(r, d.pageContentPx);
|
|
187
170
|
try {
|
|
188
|
-
const
|
|
189
|
-
scale:
|
|
171
|
+
const f = await j(r, {
|
|
172
|
+
scale: a,
|
|
190
173
|
backgroundColor: "#ffffff"
|
|
191
|
-
}), { jsPDF:
|
|
192
|
-
orientation:
|
|
174
|
+
}), { jsPDF: g } = await import("jspdf"), l = t.pageWidth - t.margin.left - t.margin.right, h = t.pageHeight - t.margin.top - t.margin.bottom, c = f.width, m = h / l * c, E = Math.ceil(f.height / m), H = t.pageWidth > t.pageHeight ? "l" : "p", w = new g({
|
|
175
|
+
orientation: H,
|
|
193
176
|
unit: "mm",
|
|
194
177
|
format: [t.pageWidth, t.pageHeight]
|
|
195
178
|
});
|
|
196
|
-
for (let
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
),
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
if (!
|
|
204
|
-
|
|
205
|
-
|
|
179
|
+
for (let u = 0; u < E; u++) {
|
|
180
|
+
const p = Math.min(
|
|
181
|
+
m,
|
|
182
|
+
f.height - u * m
|
|
183
|
+
), y = document.createElement("canvas");
|
|
184
|
+
y.width = c, y.height = p;
|
|
185
|
+
const b = y.getContext("2d");
|
|
186
|
+
if (!b) throw new Error("Could not get canvas context");
|
|
187
|
+
b.fillStyle = "#ffffff", b.fillRect(0, 0, c, p), b.drawImage(
|
|
188
|
+
f,
|
|
206
189
|
0,
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
190
|
+
u * m,
|
|
191
|
+
c,
|
|
192
|
+
p,
|
|
210
193
|
0,
|
|
211
194
|
0,
|
|
212
|
-
|
|
213
|
-
|
|
195
|
+
c,
|
|
196
|
+
p
|
|
214
197
|
);
|
|
215
|
-
const
|
|
216
|
-
`image/${
|
|
198
|
+
const N = y.toDataURL(
|
|
199
|
+
`image/${o.toLowerCase()}`,
|
|
217
200
|
e
|
|
218
201
|
);
|
|
219
|
-
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
202
|
+
u > 0 && w.addPage([t.pageWidth, t.pageHeight], H);
|
|
203
|
+
const R = p / c * l;
|
|
204
|
+
w.addImage(
|
|
205
|
+
N,
|
|
206
|
+
o,
|
|
224
207
|
t.margin.left,
|
|
225
208
|
t.margin.top,
|
|
226
|
-
|
|
227
|
-
|
|
209
|
+
l,
|
|
210
|
+
R,
|
|
228
211
|
void 0,
|
|
229
212
|
"FAST"
|
|
230
213
|
);
|
|
231
214
|
}
|
|
232
|
-
return
|
|
215
|
+
return w;
|
|
233
216
|
} finally {
|
|
234
|
-
|
|
217
|
+
r.remove(), s();
|
|
235
218
|
}
|
|
236
219
|
}
|
|
237
220
|
export {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
221
|
+
B as PAGE_MARGINS,
|
|
222
|
+
T as PAGE_SIZES,
|
|
223
|
+
x as computeLayout,
|
|
224
|
+
A as createPrintClone,
|
|
225
|
+
M as insertPageBreakSpacers,
|
|
226
|
+
W as normalizeTableAttributes,
|
|
227
|
+
k as prepare,
|
|
245
228
|
F as renderHTML,
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
229
|
+
O as renderImagePDF,
|
|
230
|
+
P as splitOversizedTables,
|
|
231
|
+
S as splitOversizedText
|
|
249
232
|
};
|
package/package.json
CHANGED