jspdf-utils 0.1.5 → 0.1.6

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