jspdf-utils 0.1.15 → 0.1.17

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.
Files changed (2) hide show
  1. package/dist/html-to-pdf.js +370 -335
  2. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
- import A from "html2canvas";
2
- const U = {
1
+ import P from "html2canvas";
2
+ const _ = {
3
3
  a0: [841, 1189],
4
4
  a1: [594, 841],
5
5
  a2: [420, 594],
@@ -10,7 +10,7 @@ const U = {
10
10
  letter: [215.9, 279.4],
11
11
  legal: [215.9, 355.6],
12
12
  tabloid: [279.4, 431.8]
13
- }, q = {
13
+ }, J = {
14
14
  a0: 40,
15
15
  a1: 35,
16
16
  a2: 30,
@@ -22,188 +22,225 @@ const U = {
22
22
  legal: 25.4,
23
23
  tabloid: 25
24
24
  };
25
- function M(t = {}) {
26
- const e = t.format ?? "a4", [a, n] = U[e], i = t.pageWidth ?? a, o = t.pageHeight ?? n, s = q[e], r = {
25
+ function S(e = {}) {
26
+ const n = e.format ?? "a4", [a, t] = _[n], i = e.pageWidth ?? a, o = e.pageHeight ?? t, s = J[n], r = {
27
27
  top: s,
28
28
  right: s,
29
29
  bottom: s,
30
30
  left: s
31
31
  };
32
32
  return {
33
- unit: t.unit ?? "mm",
34
- format: e,
33
+ unit: e.unit ?? "mm",
34
+ format: n,
35
35
  pageWidth: i,
36
36
  pageHeight: o,
37
- margin: { ...r, ...t.margin }
37
+ margin: { ...r, ...e.margin }
38
38
  };
39
39
  }
40
- function N(t, e) {
41
- const a = t.offsetWidth, n = e.pageWidth - e.margin.left - e.margin.right, i = n / a, s = (e.pageHeight - e.margin.top - e.margin.bottom) / i;
42
- return { renderedWidth: a, scale: i, contentWidthMm: n, pageContentPx: s };
40
+ function R(e, n) {
41
+ const a = e.offsetWidth, t = n.pageWidth - n.margin.left - n.margin.right, i = t / a, s = (n.pageHeight - n.margin.top - n.margin.bottom) / i;
42
+ return { renderedWidth: a, scale: i, contentWidthMm: t, pageContentPx: s };
43
43
  }
44
- function P(t, e = 210) {
45
- const a = t.cloneNode(!0);
44
+ function N(e, n = 210) {
45
+ const a = e.cloneNode(!0);
46
46
  return Object.assign(a.style, {
47
47
  position: "fixed",
48
48
  top: "0",
49
49
  left: "0",
50
50
  boxSizing: "border-box",
51
- width: e + "mm",
51
+ width: n + "mm",
52
52
  opacity: "0.000001",
53
53
  pointerEvents: "none"
54
54
  }), document.body.appendChild(a), a;
55
55
  }
56
- function R() {
57
- const t = document.createElement("style");
58
- return t.setAttribute("data-jspdf-utils", ""), t.textContent = "img { display: inline !important; }", document.head.appendChild(t), () => t.remove();
56
+ function G(e) {
57
+ const n = e.getBoundingClientRect();
58
+ let a = n.bottom;
59
+ for (const i of e.querySelectorAll("*")) {
60
+ const o = i.getBoundingClientRect().bottom;
61
+ o > a && (a = o);
62
+ }
63
+ const t = a - n.top;
64
+ t > e.offsetHeight && (e.style.minHeight = t + "px");
65
+ }
66
+ async function Q(e) {
67
+ const n = Array.from(e.querySelectorAll("img"));
68
+ if (n.length === 0) return;
69
+ await Promise.all(
70
+ n.map((t) => {
71
+ if (!(t.complete && t.naturalWidth > 0))
72
+ return new Promise((i) => {
73
+ t.onload = () => i(), t.onerror = () => i();
74
+ });
75
+ })
76
+ );
77
+ const a = 2;
78
+ for (const t of n) {
79
+ if (!t.naturalWidth || !t.naturalHeight || t.src.startsWith("data:image/svg")) continue;
80
+ const i = t.offsetWidth || t.naturalWidth, o = t.offsetHeight || t.naturalHeight, s = Math.min(i * a, t.naturalWidth), r = Math.min(o * a, t.naturalHeight);
81
+ if (t.naturalWidth <= s && t.naturalHeight <= r && t.src.startsWith("data:"))
82
+ continue;
83
+ const l = document.createElement("canvas");
84
+ l.width = s, l.height = r;
85
+ const h = l.getContext("2d");
86
+ if (h)
87
+ try {
88
+ h.drawImage(t, 0, 0, s, r), t.style.width = i + "px", t.style.height = o + "px", t.src = l.toDataURL("image/png");
89
+ } catch {
90
+ }
91
+ }
92
+ }
93
+ function F() {
94
+ const e = document.createElement("style");
95
+ return e.setAttribute("data-jspdf-utils", ""), e.textContent = "img { display: inline !important; }", document.head.appendChild(e), () => e.remove();
59
96
  }
60
- function F(t) {
61
- for (const e of t.querySelectorAll("table")) {
62
- const a = e.getAttribute("cellpadding");
97
+ function I(e) {
98
+ for (const n of e.querySelectorAll("table")) {
99
+ const a = n.getAttribute("cellpadding");
63
100
  if (a) {
64
- for (const n of e.querySelectorAll("th, td"))
65
- n.style.padding || (n.style.padding = a + "px");
66
- e.removeAttribute("cellpadding");
101
+ for (const t of n.querySelectorAll("th, td"))
102
+ t.style.padding || (t.style.padding = a + "px");
103
+ n.removeAttribute("cellpadding");
67
104
  }
68
105
  }
69
106
  }
70
- function O(t, e) {
107
+ function L(e, n) {
71
108
  for (const a of Array.from(
72
- t.querySelectorAll(":scope > table")
109
+ e.querySelectorAll(":scope > table")
73
110
  )) {
74
- if (a.offsetHeight <= e) continue;
75
- const n = Array.from(a.rows);
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, c = e - r - 2, h = [];
78
- let l = [], f = 0;
111
+ if (a.offsetHeight <= n) continue;
112
+ const t = Array.from(a.rows);
113
+ if (t.length === 0) continue;
114
+ const i = t[0].querySelector("th") !== null, o = i ? t[0] : null, s = i ? t.slice(1) : t, r = o ? o.offsetHeight : 0, l = n - r - 2, h = [];
115
+ let c = [], u = 0;
79
116
  for (const d of s) {
80
117
  const g = d.offsetHeight;
81
- f + g > c && l.length > 0 && (h.push(l), l = [], f = 0), l.push(d), f += g;
118
+ u + g > l && c.length > 0 && (h.push(c), c = [], u = 0), c.push(d), u += g;
82
119
  }
83
- l.length > 0 && h.push(l);
120
+ c.length > 0 && h.push(c);
84
121
  for (const d of h) {
85
122
  const g = a.cloneNode(!1);
86
123
  o && g.appendChild(o.cloneNode(!0));
87
- for (const u of d) g.appendChild(u.cloneNode(!0));
124
+ for (const f of d) g.appendChild(f.cloneNode(!0));
88
125
  a.parentNode.insertBefore(g, a);
89
126
  }
90
127
  a.remove();
91
128
  }
92
129
  }
93
- function I(t, e) {
94
- for (const a of Array.from(t.querySelectorAll(":scope > *"))) {
95
- const n = a;
96
- if (n.offsetHeight <= e || n.tagName === "TABLE")
130
+ function O(e, n) {
131
+ for (const a of Array.from(e.querySelectorAll(":scope > *"))) {
132
+ const t = a;
133
+ if (t.offsetHeight <= n || t.tagName === "TABLE")
97
134
  continue;
98
- const i = n.tagName, o = n.getAttribute("style") || "", s = getComputedStyle(n).width, r = (n.textContent || "").split(/\s+/).filter(Boolean), c = document.createElement(i);
99
- c.setAttribute("style", o), Object.assign(c.style, {
135
+ const i = t.tagName, o = t.getAttribute("style") || "", s = getComputedStyle(t).width, r = (t.textContent || "").split(/\s+/).filter(Boolean), l = document.createElement(i);
136
+ l.setAttribute("style", o), Object.assign(l.style, {
100
137
  position: "absolute",
101
138
  visibility: "hidden",
102
139
  width: s
103
- }), t.appendChild(c);
140
+ }), e.appendChild(l);
104
141
  const h = [];
105
- let l = 0;
106
- for (; l < r.length; ) {
107
- let f = l + 1, d = r.length;
108
- for (; f < d; ) {
109
- const u = Math.ceil((f + d) / 2);
110
- c.textContent = r.slice(l, u).join(" "), c.offsetHeight <= e ? f = u : d = u - 1;
142
+ let c = 0;
143
+ for (; c < r.length; ) {
144
+ let u = c + 1, d = r.length;
145
+ for (; u < d; ) {
146
+ const f = Math.ceil((u + d) / 2);
147
+ l.textContent = r.slice(c, f).join(" "), l.offsetHeight <= n ? u = f : d = f - 1;
111
148
  }
112
149
  const g = document.createElement(i);
113
- g.setAttribute("style", o), g.textContent = r.slice(l, f).join(" "), h.push(g), l = f;
150
+ g.setAttribute("style", o), g.textContent = r.slice(c, u).join(" "), h.push(g), c = u;
114
151
  }
115
- c.remove();
116
- for (const f of h)
117
- n.parentNode.insertBefore(f, n);
118
- n.remove();
152
+ l.remove();
153
+ for (const u of h)
154
+ t.parentNode.insertBefore(u, t);
155
+ t.remove();
119
156
  }
120
157
  }
121
- function _(t, e, a) {
122
- const n = Array.from(t.rows);
123
- if (n.length === 0) return !1;
124
- const i = n[0].querySelector("th") !== null, o = i ? n[0] : null, s = i ? n.slice(1) : n, r = o ? o.offsetHeight : 0;
158
+ function Z(e, n, a) {
159
+ const t = Array.from(e.rows);
160
+ if (t.length === 0) return !1;
161
+ const i = t[0].querySelector("th") !== null, o = i ? t[0] : null, s = i ? t.slice(1) : t, r = o ? o.offsetHeight : 0;
125
162
  if (s.length < 2) return !1;
126
- const c = a - r - 2;
127
- if (c <= 0) return !1;
128
- let h = 0, l = 0;
163
+ const l = a - r - 2;
164
+ if (l <= 0) return !1;
165
+ let h = 0, c = 0;
129
166
  for (const g of s) {
130
- if (l + g.offsetHeight > c) break;
131
- l += g.offsetHeight, h++;
167
+ if (c + g.offsetHeight > l) break;
168
+ c += g.offsetHeight, h++;
132
169
  }
133
170
  if (h === 0 || h === s.length) return !1;
134
- const f = t.cloneNode(!1);
135
- o && f.appendChild(o.cloneNode(!0));
171
+ const u = e.cloneNode(!1);
172
+ o && u.appendChild(o.cloneNode(!0));
136
173
  for (let g = 0; g < h; g++)
137
- f.appendChild(s[g].cloneNode(!0));
138
- const d = t.cloneNode(!1);
174
+ u.appendChild(s[g].cloneNode(!0));
175
+ const d = e.cloneNode(!1);
139
176
  o && d.appendChild(o.cloneNode(!0));
140
177
  for (let g = h; g < s.length; g++)
141
178
  d.appendChild(s[g].cloneNode(!0));
142
- return e.insertBefore(f, t), e.insertBefore(d, t), t.remove(), !0;
179
+ return n.insertBefore(u, e), n.insertBefore(d, e), e.remove(), !0;
143
180
  }
144
- function J(t, e, a) {
145
- if (t.tagName === "TABLE" || t.tagName === "IMG") return !1;
146
- const n = (t.textContent || "").split(/\s+/).filter(Boolean);
147
- if (n.length < 2) return !1;
148
- const i = t.tagName, o = t.getAttribute("style") || "", s = getComputedStyle(t).width, r = document.createElement(i);
181
+ function K(e, n, a) {
182
+ if (e.tagName === "TABLE" || e.tagName === "IMG") return !1;
183
+ const t = (e.textContent || "").split(/\s+/).filter(Boolean);
184
+ if (t.length < 2) return !1;
185
+ const i = e.tagName, o = e.getAttribute("style") || "", s = getComputedStyle(e).width, r = document.createElement(i);
149
186
  if (r.setAttribute("style", o), Object.assign(r.style, {
150
187
  position: "absolute",
151
188
  visibility: "hidden",
152
189
  width: s
153
- }), e.appendChild(r), r.textContent = n[0], r.offsetHeight > a)
190
+ }), n.appendChild(r), r.textContent = t[0], r.offsetHeight > a)
154
191
  return r.remove(), !1;
155
- let c = 1, h = n.length;
156
- for (; c < h; ) {
157
- const d = Math.ceil((c + h) / 2);
158
- r.textContent = n.slice(0, d).join(" "), r.offsetHeight <= a ? c = d : h = d - 1;
192
+ let l = 1, h = t.length;
193
+ for (; l < h; ) {
194
+ const d = Math.ceil((l + h) / 2);
195
+ r.textContent = t.slice(0, d).join(" "), r.offsetHeight <= a ? l = d : h = d - 1;
159
196
  }
160
- if (r.remove(), c >= n.length) return !1;
161
- const l = document.createElement(i);
162
- l.setAttribute("style", o), l.textContent = n.slice(0, c).join(" ");
163
- const f = document.createElement(i);
164
- return f.setAttribute("style", o), f.textContent = n.slice(c).join(" "), e.insertBefore(l, t), e.insertBefore(f, t), t.remove(), !0;
197
+ if (r.remove(), l >= t.length) return !1;
198
+ const c = document.createElement(i);
199
+ c.setAttribute("style", o), c.textContent = t.slice(0, l).join(" ");
200
+ const u = document.createElement(i);
201
+ return u.setAttribute("style", o), u.textContent = t.slice(l).join(" "), n.insertBefore(c, e), n.insertBefore(u, e), e.remove(), !0;
165
202
  }
166
- function L(t, e) {
203
+ function T(e, n) {
167
204
  let a = 0;
168
- for (; a < t.children.length; ) {
169
- const n = t.children[a], i = n.offsetTop, o = i + n.offsetHeight, s = (Math.floor(i / e) + 1) * e;
205
+ for (; a < e.children.length; ) {
206
+ const t = e.children[a], i = t.offsetTop, o = i + t.offsetHeight, s = (Math.floor(i / n) + 1) * n;
170
207
  if (o > s) {
171
208
  const r = s - i;
172
- if (n.tagName === "TABLE") {
173
- if (_(
174
- n,
209
+ if (t.tagName === "TABLE") {
210
+ if (Z(
175
211
  t,
212
+ e,
176
213
  r
177
214
  ))
178
215
  continue;
179
- } else if (J(n, t, r))
216
+ } else if (K(t, e, r))
180
217
  continue;
181
- if (n.offsetHeight <= e) {
182
- const c = document.createElement("div");
183
- c.style.height = s - i + 1 + "px", n.parentNode.insertBefore(c, n), a++;
218
+ if (t.offsetHeight <= n) {
219
+ const l = document.createElement("div");
220
+ l.style.height = s - i + 1 + "px", t.parentNode.insertBefore(l, t), a++;
184
221
  }
185
222
  }
186
223
  a++;
187
224
  }
188
225
  }
189
- function Q(t, e = {}) {
190
- const a = M(e), n = R(), i = P(t, a.pageWidth);
191
- F(i);
192
- const o = N(i, a);
193
- return O(i, o.pageContentPx), I(i, o.pageContentPx), L(i, o.pageContentPx), {
226
+ function V(e, n = {}) {
227
+ const a = S(n), t = F(), i = N(e, a.pageWidth);
228
+ I(i);
229
+ const o = R(i, a);
230
+ return L(i, o.pageContentPx), O(i, o.pageContentPx), T(i, o.pageContentPx), {
194
231
  clone: i,
195
232
  layout: o,
196
233
  options: a,
197
234
  cleanup: () => {
198
- i.remove(), n();
235
+ i.remove(), t();
199
236
  }
200
237
  };
201
238
  }
202
- async function tt(t, e, a = {}) {
203
- const { clone: n, layout: i, options: o, cleanup: s } = Q(e, a);
239
+ async function ot(e, n, a = {}) {
240
+ const { clone: t, layout: i, options: o, cleanup: s } = V(n, a);
204
241
  try {
205
- await new Promise((r) => {
206
- t.html(n, {
242
+ await Q(t), await new Promise((r) => {
243
+ e.html(t, {
207
244
  callback: () => r(),
208
245
  width: i.contentWidthMm,
209
246
  windowWidth: i.renderedWidth,
@@ -218,57 +255,57 @@ async function tt(t, e, a = {}) {
218
255
  } finally {
219
256
  s();
220
257
  }
221
- return a.marginContent && await z(t, a.marginContent, a), t;
258
+ return a.marginContent && await $(e, a.marginContent, a), e;
222
259
  }
223
- function T(t, e) {
224
- switch (t) {
260
+ function j(e, n) {
261
+ switch (e) {
225
262
  case "top":
226
- return { x: 0, y: 0, width: e.pageWidth, height: e.margin.top };
263
+ return { x: 0, y: 0, width: n.pageWidth, height: n.margin.top };
227
264
  case "bottom":
228
265
  return {
229
266
  x: 0,
230
- y: e.pageHeight - e.margin.bottom,
231
- width: e.pageWidth,
232
- height: e.margin.bottom
267
+ y: n.pageHeight - n.margin.bottom,
268
+ width: n.pageWidth,
269
+ height: n.margin.bottom
233
270
  };
234
271
  case "left":
235
- return { x: 0, y: 0, width: e.margin.left, height: e.pageHeight };
272
+ return { x: 0, y: 0, width: n.margin.left, height: n.pageHeight };
236
273
  case "right":
237
274
  return {
238
- x: e.pageWidth - e.margin.right,
275
+ x: n.pageWidth - n.margin.right,
239
276
  y: 0,
240
- width: e.margin.right,
241
- height: e.pageHeight
277
+ width: n.margin.right,
278
+ height: n.pageHeight
242
279
  };
243
280
  }
244
281
  }
245
- async function j(t, e, a, n) {
282
+ async function k(e, n, a, t) {
246
283
  const i = document.createElement("div");
247
284
  Object.assign(i.style, {
248
285
  position: "fixed",
249
286
  left: "-99999px",
250
287
  top: "0",
251
- width: e + "mm",
288
+ width: n + "mm",
252
289
  height: a + "mm",
253
290
  overflow: "hidden"
254
- }), i.appendChild(t), document.body.appendChild(i);
291
+ }), i.appendChild(e), document.body.appendChild(i);
255
292
  try {
256
- return await A(i, {
257
- scale: n,
293
+ return await P(i, {
294
+ scale: t,
258
295
  backgroundColor: null
259
296
  });
260
297
  } finally {
261
298
  i.remove();
262
299
  }
263
300
  }
264
- const S = ["top", "right", "bottom", "left"];
265
- async function G(t, e, a) {
266
- const n = {};
267
- for (const i of S) {
268
- const o = t[i];
301
+ const B = ["top", "right", "bottom", "left"];
302
+ async function q(e, n, a) {
303
+ const t = {};
304
+ for (const i of B) {
305
+ const o = e[i];
269
306
  if (o && typeof o != "function") {
270
- const s = T(i, e);
271
- n[i] = await j(
307
+ const s = j(i, n);
308
+ t[i] = await k(
272
309
  o.cloneNode(!0),
273
310
  s.width,
274
311
  s.height,
@@ -276,295 +313,293 @@ async function G(t, e, a) {
276
313
  );
277
314
  }
278
315
  }
279
- return n;
316
+ return t;
280
317
  }
281
- function Z(t, e) {
282
- return t == null ? e.margin : typeof t == "number" ? { top: t, right: t, bottom: t, left: t } : {
283
- top: t.top ?? e.margin.top,
284
- right: t.right ?? e.margin.right,
285
- bottom: t.bottom ?? e.margin.bottom,
286
- left: t.left ?? e.margin.left
318
+ function Y(e, n) {
319
+ return e == null ? n.margin : typeof e == "number" ? { top: e, right: e, bottom: e, left: e } : {
320
+ top: e.top ?? n.margin.top,
321
+ right: e.right ?? n.margin.right,
322
+ bottom: e.bottom ?? n.margin.bottom,
323
+ left: e.left ?? n.margin.left
287
324
  };
288
325
  }
289
- const K = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF\u0590-\u05FF]/;
290
- async function D(t, e, a, n, i, o, s, r, c = !1) {
326
+ const X = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF\u0590-\u05FF]/;
327
+ async function D(e, n, a, t, i, o, s, r, l = !1) {
291
328
  const h = document.createElement("div");
292
329
  Object.assign(h.style, {
293
330
  position: "fixed",
294
331
  left: "-99999px",
295
332
  top: "0",
296
- width: `${e}px`,
333
+ width: `${n}px`,
297
334
  height: `${a}px`,
298
335
  overflow: "hidden",
299
336
  whiteSpace: "nowrap",
300
- fontSize: `${n}px`,
337
+ fontSize: `${t}px`,
301
338
  fontFamily: i,
302
339
  fontWeight: o,
303
340
  color: s,
304
341
  display: "flex",
305
342
  alignItems: "center",
306
343
  gap: `${r}px`,
307
- direction: c ? "rtl" : "ltr"
344
+ direction: l ? "rtl" : "ltr"
308
345
  });
309
- const l = document.createElement("span");
310
- l.textContent = t, Object.assign(l.style, {
346
+ const c = document.createElement("span");
347
+ c.textContent = e, Object.assign(c.style, {
311
348
  position: "absolute",
312
349
  visibility: "hidden",
313
350
  whiteSpace: "nowrap",
314
- fontSize: `${n}px`,
351
+ fontSize: `${t}px`,
315
352
  fontFamily: i,
316
353
  fontWeight: o
317
- }), document.body.appendChild(l);
318
- const f = l.offsetWidth;
319
- l.remove();
320
- const d = Math.ceil(e / (f + r)) + 2;
354
+ }), document.body.appendChild(c);
355
+ const u = c.offsetWidth;
356
+ c.remove();
357
+ const d = Math.ceil(n / (u + r)) + 2;
321
358
  for (let g = 0; g < d; g++) {
322
- const u = document.createElement("span");
323
- u.textContent = t, u.style.flexShrink = "0", h.appendChild(u);
359
+ const f = document.createElement("span");
360
+ f.textContent = e, f.style.flexShrink = "0", h.appendChild(f);
324
361
  }
325
362
  document.body.appendChild(h);
326
363
  try {
327
- return await A(h, {
328
- scale: 1,
364
+ return await P(h, {
365
+ scale: 3,
329
366
  backgroundColor: null,
330
- width: Math.ceil(e),
367
+ width: Math.ceil(n),
331
368
  height: Math.ceil(a)
332
369
  });
333
370
  } finally {
334
371
  h.remove();
335
372
  }
336
373
  }
337
- async function $(t, e, a, n, i, o, s) {
374
+ async function U(e, n, a, t, i, o, s) {
338
375
  const {
339
376
  text: r,
340
- color: c = "#000000",
377
+ color: l = "#000000",
341
378
  fontSize: h = 2.5,
342
- fontFamily: l = "Arial, sans-serif",
343
- fontWeight: f = "normal"
344
- } = e, d = h * a, g = (e.gap ?? h * 0.5) * a, u = d * 0.5, m = Math.ceil(d * 1.5), b = K.test(r), p = Math.round(o - u * 2), y = Math.round(s - u * 2), [C, v] = await Promise.all([
379
+ fontFamily: c = "Arial, sans-serif",
380
+ fontWeight: u = "normal"
381
+ } = n, d = h * a, g = (n.gap ?? h * 0.5) * a, f = d * 0.5, m = Math.ceil(d * 1.5), y = X.test(r), p = Math.round(o - f * 2), C = Math.round(s - f * 2), [w, b] = await Promise.all([
345
382
  D(
346
383
  r,
347
384
  p,
348
385
  m,
349
386
  d,
350
- l,
351
- f,
352
387
  c,
388
+ u,
389
+ l,
353
390
  g,
354
- b
391
+ y
355
392
  ),
356
393
  D(
357
394
  r,
358
- y,
395
+ C,
359
396
  m,
360
397
  d,
361
- l,
362
- f,
363
398
  c,
399
+ u,
400
+ l,
364
401
  g,
365
- b
402
+ y
366
403
  )
367
- ]), w = Math.round(m / 2);
368
- t.drawImage(
369
- C,
404
+ ]), x = Math.round(m / 2);
405
+ e.drawImage(
406
+ w,
370
407
  0,
371
408
  0,
372
- p,
373
- m,
374
- n + u,
375
- i - w,
409
+ w.width,
410
+ w.height,
411
+ t + f,
412
+ i - x,
376
413
  p,
377
414
  m
378
- ), t.drawImage(
379
- C,
415
+ ), e.drawImage(
416
+ w,
380
417
  0,
381
418
  0,
382
- p,
383
- m,
384
- n + u,
385
- i + s - w,
419
+ w.width,
420
+ w.height,
421
+ t + f,
422
+ i + s - x,
386
423
  p,
387
424
  m
388
- ), t.save(), t.translate(n, i + s - u), t.rotate(-Math.PI / 2), t.drawImage(
389
- v,
425
+ ), e.save(), e.translate(t, i + s - f), e.rotate(-Math.PI / 2), e.drawImage(
426
+ b,
390
427
  0,
391
428
  0,
392
- y,
393
- m,
429
+ b.width,
430
+ b.height,
394
431
  0,
395
- -w,
396
- y,
432
+ -x,
433
+ C,
397
434
  m
398
- ), t.restore(), t.save(), t.translate(n + o, i + u), t.rotate(Math.PI / 2), t.drawImage(
399
- v,
435
+ ), e.restore(), e.save(), e.translate(t + o, i + f), e.rotate(Math.PI / 2), e.drawImage(
436
+ b,
400
437
  0,
401
438
  0,
402
- y,
403
- m,
439
+ b.width,
440
+ b.height,
404
441
  0,
405
- -w,
406
- y,
442
+ -x,
443
+ C,
407
444
  m
408
- ), t.restore();
445
+ ), e.restore();
409
446
  }
410
- function B(t, e) {
411
- return Z(t.margin, e);
447
+ function A(e, n) {
448
+ return Y(e.margin, n);
412
449
  }
413
- async function V(t, e, a, n, i, o, s, r) {
414
- for (const c of S) {
415
- const h = e[c];
450
+ async function tt(e, n, a, t, i, o, s, r) {
451
+ for (const l of B) {
452
+ const h = n[l];
416
453
  if (!h) continue;
417
- const l = T(c, n);
418
- let f;
419
- typeof h == "function" ? f = await j(
454
+ const c = j(l, t);
455
+ let u;
456
+ typeof h == "function" ? u = await k(
420
457
  h(o, s),
421
- l.width,
422
- l.height,
458
+ c.width,
459
+ c.height,
423
460
  r
424
- ) : f = a[c], t.drawImage(
425
- f,
461
+ ) : u = a[l], e.drawImage(
462
+ u,
426
463
  0,
427
464
  0,
428
- f.width,
429
- f.height,
430
- Math.round(l.x * i),
431
- Math.round(l.y * i),
432
- Math.round(l.width * i),
433
- Math.round(l.height * i)
465
+ u.width,
466
+ u.height,
467
+ Math.round(c.x * i),
468
+ Math.round(c.y * i),
469
+ Math.round(c.width * i),
470
+ Math.round(c.height * i)
434
471
  );
435
472
  }
436
- if (e.contentBorder) {
437
- const { color: c = "#000000", width: h = 0.3 } = e.contentBorder, l = B(e.contentBorder, n);
438
- t.strokeStyle = c, t.lineWidth = h * i, t.strokeRect(
439
- Math.round(l.left * i),
440
- Math.round(l.top * i),
441
- Math.round((n.pageWidth - l.left - l.right) * i),
442
- Math.round((n.pageHeight - l.top - l.bottom) * i)
473
+ if (n.contentBorder) {
474
+ const { color: l = "#000000", width: h = 0.3 } = n.contentBorder, c = A(n.contentBorder, t);
475
+ e.strokeStyle = l, e.lineWidth = h * i, e.strokeRect(
476
+ Math.round(c.left * i),
477
+ Math.round(c.top * i),
478
+ Math.round((t.pageWidth - c.left - c.right) * i),
479
+ Math.round((t.pageHeight - c.top - c.bottom) * i)
443
480
  );
444
481
  }
445
- if (e.textBorder) {
446
- const c = B(e.textBorder, n);
447
- await $(
448
- t,
449
- e.textBorder,
482
+ if (n.textBorder) {
483
+ const l = A(n.textBorder, t);
484
+ await U(
485
+ e,
486
+ n.textBorder,
450
487
  i,
451
- Math.round(c.left * i),
452
- Math.round(c.top * i),
453
- Math.round((n.pageWidth - c.left - c.right) * i),
454
- Math.round((n.pageHeight - c.top - c.bottom) * i)
488
+ Math.round(l.left * i),
489
+ Math.round(l.top * i),
490
+ Math.round((t.pageWidth - l.left - l.right) * i),
491
+ Math.round((t.pageHeight - l.top - l.bottom) * i)
455
492
  );
456
493
  }
457
494
  }
458
- async function et(t, e = {}) {
459
- const { imageFormat: a = "JPEG", imageQuality: n = 0.7, scale: i = 2 } = e, o = M(e), s = R(), r = P(t, o.pageWidth);
460
- r.style.opacity = "1", r.style.left = "-99999px", F(r);
461
- const c = N(r, o);
462
- O(r, c.pageContentPx), I(r, c.pageContentPx), L(r, c.pageContentPx);
495
+ async function it(e, n = {}) {
496
+ const { imageFormat: a = "JPEG", imageQuality: t = 0.7, scale: i = 2 } = n, o = S(n), s = F(), r = N(e, o.pageWidth);
497
+ r.style.opacity = "1", r.style.left = "-99999px", I(r);
498
+ const l = R(r, o);
499
+ L(r, l.pageContentPx), O(r, l.pageContentPx), T(r, l.pageContentPx), G(r);
463
500
  try {
464
- const h = await A(r, {
501
+ const h = await P(r, {
465
502
  scale: i,
466
503
  backgroundColor: "#ffffff"
467
- }), { jsPDF: l } = await import("jspdf"), f = o.pageWidth - o.margin.left - o.margin.right, d = o.pageHeight - o.margin.top - o.margin.bottom, g = h.width, u = d / f * g, m = Math.ceil(h.height / u), b = o.pageWidth > o.pageHeight ? "l" : "p", p = new l({
468
- orientation: b,
504
+ }), { jsPDF: c } = await import("jspdf"), u = o.pageWidth - o.margin.left - o.margin.right, d = o.pageHeight - o.margin.top - o.margin.bottom, g = h.width, f = d / u * g, m = Math.ceil(h.height / f), y = o.pageWidth > o.pageHeight ? "l" : "p", p = new c({
505
+ orientation: y,
469
506
  unit: "mm",
470
507
  format: [o.pageWidth, o.pageHeight]
471
- });
472
- for (let y = 0; y < m; y++) {
473
- const C = Math.min(
474
- u,
475
- h.height - y * u
476
- ), v = document.createElement("canvas");
477
- v.width = g, v.height = C;
478
- const w = v.getContext("2d");
479
- if (!w) throw new Error("Could not get canvas context");
480
- w.fillStyle = "#ffffff", w.fillRect(0, 0, g, C), w.drawImage(
508
+ }), C = g / u, w = Math.round(o.pageWidth * C), b = Math.round(o.pageHeight * C), x = Math.round(o.margin.top * C), E = Math.round(o.margin.left * C);
509
+ for (let H = 0; H < m; H++) {
510
+ const M = Math.min(
511
+ f,
512
+ h.height - H * f
513
+ ), W = document.createElement("canvas");
514
+ W.width = w, W.height = b;
515
+ const v = W.getContext("2d");
516
+ if (!v) throw new Error("Could not get canvas context");
517
+ v.fillStyle = "#ffffff", v.fillRect(0, 0, w, b), v.drawImage(
481
518
  h,
482
519
  0,
483
- y * u,
520
+ H * f,
484
521
  g,
485
- C,
486
- 0,
487
- 0,
522
+ M,
523
+ E,
524
+ x,
488
525
  g,
489
- C
526
+ M
490
527
  );
491
- const E = v.toDataURL(
528
+ const z = W.toDataURL(
492
529
  `image/${a.toLowerCase()}`,
493
- n
530
+ t
494
531
  );
495
- y > 0 && p.addPage([o.pageWidth, o.pageHeight], b);
496
- const x = C / g * f;
497
- p.addImage(
498
- E,
532
+ H > 0 && p.addPage([o.pageWidth, o.pageHeight], y), p.addImage(
533
+ z,
499
534
  a,
500
- o.margin.left,
501
- o.margin.top,
502
- f,
503
- x,
535
+ 0,
536
+ 0,
537
+ o.pageWidth,
538
+ o.pageHeight,
504
539
  void 0,
505
540
  "SLOW"
506
541
  );
507
542
  }
508
- return e.marginContent && await z(p, e.marginContent, e), p;
543
+ return n.marginContent && await $(p, n.marginContent, n), p;
509
544
  } finally {
510
545
  r.remove(), s();
511
546
  }
512
547
  }
513
- async function Y(t, e = {}) {
514
- const { imageFormat: a = "PNG", imageQuality: n = 0.75, scale: i = 2 } = e, o = M(e), s = R(), r = P(t, o.pageWidth);
515
- r.style.opacity = "1", r.style.left = "-99999px", F(r);
516
- const c = N(r, o);
517
- O(r, c.pageContentPx), I(r, c.pageContentPx), L(r, c.pageContentPx);
548
+ async function et(e, n = {}) {
549
+ const { imageFormat: a = "PNG", imageQuality: t = 0.75, scale: i = 2 } = n, o = S(n), s = F(), r = N(e, o.pageWidth);
550
+ r.style.opacity = "1", r.style.left = "-99999px", I(r);
551
+ const l = R(r, o);
552
+ L(r, l.pageContentPx), O(r, l.pageContentPx), T(r, l.pageContentPx), G(r);
518
553
  try {
519
- const h = await A(r, {
554
+ const h = await P(r, {
520
555
  scale: i,
521
556
  backgroundColor: "#ffffff"
522
- }), l = o.pageWidth - o.margin.left - o.margin.right, f = o.pageHeight - o.margin.top - o.margin.bottom, d = h.width, g = f / l * d, u = d / l, m = Math.round(o.pageWidth * u), b = Math.round(o.pageHeight * u), p = Math.round(o.margin.top * u), y = Math.round(o.margin.left * u), C = Math.ceil(h.height / g), v = [], { marginContent: w } = e, E = w ? await G(w, o, i) : {};
523
- for (let x = 0; x < C; x++) {
524
- const k = Math.min(
557
+ }), c = o.pageWidth - o.margin.left - o.margin.right, u = o.pageHeight - o.margin.top - o.margin.bottom, d = h.width, g = u / c * d, f = d / c, m = Math.round(o.pageWidth * f), y = Math.round(o.pageHeight * f), p = Math.round(o.margin.top * f), C = Math.round(o.margin.left * f), w = Math.ceil(h.height / g), b = [], { marginContent: x } = n, E = x ? await q(x, o, i) : {};
558
+ for (let H = 0; H < w; H++) {
559
+ const M = Math.min(
525
560
  g,
526
- h.height - x * g
561
+ h.height - H * g
527
562
  ), W = document.createElement("canvas");
528
- W.width = m, W.height = b;
529
- const H = W.getContext("2d");
530
- if (!H) throw new Error("Could not get canvas context");
531
- H.fillStyle = "#ffffff", H.fillRect(0, 0, m, b), H.drawImage(
563
+ W.width = m, W.height = y;
564
+ const v = W.getContext("2d");
565
+ if (!v) throw new Error("Could not get canvas context");
566
+ v.fillStyle = "#ffffff", v.fillRect(0, 0, m, y), v.drawImage(
532
567
  h,
533
568
  0,
534
- x * g,
569
+ H * g,
535
570
  d,
536
- k,
537
- y,
571
+ M,
572
+ C,
538
573
  p,
539
574
  d,
540
- k
541
- ), w && await V(
542
- H,
543
- w,
575
+ M
576
+ ), x && await tt(
577
+ v,
578
+ x,
544
579
  E,
545
580
  o,
546
- u,
547
- x + 1,
548
- C,
581
+ f,
582
+ H + 1,
583
+ w,
549
584
  i
550
- ), v.push(
585
+ ), b.push(
551
586
  W.toDataURL(
552
587
  `image/${a.toLowerCase()}`,
553
- n
588
+ t
554
589
  )
555
590
  );
556
591
  }
557
- return v;
592
+ return b;
558
593
  } finally {
559
594
  r.remove(), s();
560
595
  }
561
596
  }
562
- async function nt(t, e, a = {}) {
563
- const n = M(a), i = await Y(t, a);
564
- e.innerHTML = "", Object.assign(e.style, {
597
+ async function at(e, n, a = {}) {
598
+ const t = S(a), i = await et(e, a);
599
+ n.innerHTML = "", Object.assign(n.style, {
565
600
  direction: "ltr",
566
601
  width: "fit-content",
567
- height: n.pageHeight + "mm",
602
+ height: t.pageHeight + "mm",
568
603
  maxHeight: "100vh",
569
604
  overflowY: "auto",
570
605
  background: "#e0e0e0"
@@ -572,7 +607,7 @@ async function nt(t, e, a = {}) {
572
607
  for (let o = 0; o < i.length; o++) {
573
608
  const s = document.createElement("img");
574
609
  s.src = i[o], s.alt = `Page ${o + 1}`, Object.assign(s.style, {
575
- width: n.pageWidth + "mm",
610
+ width: t.pageWidth + "mm",
576
611
  maxWidth: "100%",
577
612
  height: "auto",
578
613
  boxSizing: "border-box",
@@ -580,45 +615,45 @@ async function nt(t, e, a = {}) {
580
615
  border: "1px solid #bbb",
581
616
  boxShadow: "0 2px 8px rgba(0,0,0,0.2)",
582
617
  marginBottom: "16px"
583
- }), e.appendChild(s);
618
+ }), n.appendChild(s);
584
619
  }
585
620
  }
586
- async function z(t, e, a = {}) {
587
- const n = M(a), i = t.getNumberOfPages(), o = 2, s = o * (96 / 25.4), r = Math.round(n.pageWidth * s), c = Math.round(n.pageHeight * s), h = await G(e, n, o), l = {};
588
- for (const d of S)
589
- h[d] && (l[d] = h[d].toDataURL("image/png"));
590
- let f;
591
- if (e.textBorder) {
621
+ async function $(e, n, a = {}) {
622
+ const t = S(a), i = e.getNumberOfPages(), o = 2, s = o * (96 / 25.4), r = Math.round(t.pageWidth * s), l = Math.round(t.pageHeight * s), h = await q(n, t, o), c = {};
623
+ for (const d of B)
624
+ h[d] && (c[d] = h[d].toDataURL("image/png"));
625
+ let u;
626
+ if (n.textBorder) {
592
627
  const d = document.createElement("canvas");
593
- d.width = r, d.height = c;
628
+ d.width = r, d.height = l;
594
629
  const g = d.getContext("2d");
595
630
  if (g) {
596
- const u = B(e.textBorder, n);
597
- await $(
631
+ const f = A(n.textBorder, t);
632
+ await U(
598
633
  g,
599
- e.textBorder,
634
+ n.textBorder,
600
635
  s,
601
- Math.round(u.left * s),
602
- Math.round(u.top * s),
603
- Math.round((n.pageWidth - u.left - u.right) * s),
604
- Math.round((n.pageHeight - u.top - u.bottom) * s)
605
- ), f = d.toDataURL("image/png");
636
+ Math.round(f.left * s),
637
+ Math.round(f.top * s),
638
+ Math.round((t.pageWidth - f.left - f.right) * s),
639
+ Math.round((t.pageHeight - f.top - f.bottom) * s)
640
+ ), u = d.toDataURL("image/png");
606
641
  }
607
642
  }
608
643
  for (let d = 1; d <= i; d++) {
609
- t.setPage(d);
610
- for (const g of S) {
611
- const u = e[g];
612
- if (!u) continue;
613
- const m = T(g, n);
614
- let b, p;
615
- typeof u == "function" ? b = (await j(
616
- u(d, i),
644
+ e.setPage(d);
645
+ for (const g of B) {
646
+ const f = n[g];
647
+ if (!f) continue;
648
+ const m = j(g, t);
649
+ let y, p;
650
+ typeof f == "function" ? y = (await k(
651
+ f(d, i),
617
652
  m.width,
618
653
  m.height,
619
654
  o
620
- )).toDataURL("image/png") : (b = l[g], p = `margin-${g}`), t.addImage(
621
- b,
655
+ )).toDataURL("image/png") : (y = c[g], p = `margin-${g}`), e.addImage(
656
+ y,
622
657
  "PNG",
623
658
  m.x,
624
659
  m.y,
@@ -628,41 +663,41 @@ async function z(t, e, a = {}) {
628
663
  "SLOW"
629
664
  );
630
665
  }
631
- if (e.contentBorder) {
632
- const { color: g = "#000000", width: u = 0.3 } = e.contentBorder, m = B(e.contentBorder, n);
633
- t.setDrawColor(g), t.setLineWidth(u), t.rect(
666
+ if (n.contentBorder) {
667
+ const { color: g = "#000000", width: f = 0.3 } = n.contentBorder, m = A(n.contentBorder, t);
668
+ e.setDrawColor(g), e.setLineWidth(f), e.rect(
634
669
  m.left,
635
670
  m.top,
636
- n.pageWidth - m.left - m.right,
637
- n.pageHeight - m.top - m.bottom
671
+ t.pageWidth - m.left - m.right,
672
+ t.pageHeight - m.top - m.bottom
638
673
  );
639
674
  }
640
- f && t.addImage(
641
- f,
675
+ u && e.addImage(
676
+ u,
642
677
  "PNG",
643
678
  0,
644
679
  0,
645
- n.pageWidth,
646
- n.pageHeight,
680
+ t.pageWidth,
681
+ t.pageHeight,
647
682
  "text-border",
648
683
  "SLOW"
649
684
  );
650
685
  }
651
- return t;
686
+ return e;
652
687
  }
653
688
  export {
654
- q as PAGE_MARGINS,
655
- U as PAGE_SIZES,
656
- z as addMarginContent,
657
- N as computeLayout,
658
- P as createPrintClone,
659
- L as insertPageBreakSpacers,
660
- F as normalizeTableAttributes,
661
- Q as prepare,
662
- nt as previewPageImages,
663
- tt as renderHTML,
664
- et as renderImagePDF,
665
- Y as renderPageImages,
666
- O as splitOversizedTables,
667
- I as splitOversizedText
689
+ J as PAGE_MARGINS,
690
+ _ as PAGE_SIZES,
691
+ $ as addMarginContent,
692
+ R as computeLayout,
693
+ N as createPrintClone,
694
+ T as insertPageBreakSpacers,
695
+ I as normalizeTableAttributes,
696
+ V as prepare,
697
+ at as previewPageImages,
698
+ ot as renderHTML,
699
+ it as renderImagePDF,
700
+ et as renderPageImages,
701
+ L as splitOversizedTables,
702
+ O as splitOversizedText
668
703
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jspdf-utils",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "Utility helpers for jsPDF's doc.html() renderer with automatic page breaking, table splitting, and RTL support",
5
5
  "type": "module",
6
6
  "main": "dist/html-to-pdf.js",