jspdf-utils 0.1.14 → 0.1.16

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