jspdf-utils 0.1.8 → 0.1.10
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 +130 -119
- package/package.json +1 -1
package/dist/html-to-pdf.js
CHANGED
|
@@ -22,7 +22,7 @@ const q = {
|
|
|
22
22
|
legal: 25.4,
|
|
23
23
|
tabloid: 25
|
|
24
24
|
};
|
|
25
|
-
function
|
|
25
|
+
function P(t = {}) {
|
|
26
26
|
const e = t.format ?? "a4", [a, n] = q[e], i = t.pageWidth ?? a, o = t.pageHeight ?? n, s = $[e], r = {
|
|
27
27
|
top: s,
|
|
28
28
|
right: s,
|
|
@@ -74,11 +74,11 @@ function T(t, e) {
|
|
|
74
74
|
if (a.offsetHeight <= e) continue;
|
|
75
75
|
const n = Array.from(a.rows);
|
|
76
76
|
if (n.length === 0) continue;
|
|
77
|
-
const i = n[0].querySelector("th") !== null, o = i ? n[0] : null, s = i ? n.slice(1) : n, r = o ? o.offsetHeight : 0,
|
|
78
|
-
let c = [],
|
|
77
|
+
const i = n[0].querySelector("th") !== null, o = i ? n[0] : null, s = i ? n.slice(1) : n, r = o ? o.offsetHeight : 0, g = e - r - 2, d = [];
|
|
78
|
+
let c = [], l = 0;
|
|
79
79
|
for (const m of s) {
|
|
80
80
|
const h = m.offsetHeight;
|
|
81
|
-
|
|
81
|
+
l + h > g && c.length > 0 && (d.push(c), c = [], l = 0), c.push(m), l += h;
|
|
82
82
|
}
|
|
83
83
|
c.length > 0 && d.push(c);
|
|
84
84
|
for (const m of d) {
|
|
@@ -95,26 +95,26 @@ function R(t, e) {
|
|
|
95
95
|
const n = a;
|
|
96
96
|
if (n.offsetHeight <= e || n.tagName === "TABLE")
|
|
97
97
|
continue;
|
|
98
|
-
const i = n.tagName, o = n.getAttribute("style") || "", s = getComputedStyle(n).width, r = (n.textContent || "").split(/\s+/).filter(Boolean),
|
|
99
|
-
|
|
98
|
+
const i = n.tagName, o = n.getAttribute("style") || "", s = getComputedStyle(n).width, r = (n.textContent || "").split(/\s+/).filter(Boolean), g = document.createElement(i);
|
|
99
|
+
g.setAttribute("style", o), Object.assign(g.style, {
|
|
100
100
|
position: "absolute",
|
|
101
101
|
visibility: "hidden",
|
|
102
102
|
width: s
|
|
103
|
-
}), t.appendChild(
|
|
103
|
+
}), t.appendChild(g);
|
|
104
104
|
const d = [];
|
|
105
105
|
let c = 0;
|
|
106
106
|
for (; c < r.length; ) {
|
|
107
|
-
let
|
|
108
|
-
for (;
|
|
109
|
-
const f = Math.ceil((
|
|
110
|
-
|
|
107
|
+
let l = c + 1, m = r.length;
|
|
108
|
+
for (; l < m; ) {
|
|
109
|
+
const f = Math.ceil((l + m) / 2);
|
|
110
|
+
g.textContent = r.slice(c, f).join(" "), g.offsetHeight <= e ? l = f : m = f - 1;
|
|
111
111
|
}
|
|
112
112
|
const h = document.createElement(i);
|
|
113
|
-
h.setAttribute("style", o), h.textContent = r.slice(c,
|
|
113
|
+
h.setAttribute("style", o), h.textContent = r.slice(c, l).join(" "), d.push(h), c = l;
|
|
114
114
|
}
|
|
115
|
-
|
|
116
|
-
for (const
|
|
117
|
-
n.parentNode.insertBefore(
|
|
115
|
+
g.remove();
|
|
116
|
+
for (const l of d)
|
|
117
|
+
n.parentNode.insertBefore(l, n);
|
|
118
118
|
n.remove();
|
|
119
119
|
}
|
|
120
120
|
}
|
|
@@ -123,23 +123,23 @@ function U(t, e, a) {
|
|
|
123
123
|
if (n.length === 0) return !1;
|
|
124
124
|
const i = n[0].querySelector("th") !== null, o = i ? n[0] : null, s = i ? n.slice(1) : n, r = o ? o.offsetHeight : 0;
|
|
125
125
|
if (s.length < 2) return !1;
|
|
126
|
-
const
|
|
127
|
-
if (
|
|
126
|
+
const g = a - r - 2;
|
|
127
|
+
if (g <= 0) return !1;
|
|
128
128
|
let d = 0, c = 0;
|
|
129
129
|
for (const h of s) {
|
|
130
|
-
if (c + h.offsetHeight >
|
|
130
|
+
if (c + h.offsetHeight > g) break;
|
|
131
131
|
c += h.offsetHeight, d++;
|
|
132
132
|
}
|
|
133
133
|
if (d === 0 || d === s.length) return !1;
|
|
134
|
-
const
|
|
135
|
-
o &&
|
|
134
|
+
const l = t.cloneNode(!1);
|
|
135
|
+
o && l.appendChild(o.cloneNode(!0));
|
|
136
136
|
for (let h = 0; h < d; h++)
|
|
137
|
-
|
|
137
|
+
l.appendChild(s[h].cloneNode(!0));
|
|
138
138
|
const m = t.cloneNode(!1);
|
|
139
139
|
o && m.appendChild(o.cloneNode(!0));
|
|
140
140
|
for (let h = d; h < s.length; h++)
|
|
141
141
|
m.appendChild(s[h].cloneNode(!0));
|
|
142
|
-
return e.insertBefore(
|
|
142
|
+
return e.insertBefore(l, t), e.insertBefore(m, t), t.remove(), !0;
|
|
143
143
|
}
|
|
144
144
|
function _(t, e, a) {
|
|
145
145
|
if (t.tagName === "TABLE" || t.tagName === "IMG") return !1;
|
|
@@ -152,16 +152,16 @@ function _(t, e, a) {
|
|
|
152
152
|
width: s
|
|
153
153
|
}), e.appendChild(r), r.textContent = n[0], r.offsetHeight > a)
|
|
154
154
|
return r.remove(), !1;
|
|
155
|
-
let
|
|
156
|
-
for (;
|
|
157
|
-
const m = Math.ceil((
|
|
158
|
-
r.textContent = n.slice(0, m).join(" "), r.offsetHeight <= a ?
|
|
155
|
+
let g = 1, d = n.length;
|
|
156
|
+
for (; g < d; ) {
|
|
157
|
+
const m = Math.ceil((g + d) / 2);
|
|
158
|
+
r.textContent = n.slice(0, m).join(" "), r.offsetHeight <= a ? g = m : d = m - 1;
|
|
159
159
|
}
|
|
160
|
-
if (r.remove(),
|
|
160
|
+
if (r.remove(), g >= n.length) return !1;
|
|
161
161
|
const c = document.createElement(i);
|
|
162
|
-
c.setAttribute("style", o), c.textContent = n.slice(0,
|
|
163
|
-
const
|
|
164
|
-
return
|
|
162
|
+
c.setAttribute("style", o), c.textContent = n.slice(0, g).join(" ");
|
|
163
|
+
const l = document.createElement(i);
|
|
164
|
+
return l.setAttribute("style", o), l.textContent = n.slice(g).join(" "), e.insertBefore(c, t), e.insertBefore(l, t), t.remove(), !0;
|
|
165
165
|
}
|
|
166
166
|
function j(t, e) {
|
|
167
167
|
let a = 0;
|
|
@@ -179,15 +179,15 @@ function j(t, e) {
|
|
|
179
179
|
} else if (_(n, t, r))
|
|
180
180
|
continue;
|
|
181
181
|
if (n.offsetHeight <= e) {
|
|
182
|
-
const
|
|
183
|
-
|
|
182
|
+
const g = document.createElement("div");
|
|
183
|
+
g.style.height = s - i + 1 + "px", n.parentNode.insertBefore(g, n), a++;
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
a++;
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
189
|
function J(t, e = {}) {
|
|
190
|
-
const a =
|
|
190
|
+
const a = P(e), n = N(), i = B(t, a.pageWidth);
|
|
191
191
|
E(i);
|
|
192
192
|
const o = x(i, a);
|
|
193
193
|
return T(i, o.pageContentPx), R(i, o.pageContentPx), j(i, o.pageContentPx), {
|
|
@@ -261,10 +261,10 @@ async function L(t, e, a, n) {
|
|
|
261
261
|
i.remove();
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
|
-
const
|
|
265
|
-
async function
|
|
264
|
+
const G = ["top", "right", "bottom", "left"];
|
|
265
|
+
async function D(t, e, a) {
|
|
266
266
|
const n = {};
|
|
267
|
-
for (const i of
|
|
267
|
+
for (const i of G) {
|
|
268
268
|
const o = t[i];
|
|
269
269
|
if (o && typeof o != "function") {
|
|
270
270
|
const s = k(i, e);
|
|
@@ -289,46 +289,56 @@ function Q(t, e) {
|
|
|
289
289
|
function Z(t, e, a, n, i, o, s) {
|
|
290
290
|
const {
|
|
291
291
|
text: r,
|
|
292
|
-
color:
|
|
292
|
+
color: g = "#000000",
|
|
293
293
|
fontSize: d = 2.5,
|
|
294
294
|
fontFamily: c = "Arial, sans-serif"
|
|
295
|
-
} = e,
|
|
296
|
-
t.save(), t.fillStyle =
|
|
297
|
-
const h = t.measureText(r).width + m;
|
|
298
|
-
t.save(), t.beginPath(), t.rect(
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
295
|
+
} = e, l = d * a, m = (e.gap ?? d * 0.5) * a;
|
|
296
|
+
t.save(), t.fillStyle = g, t.font = `${l}px ${c}`, t.textBaseline = "middle";
|
|
297
|
+
const h = t.measureText(r).width + m, f = l * 0.5;
|
|
298
|
+
t.save(), t.beginPath(), t.rect(
|
|
299
|
+
n + f,
|
|
300
|
+
i - l,
|
|
301
|
+
o - f * 2,
|
|
302
|
+
l * 2
|
|
303
|
+
), t.clip();
|
|
304
|
+
for (let u = n + f; u < n + o - f; u += h)
|
|
305
|
+
t.fillText(r, u, i);
|
|
306
|
+
t.restore(), t.save(), t.beginPath(), t.rect(
|
|
307
|
+
n + f,
|
|
308
|
+
i + s - l,
|
|
309
|
+
o - f * 2,
|
|
310
|
+
l * 2
|
|
311
|
+
), t.clip();
|
|
312
|
+
for (let u = n + f; u < n + o - f; u += h)
|
|
313
|
+
t.fillText(r, u, i + s);
|
|
314
|
+
t.restore(), t.save(), t.translate(n, i + s), t.rotate(-Math.PI / 2), t.beginPath(), t.rect(f, -l, s - f * 2, l * 2), t.clip();
|
|
315
|
+
for (let u = f; u < s - f; u += h)
|
|
316
|
+
t.fillText(r, u, 0);
|
|
317
|
+
t.restore(), t.save(), t.translate(n + o, i), t.rotate(Math.PI / 2), t.beginPath(), t.rect(f, -l, s - f * 2, l * 2), t.clip();
|
|
318
|
+
for (let u = f; u < s - f; u += h)
|
|
319
|
+
t.fillText(r, u, 0);
|
|
310
320
|
t.restore(), t.restore();
|
|
311
321
|
}
|
|
312
322
|
function I(t, e) {
|
|
313
323
|
return Q(t.margin, e);
|
|
314
324
|
}
|
|
315
|
-
async function
|
|
316
|
-
for (const
|
|
317
|
-
const d = e[
|
|
325
|
+
async function F(t, e, a, n, i, o, s, r) {
|
|
326
|
+
for (const g of G) {
|
|
327
|
+
const d = e[g];
|
|
318
328
|
if (!d) continue;
|
|
319
|
-
const c = k(
|
|
320
|
-
let
|
|
321
|
-
typeof d == "function" ?
|
|
329
|
+
const c = k(g, n);
|
|
330
|
+
let l;
|
|
331
|
+
typeof d == "function" ? l = await L(
|
|
322
332
|
d(o, s),
|
|
323
333
|
c.width,
|
|
324
334
|
c.height,
|
|
325
335
|
r
|
|
326
|
-
) :
|
|
327
|
-
|
|
336
|
+
) : l = a[g], t.drawImage(
|
|
337
|
+
l,
|
|
328
338
|
0,
|
|
329
339
|
0,
|
|
330
|
-
|
|
331
|
-
|
|
340
|
+
l.width,
|
|
341
|
+
l.height,
|
|
332
342
|
Math.round(c.x * i),
|
|
333
343
|
Math.round(c.y * i),
|
|
334
344
|
Math.round(c.width * i),
|
|
@@ -336,8 +346,8 @@ async function G(t, e, a, n, i, o, s, r) {
|
|
|
336
346
|
);
|
|
337
347
|
}
|
|
338
348
|
if (e.contentBorder) {
|
|
339
|
-
const { color:
|
|
340
|
-
t.strokeStyle =
|
|
349
|
+
const { color: g = "#000000", width: d = 0.3 } = e.contentBorder, c = I(e.contentBorder, n);
|
|
350
|
+
t.strokeStyle = g, t.lineWidth = d * i, t.strokeRect(
|
|
341
351
|
Math.round(c.left * i),
|
|
342
352
|
Math.round(c.top * i),
|
|
343
353
|
Math.round((n.pageWidth - c.left - c.right) * i),
|
|
@@ -345,125 +355,126 @@ async function G(t, e, a, n, i, o, s, r) {
|
|
|
345
355
|
);
|
|
346
356
|
}
|
|
347
357
|
if (e.textBorder) {
|
|
348
|
-
const
|
|
358
|
+
const g = I(e.textBorder, n);
|
|
349
359
|
Z(
|
|
350
360
|
t,
|
|
351
361
|
e.textBorder,
|
|
352
362
|
i,
|
|
353
|
-
Math.round(
|
|
354
|
-
Math.round(
|
|
355
|
-
Math.round((n.pageWidth -
|
|
356
|
-
Math.round((n.pageHeight -
|
|
363
|
+
Math.round(g.left * i),
|
|
364
|
+
Math.round(g.top * i),
|
|
365
|
+
Math.round((n.pageWidth - g.left - g.right) * i),
|
|
366
|
+
Math.round((n.pageHeight - g.top - g.bottom) * i)
|
|
357
367
|
);
|
|
358
368
|
}
|
|
359
369
|
}
|
|
360
370
|
async function X(t, e = {}) {
|
|
361
|
-
const { imageFormat: a = "JPEG", imageQuality: n = 1, scale: i = 2 } = e, o =
|
|
371
|
+
const { imageFormat: a = "JPEG", imageQuality: n = 1, scale: i = 2 } = e, o = P(e), s = N(), r = B(t, o.pageWidth);
|
|
362
372
|
r.style.opacity = "1", r.style.left = "-99999px", E(r);
|
|
363
|
-
const
|
|
364
|
-
T(r,
|
|
373
|
+
const g = x(r, o);
|
|
374
|
+
T(r, g.pageContentPx), R(r, g.pageContentPx), j(r, g.pageContentPx);
|
|
365
375
|
try {
|
|
366
376
|
const d = await W(r, {
|
|
367
377
|
scale: i,
|
|
368
378
|
backgroundColor: "#ffffff"
|
|
369
|
-
}), { jsPDF: c } = await import("jspdf"),
|
|
370
|
-
orientation:
|
|
379
|
+
}), { jsPDF: c } = await import("jspdf"), l = o.pageWidth - o.margin.left - o.margin.right, m = o.pageHeight - o.margin.top - o.margin.bottom, h = d.width, f = m / l * h, u = Math.ceil(d.height / f), H = o.pageWidth > o.pageHeight ? "l" : "p", v = new c({
|
|
380
|
+
orientation: H,
|
|
371
381
|
unit: "mm",
|
|
372
382
|
format: [o.pageWidth, o.pageHeight]
|
|
373
383
|
});
|
|
374
|
-
for (let
|
|
375
|
-
const
|
|
384
|
+
for (let w = 0; w < u; w++) {
|
|
385
|
+
const y = Math.min(
|
|
376
386
|
f,
|
|
377
|
-
d.height -
|
|
378
|
-
),
|
|
379
|
-
|
|
380
|
-
const
|
|
381
|
-
if (!
|
|
382
|
-
|
|
387
|
+
d.height - w * f
|
|
388
|
+
), b = document.createElement("canvas");
|
|
389
|
+
b.width = h, b.height = y;
|
|
390
|
+
const p = b.getContext("2d");
|
|
391
|
+
if (!p) throw new Error("Could not get canvas context");
|
|
392
|
+
p.fillStyle = "#ffffff", p.fillRect(0, 0, h, y), p.drawImage(
|
|
383
393
|
d,
|
|
384
394
|
0,
|
|
385
|
-
|
|
395
|
+
w * f,
|
|
386
396
|
h,
|
|
387
|
-
|
|
397
|
+
y,
|
|
388
398
|
0,
|
|
389
399
|
0,
|
|
390
400
|
h,
|
|
391
|
-
|
|
401
|
+
y
|
|
392
402
|
);
|
|
393
|
-
const A =
|
|
403
|
+
const A = b.toDataURL(
|
|
394
404
|
`image/${a.toLowerCase()}`,
|
|
395
405
|
n
|
|
396
406
|
);
|
|
397
|
-
|
|
398
|
-
const
|
|
399
|
-
|
|
407
|
+
w > 0 && v.addPage([o.pageWidth, o.pageHeight], H);
|
|
408
|
+
const C = y / h * l;
|
|
409
|
+
v.addImage(
|
|
400
410
|
A,
|
|
401
411
|
a,
|
|
402
412
|
o.margin.left,
|
|
403
413
|
o.margin.top,
|
|
404
|
-
|
|
405
|
-
|
|
414
|
+
l,
|
|
415
|
+
C,
|
|
406
416
|
void 0,
|
|
407
417
|
"FAST"
|
|
408
418
|
);
|
|
409
419
|
}
|
|
410
|
-
return e.marginContent && await z(
|
|
420
|
+
return e.marginContent && await z(v, e.marginContent, e), v;
|
|
411
421
|
} finally {
|
|
412
422
|
r.remove(), s();
|
|
413
423
|
}
|
|
414
424
|
}
|
|
415
425
|
async function K(t, e = {}) {
|
|
416
|
-
const { imageFormat: a = "PNG", imageQuality: n = 1, scale: i = 2 } = e, o =
|
|
426
|
+
const { imageFormat: a = "PNG", imageQuality: n = 1, scale: i = 2 } = e, o = P(e), s = N(), r = B(t, o.pageWidth);
|
|
417
427
|
r.style.opacity = "1", r.style.left = "-99999px", E(r);
|
|
418
|
-
const
|
|
419
|
-
T(r,
|
|
428
|
+
const g = x(r, o);
|
|
429
|
+
T(r, g.pageContentPx), R(r, g.pageContentPx), j(r, g.pageContentPx);
|
|
420
430
|
try {
|
|
421
431
|
const d = await W(r, {
|
|
422
432
|
scale: i,
|
|
423
433
|
backgroundColor: "#ffffff"
|
|
424
|
-
}), c = o.pageWidth - o.margin.left - o.margin.right,
|
|
425
|
-
for (let
|
|
434
|
+
}), c = o.pageWidth - o.margin.left - o.margin.right, l = o.pageHeight - o.margin.top - o.margin.bottom, m = d.width, h = l / c * m, f = m / c, u = Math.round(o.pageWidth * f), H = Math.round(o.pageHeight * f), v = Math.round(o.margin.top * f), w = Math.round(o.margin.left * f), y = Math.ceil(d.height / h), b = [], { marginContent: p } = e, A = p ? await D(p, o, i) : {};
|
|
435
|
+
for (let C = 0; C < y; C++) {
|
|
426
436
|
const O = Math.min(
|
|
427
437
|
h,
|
|
428
|
-
d.height -
|
|
438
|
+
d.height - C * h
|
|
429
439
|
), S = document.createElement("canvas");
|
|
430
|
-
S.width =
|
|
431
|
-
const
|
|
432
|
-
if (!
|
|
433
|
-
|
|
440
|
+
S.width = u, S.height = H;
|
|
441
|
+
const M = S.getContext("2d");
|
|
442
|
+
if (!M) throw new Error("Could not get canvas context");
|
|
443
|
+
M.fillStyle = "#ffffff", M.fillRect(0, 0, u, H), M.drawImage(
|
|
434
444
|
d,
|
|
435
445
|
0,
|
|
436
|
-
|
|
446
|
+
C * h,
|
|
437
447
|
m,
|
|
438
448
|
O,
|
|
439
|
-
|
|
440
|
-
|
|
449
|
+
w,
|
|
450
|
+
v,
|
|
441
451
|
m,
|
|
442
452
|
O
|
|
443
|
-
),
|
|
444
|
-
|
|
445
|
-
|
|
453
|
+
), p && await F(
|
|
454
|
+
M,
|
|
455
|
+
p,
|
|
446
456
|
A,
|
|
447
457
|
o,
|
|
448
458
|
f,
|
|
449
|
-
|
|
450
|
-
|
|
459
|
+
C + 1,
|
|
460
|
+
y,
|
|
451
461
|
i
|
|
452
|
-
),
|
|
462
|
+
), b.push(
|
|
453
463
|
S.toDataURL(
|
|
454
464
|
`image/${a.toLowerCase()}`,
|
|
455
465
|
n
|
|
456
466
|
)
|
|
457
467
|
);
|
|
458
468
|
}
|
|
459
|
-
return
|
|
469
|
+
return b;
|
|
460
470
|
} finally {
|
|
461
471
|
r.remove(), s();
|
|
462
472
|
}
|
|
463
473
|
}
|
|
464
474
|
async function tt(t, e, a = {}) {
|
|
465
|
-
const n =
|
|
475
|
+
const n = P(a), i = await K(t, a);
|
|
466
476
|
e.innerHTML = "", Object.assign(e.style, {
|
|
477
|
+
direction: "ltr",
|
|
467
478
|
width: "fit-content",
|
|
468
479
|
height: n.pageHeight + "mm",
|
|
469
480
|
maxHeight: "100vh",
|
|
@@ -485,13 +496,13 @@ async function tt(t, e, a = {}) {
|
|
|
485
496
|
}
|
|
486
497
|
}
|
|
487
498
|
async function z(t, e, a = {}) {
|
|
488
|
-
const n =
|
|
499
|
+
const n = P(a), i = t.getNumberOfPages(), o = 2, s = o * (96 / 25.4), r = Math.round(n.pageWidth * s), g = Math.round(n.pageHeight * s), d = await D(e, n, o);
|
|
489
500
|
for (let c = 1; c <= i; c++) {
|
|
490
501
|
t.setPage(c);
|
|
491
|
-
const
|
|
492
|
-
|
|
493
|
-
const m =
|
|
494
|
-
m && (await
|
|
502
|
+
const l = document.createElement("canvas");
|
|
503
|
+
l.width = r, l.height = g;
|
|
504
|
+
const m = l.getContext("2d");
|
|
505
|
+
m && (await F(
|
|
495
506
|
m,
|
|
496
507
|
e,
|
|
497
508
|
d,
|
|
@@ -501,7 +512,7 @@ async function z(t, e, a = {}) {
|
|
|
501
512
|
i,
|
|
502
513
|
o
|
|
503
514
|
), t.addImage(
|
|
504
|
-
|
|
515
|
+
l.toDataURL("image/png"),
|
|
505
516
|
"PNG",
|
|
506
517
|
0,
|
|
507
518
|
0,
|
package/package.json
CHANGED