jspdf-utils 0.1.5 → 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.js +132 -126
- package/package.json +1 -1
package/dist/html-to-pdf.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import j from "html2canvas";
|
|
2
2
|
const T = {
|
|
3
3
|
a0: [841, 1189],
|
|
4
4
|
a1: [594, 841],
|
|
@@ -22,205 +22,211 @@ const T = {
|
|
|
22
22
|
legal: 25.4,
|
|
23
23
|
tabloid: 25
|
|
24
24
|
};
|
|
25
|
-
function
|
|
26
|
-
const
|
|
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
33
|
unit: i.unit ?? "mm",
|
|
34
|
-
format:
|
|
35
|
-
pageWidth:
|
|
36
|
-
pageHeight:
|
|
37
|
-
margin: { ...
|
|
34
|
+
format: n,
|
|
35
|
+
pageWidth: a,
|
|
36
|
+
pageHeight: t,
|
|
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
|
-
return 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
49
|
left: "0",
|
|
50
50
|
boxSizing: "border-box",
|
|
51
|
-
width:
|
|
51
|
+
width: n + "mm",
|
|
52
52
|
opacity: "0.000001",
|
|
53
53
|
pointerEvents: "none"
|
|
54
|
-
}), document.body.appendChild(
|
|
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();
|
|
55
59
|
}
|
|
56
60
|
function W(i) {
|
|
57
|
-
for (const
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
60
|
-
for (const
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
for (const n of i.querySelectorAll("table")) {
|
|
62
|
+
const o = n.getAttribute("cellpadding");
|
|
63
|
+
if (o) {
|
|
64
|
+
for (const e of n.querySelectorAll("th, td"))
|
|
65
|
+
e.style.padding || (e.style.padding = o + "px");
|
|
66
|
+
n.removeAttribute("cellpadding");
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
}
|
|
66
|
-
function
|
|
67
|
-
for (const
|
|
70
|
+
function P(i, n) {
|
|
71
|
+
for (const o of Array.from(
|
|
68
72
|
i.querySelectorAll(":scope > table")
|
|
69
73
|
)) {
|
|
70
|
-
if (
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
73
|
-
const
|
|
74
|
-
let
|
|
75
|
-
for (const
|
|
76
|
-
const
|
|
77
|
-
|
|
74
|
+
if (o.offsetHeight <= n) continue;
|
|
75
|
+
const e = Array.from(o.rows);
|
|
76
|
+
if (e.length === 0) continue;
|
|
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;
|
|
78
82
|
}
|
|
79
|
-
|
|
80
|
-
for (const
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
for (const m of
|
|
84
|
-
|
|
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);
|
|
85
89
|
}
|
|
86
|
-
|
|
90
|
+
o.remove();
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
|
-
function
|
|
90
|
-
for (const
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
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")
|
|
93
97
|
continue;
|
|
94
|
-
const
|
|
95
|
-
|
|
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, {
|
|
96
100
|
position: "absolute",
|
|
97
101
|
visibility: "hidden",
|
|
98
|
-
width:
|
|
99
|
-
}), i.appendChild(
|
|
100
|
-
const
|
|
101
|
-
let
|
|
102
|
-
for (;
|
|
103
|
-
let
|
|
104
|
-
for (;
|
|
105
|
-
const m = Math.ceil((
|
|
106
|
-
|
|
102
|
+
width: s
|
|
103
|
+
}), i.appendChild(d);
|
|
104
|
+
const f = [];
|
|
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;
|
|
107
111
|
}
|
|
108
|
-
const
|
|
109
|
-
|
|
112
|
+
const c = document.createElement(a);
|
|
113
|
+
c.setAttribute("style", t), c.textContent = r.slice(g, l).join(" "), f.push(c), g = l;
|
|
110
114
|
}
|
|
111
|
-
|
|
112
|
-
for (const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
d.remove();
|
|
116
|
+
for (const l of f)
|
|
117
|
+
e.parentNode.insertBefore(l, e);
|
|
118
|
+
e.remove();
|
|
115
119
|
}
|
|
116
120
|
}
|
|
117
|
-
function M(i,
|
|
118
|
-
const
|
|
119
|
-
for (const
|
|
120
|
-
const
|
|
121
|
-
if (
|
|
122
|
-
const
|
|
123
|
-
|
|
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);
|
|
124
128
|
}
|
|
125
129
|
}
|
|
126
130
|
}
|
|
127
|
-
function
|
|
128
|
-
const n =
|
|
129
|
-
W(
|
|
130
|
-
const
|
|
131
|
-
return
|
|
132
|
-
clone:
|
|
133
|
-
layout:
|
|
134
|
-
options:
|
|
135
|
-
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
|
+
}
|
|
136
142
|
};
|
|
137
143
|
}
|
|
138
|
-
async function
|
|
139
|
-
const { clone:
|
|
144
|
+
async function F(i, n, o = {}) {
|
|
145
|
+
const { clone: e, layout: a, options: t, cleanup: s } = k(n, o);
|
|
140
146
|
try {
|
|
141
|
-
await new Promise((
|
|
142
|
-
i.html(
|
|
143
|
-
callback: () =>
|
|
144
|
-
width:
|
|
145
|
-
windowWidth:
|
|
147
|
+
await new Promise((r) => {
|
|
148
|
+
i.html(e, {
|
|
149
|
+
callback: () => r(),
|
|
150
|
+
width: a.contentWidthMm,
|
|
151
|
+
windowWidth: a.renderedWidth,
|
|
146
152
|
margin: [
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
153
|
+
t.margin.top,
|
|
154
|
+
t.margin.right,
|
|
155
|
+
t.margin.bottom,
|
|
156
|
+
t.margin.left
|
|
151
157
|
]
|
|
152
158
|
});
|
|
153
159
|
});
|
|
154
160
|
} finally {
|
|
155
|
-
|
|
161
|
+
s();
|
|
156
162
|
}
|
|
157
163
|
return i;
|
|
158
164
|
}
|
|
159
|
-
async function
|
|
160
|
-
const { imageFormat:
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
|
|
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);
|
|
164
170
|
try {
|
|
165
|
-
const f = await
|
|
166
|
-
scale:
|
|
171
|
+
const f = await j(r, {
|
|
172
|
+
scale: a,
|
|
167
173
|
backgroundColor: "#ffffff"
|
|
168
|
-
}), { jsPDF:
|
|
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({
|
|
169
175
|
orientation: H,
|
|
170
176
|
unit: "mm",
|
|
171
|
-
format: [
|
|
177
|
+
format: [t.pageWidth, t.pageHeight]
|
|
172
178
|
});
|
|
173
|
-
for (let u = 0; u <
|
|
179
|
+
for (let u = 0; u < E; u++) {
|
|
174
180
|
const p = Math.min(
|
|
175
|
-
|
|
176
|
-
f.height - u *
|
|
181
|
+
m,
|
|
182
|
+
f.height - u * m
|
|
177
183
|
), y = document.createElement("canvas");
|
|
178
|
-
y.width =
|
|
184
|
+
y.width = c, y.height = p;
|
|
179
185
|
const b = y.getContext("2d");
|
|
180
186
|
if (!b) throw new Error("Could not get canvas context");
|
|
181
|
-
b.fillStyle = "#ffffff", b.fillRect(0, 0,
|
|
187
|
+
b.fillStyle = "#ffffff", b.fillRect(0, 0, c, p), b.drawImage(
|
|
182
188
|
f,
|
|
183
189
|
0,
|
|
184
|
-
u *
|
|
185
|
-
|
|
190
|
+
u * m,
|
|
191
|
+
c,
|
|
186
192
|
p,
|
|
187
193
|
0,
|
|
188
194
|
0,
|
|
189
|
-
|
|
195
|
+
c,
|
|
190
196
|
p
|
|
191
197
|
);
|
|
192
|
-
const
|
|
193
|
-
`image/${
|
|
194
|
-
|
|
198
|
+
const N = y.toDataURL(
|
|
199
|
+
`image/${o.toLowerCase()}`,
|
|
200
|
+
e
|
|
195
201
|
);
|
|
196
|
-
u > 0 && w.addPage([
|
|
197
|
-
const
|
|
202
|
+
u > 0 && w.addPage([t.pageWidth, t.pageHeight], H);
|
|
203
|
+
const R = p / c * l;
|
|
198
204
|
w.addImage(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
205
|
+
N,
|
|
206
|
+
o,
|
|
207
|
+
t.margin.left,
|
|
208
|
+
t.margin.top,
|
|
203
209
|
l,
|
|
204
|
-
|
|
210
|
+
R,
|
|
205
211
|
void 0,
|
|
206
212
|
"FAST"
|
|
207
213
|
);
|
|
208
214
|
}
|
|
209
215
|
return w;
|
|
210
216
|
} finally {
|
|
211
|
-
|
|
217
|
+
r.remove(), s();
|
|
212
218
|
}
|
|
213
219
|
}
|
|
214
220
|
export {
|
|
215
221
|
B as PAGE_MARGINS,
|
|
216
222
|
T as PAGE_SIZES,
|
|
217
|
-
|
|
218
|
-
|
|
223
|
+
x as computeLayout,
|
|
224
|
+
A as createPrintClone,
|
|
219
225
|
M as insertPageBreakSpacers,
|
|
220
226
|
W as normalizeTableAttributes,
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
227
|
+
k as prepare,
|
|
228
|
+
F as renderHTML,
|
|
229
|
+
O as renderImagePDF,
|
|
230
|
+
P as splitOversizedTables,
|
|
231
|
+
S as splitOversizedText
|
|
226
232
|
};
|
package/package.json
CHANGED