jspdf-utils 0.1.9 → 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.
Files changed (2) hide show
  1. package/dist/html-to-pdf.js +129 -119
  2. package/package.json +1 -1
@@ -22,7 +22,7 @@ const q = {
22
22
  legal: 25.4,
23
23
  tabloid: 25
24
24
  };
25
- function M(t = {}) {
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, l = e - r - 2, d = [];
78
- let c = [], g = 0;
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
- g + h > l && c.length > 0 && (d.push(c), c = [], g = 0), c.push(m), g += h;
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), l = document.createElement(i);
99
- l.setAttribute("style", o), Object.assign(l.style, {
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(l);
103
+ }), t.appendChild(g);
104
104
  const d = [];
105
105
  let c = 0;
106
106
  for (; c < r.length; ) {
107
- let g = c + 1, m = r.length;
108
- for (; g < m; ) {
109
- const f = Math.ceil((g + m) / 2);
110
- l.textContent = r.slice(c, f).join(" "), l.offsetHeight <= e ? g = f : m = f - 1;
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, g).join(" "), d.push(h), c = g;
113
+ h.setAttribute("style", o), h.textContent = r.slice(c, l).join(" "), d.push(h), c = l;
114
114
  }
115
- l.remove();
116
- for (const g of d)
117
- n.parentNode.insertBefore(g, n);
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 l = a - r - 2;
127
- if (l <= 0) return !1;
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 > l) break;
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 g = t.cloneNode(!1);
135
- o && g.appendChild(o.cloneNode(!0));
134
+ const l = t.cloneNode(!1);
135
+ o && l.appendChild(o.cloneNode(!0));
136
136
  for (let h = 0; h < d; h++)
137
- g.appendChild(s[h].cloneNode(!0));
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(g, t), e.insertBefore(m, t), t.remove(), !0;
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 l = 1, d = n.length;
156
- for (; l < d; ) {
157
- const m = Math.ceil((l + d) / 2);
158
- r.textContent = n.slice(0, m).join(" "), r.offsetHeight <= a ? l = m : d = m - 1;
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(), l >= n.length) return !1;
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, l).join(" ");
163
- const g = document.createElement(i);
164
- return g.setAttribute("style", o), g.textContent = n.slice(l).join(" "), e.insertBefore(c, t), e.insertBefore(g, t), t.remove(), !0;
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 l = document.createElement("div");
183
- l.style.height = s - i + 1 + "px", n.parentNode.insertBefore(l, n), a++;
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 = M(e), n = N(), i = B(t, a.pageWidth);
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 D = ["top", "right", "bottom", "left"];
265
- async function F(t, e, a) {
264
+ const G = ["top", "right", "bottom", "left"];
265
+ async function D(t, e, a) {
266
266
  const n = {};
267
- for (const i of D) {
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: l = "#000000",
292
+ color: g = "#000000",
293
293
  fontSize: d = 2.5,
294
294
  fontFamily: c = "Arial, sans-serif"
295
- } = e, g = d * a, m = (e.gap ?? d * 0.5) * a;
296
- t.save(), t.fillStyle = l, t.font = `${g}px ${c}`, t.textBaseline = "middle";
297
- const h = t.measureText(r).width + m;
298
- t.save(), t.beginPath(), t.rect(n, i - g, o, g * 2), t.clip();
299
- for (let f = n; f < n + o; f += h)
300
- t.fillText(r, f, i);
301
- t.restore(), t.save(), t.beginPath(), t.rect(n, i + s - g, o, g * 2), t.clip();
302
- for (let f = n; f < n + o; f += h)
303
- t.fillText(r, f, i + s);
304
- t.restore(), t.save(), t.translate(n, i + s), t.rotate(-Math.PI / 2), t.beginPath(), t.rect(0, -g, s, g * 2), t.clip();
305
- for (let f = 0; f < s; f += h)
306
- t.fillText(r, f, 0);
307
- t.restore(), t.save(), t.translate(n + o, i), t.rotate(Math.PI / 2), t.beginPath(), t.rect(0, -g, s, g * 2), t.clip();
308
- for (let f = 0; f < s; f += h)
309
- t.fillText(r, f, 0);
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 G(t, e, a, n, i, o, s, r) {
316
- for (const l of D) {
317
- const d = e[l];
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(l, n);
320
- let g;
321
- typeof d == "function" ? g = await L(
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
- ) : g = a[l], t.drawImage(
327
- g,
336
+ ) : l = a[g], t.drawImage(
337
+ l,
328
338
  0,
329
339
  0,
330
- g.width,
331
- g.height,
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: l = "#000000", width: d = 0.3 } = e.contentBorder, c = I(e.contentBorder, n);
340
- t.strokeStyle = l, t.lineWidth = d * i, t.strokeRect(
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,124 +355,124 @@ async function G(t, e, a, n, i, o, s, r) {
345
355
  );
346
356
  }
347
357
  if (e.textBorder) {
348
- const l = I(e.textBorder, n);
358
+ const g = I(e.textBorder, n);
349
359
  Z(
350
360
  t,
351
361
  e.textBorder,
352
362
  i,
353
- Math.round(l.left * i),
354
- Math.round(l.top * i),
355
- Math.round((n.pageWidth - l.left - l.right) * i),
356
- Math.round((n.pageHeight - l.top - l.bottom) * i)
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 = M(e), s = N(), r = B(t, o.pageWidth);
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 l = x(r, o);
364
- T(r, l.pageContentPx), R(r, l.pageContentPx), j(r, l.pageContentPx);
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"), g = o.pageWidth - o.margin.left - o.margin.right, m = o.pageHeight - o.margin.top - o.margin.bottom, h = d.width, f = m / g * h, P = Math.ceil(d.height / f), v = o.pageWidth > o.pageHeight ? "l" : "p", C = new c({
370
- orientation: v,
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 y = 0; y < P; y++) {
375
- const p = Math.min(
384
+ for (let w = 0; w < u; w++) {
385
+ const y = Math.min(
376
386
  f,
377
- d.height - y * f
378
- ), w = document.createElement("canvas");
379
- w.width = h, w.height = p;
380
- const u = w.getContext("2d");
381
- if (!u) throw new Error("Could not get canvas context");
382
- u.fillStyle = "#ffffff", u.fillRect(0, 0, h, p), u.drawImage(
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
- y * f,
395
+ w * f,
386
396
  h,
387
- p,
397
+ y,
388
398
  0,
389
399
  0,
390
400
  h,
391
- p
401
+ y
392
402
  );
393
- const A = w.toDataURL(
403
+ const A = b.toDataURL(
394
404
  `image/${a.toLowerCase()}`,
395
405
  n
396
406
  );
397
- y > 0 && C.addPage([o.pageWidth, o.pageHeight], v);
398
- const b = p / h * g;
399
- C.addImage(
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
- g,
405
- b,
414
+ l,
415
+ C,
406
416
  void 0,
407
417
  "FAST"
408
418
  );
409
419
  }
410
- return e.marginContent && await z(C, e.marginContent, e), C;
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 = M(e), s = N(), r = B(t, o.pageWidth);
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 l = x(r, o);
419
- T(r, l.pageContentPx), R(r, l.pageContentPx), j(r, l.pageContentPx);
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, g = o.pageHeight - o.margin.top - o.margin.bottom, m = d.width, h = g / c * m, f = m / c, P = Math.round(o.pageWidth * f), v = Math.round(o.pageHeight * f), C = Math.round(o.margin.top * f), y = Math.round(o.margin.left * f), p = Math.ceil(d.height / h), w = [], { marginContent: u } = e, A = u ? await F(u, o, i) : {};
425
- for (let b = 0; b < p; b++) {
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 - b * h
438
+ d.height - C * h
429
439
  ), S = document.createElement("canvas");
430
- S.width = P, S.height = v;
431
- const H = S.getContext("2d");
432
- if (!H) throw new Error("Could not get canvas context");
433
- H.fillStyle = "#ffffff", H.fillRect(0, 0, P, v), H.drawImage(
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
- b * h,
446
+ C * h,
437
447
  m,
438
448
  O,
439
- y,
440
- C,
449
+ w,
450
+ v,
441
451
  m,
442
452
  O
443
- ), u && await G(
444
- H,
445
- u,
453
+ ), p && await F(
454
+ M,
455
+ p,
446
456
  A,
447
457
  o,
448
458
  f,
449
- b + 1,
450
- p,
459
+ C + 1,
460
+ y,
451
461
  i
452
- ), w.push(
462
+ ), b.push(
453
463
  S.toDataURL(
454
464
  `image/${a.toLowerCase()}`,
455
465
  n
456
466
  )
457
467
  );
458
468
  }
459
- return w;
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 = M(a), i = await K(t, a);
475
+ const n = P(a), i = await K(t, a);
466
476
  e.innerHTML = "", Object.assign(e.style, {
467
477
  direction: "ltr",
468
478
  width: "fit-content",
@@ -486,13 +496,13 @@ async function tt(t, e, a = {}) {
486
496
  }
487
497
  }
488
498
  async function z(t, e, a = {}) {
489
- const n = M(a), i = t.getNumberOfPages(), o = 2, s = o * (96 / 25.4), r = Math.round(n.pageWidth * s), l = Math.round(n.pageHeight * s), d = await F(e, n, o);
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);
490
500
  for (let c = 1; c <= i; c++) {
491
501
  t.setPage(c);
492
- const g = document.createElement("canvas");
493
- g.width = r, g.height = l;
494
- const m = g.getContext("2d");
495
- m && (await G(
502
+ const l = document.createElement("canvas");
503
+ l.width = r, l.height = g;
504
+ const m = l.getContext("2d");
505
+ m && (await F(
496
506
  m,
497
507
  e,
498
508
  d,
@@ -502,7 +512,7 @@ async function z(t, e, a = {}) {
502
512
  i,
503
513
  o
504
514
  ), t.addImage(
505
- g.toDataURL("image/png"),
515
+ l.toDataURL("image/png"),
506
516
  "PNG",
507
517
  0,
508
518
  0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jspdf-utils",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
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",