jspdf-utils 0.1.4 → 0.1.5

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.
@@ -38,11 +38,7 @@ export interface PrepareResult {
38
38
  /** Compute derived layout values from options. */
39
39
  declare function computeLayout(container: HTMLElement, opts: PageOptions): Layout;
40
40
  /**
41
- * Clone an element into a hidden iframe so that the page's CSS frameworks
42
- * (Tailwind, Bootstrap, etc.) cannot interfere with PDF rendering.
43
- *
44
- * Only @font-face rules are copied into the iframe so custom fonts still work.
45
- * The returned element's `.remove()` method cleans up the iframe automatically.
41
+ * Clone an element and position it off-screen at print width for measurement.
46
42
  */
47
43
  declare function createPrintClone(source: HTMLElement, pageWidth?: number): HTMLElement;
48
44
  /**
@@ -1,5 +1,5 @@
1
- import D from "html2canvas";
2
- const N = {
1
+ import N from "html2canvas";
2
+ const T = {
3
3
  a0: [841, 1189],
4
4
  a1: [594, 841],
5
5
  a2: [420, 594],
@@ -10,7 +10,7 @@ const N = {
10
10
  letter: [215.9, 279.4],
11
11
  legal: [215.9, 355.6],
12
12
  tabloid: [279.4, 431.8]
13
- }, R = {
13
+ }, B = {
14
14
  a0: 40,
15
15
  a1: 35,
16
16
  a2: 30,
@@ -22,228 +22,205 @@ const N = {
22
22
  legal: 25.4,
23
23
  tabloid: 25
24
24
  };
25
- function x(r = {}) {
26
- const n = r.format ?? "a4", [i, e] = N[n], o = r.pageWidth ?? i, t = r.pageHeight ?? e, a = R[n], c = {
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
27
  top: a,
28
28
  right: a,
29
29
  bottom: a,
30
30
  left: a
31
31
  };
32
32
  return {
33
- unit: r.unit ?? "mm",
34
- format: n,
35
- pageWidth: o,
36
- pageHeight: t,
37
- margin: { ...c, ...r.margin }
33
+ unit: i.unit ?? "mm",
34
+ format: o,
35
+ pageWidth: r,
36
+ pageHeight: e,
37
+ margin: { ...c, ...i.margin }
38
38
  };
39
39
  }
40
- function C(r, n) {
41
- const i = r.offsetWidth, e = n.pageWidth - n.margin.left - n.margin.right, o = e / i, a = (n.pageHeight - n.margin.top - n.margin.bottom) / o;
42
- return { renderedWidth: i, scale: o, contentWidthMm: e, pageContentPx: a };
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 };
43
43
  }
44
- function v(r, n = 210) {
45
- const i = document.createElement("iframe");
46
- Object.assign(i.style, {
44
+ function v(i, o = 210) {
45
+ const n = i.cloneNode(!0);
46
+ return Object.assign(n.style, {
47
47
  position: "fixed",
48
48
  top: "0",
49
- left: "-99999px",
50
- width: n + "mm",
51
- height: "99999px",
52
- border: "none",
49
+ left: "0",
50
+ boxSizing: "border-box",
51
+ width: o + "mm",
53
52
  opacity: "0.000001",
54
53
  pointerEvents: "none"
55
- }), document.body.appendChild(i);
56
- const e = i.contentDocument;
57
- if (!e) throw new Error("Could not access iframe document");
58
- const o = e.createElement("base");
59
- o.href = document.baseURI, e.head.appendChild(o);
60
- const t = [];
61
- for (const c of document.styleSheets)
62
- try {
63
- for (const l of c.cssRules)
64
- l instanceof CSSFontFaceRule && t.push(l.cssText);
65
- } catch {
66
- }
67
- if (t.length > 0) {
68
- const c = e.createElement("style");
69
- c.textContent = t.join(`
70
- `), e.head.appendChild(c);
71
- }
72
- const a = r.cloneNode(!0);
73
- return Object.assign(a.style, {
74
- boxSizing: "border-box",
75
- width: n + "mm"
76
- }), e.body.style.margin = "0", e.body.appendChild(a), a.remove = () => i.remove(), a;
54
+ }), document.body.appendChild(n), n;
77
55
  }
78
- function A(r) {
79
- for (const n of r.querySelectorAll("table")) {
80
- const i = n.getAttribute("cellpadding");
81
- if (i) {
82
- for (const e of n.querySelectorAll("th, td"))
83
- e.style.padding || (e.style.padding = i + "px");
84
- n.removeAttribute("cellpadding");
56
+ 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");
85
63
  }
86
64
  }
87
65
  }
88
- function E(r, n) {
89
- for (const i of Array.from(
90
- r.querySelectorAll(":scope > table")
66
+ function C(i, o) {
67
+ for (const n of Array.from(
68
+ i.querySelectorAll(":scope > table")
91
69
  )) {
92
- if (i.offsetHeight <= n) continue;
93
- const e = Array.from(i.rows);
94
- if (e.length === 0) continue;
95
- const o = e[0].querySelector("th") !== null, t = o ? e[0] : null, a = o ? e.slice(1) : e, c = t ? t.offsetHeight : 0, l = n - c - 2, d = [];
96
- let f = [], h = 0;
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;
97
75
  for (const s of a) {
98
76
  const g = s.offsetHeight;
99
- h + g > l && f.length > 0 && (d.push(f), f = [], h = 0), f.push(s), h += g;
77
+ d + g > f && l.length > 0 && (h.push(l), l = [], d = 0), l.push(s), d += g;
100
78
  }
101
- f.length > 0 && d.push(f);
102
- for (const s of d) {
103
- const g = i.cloneNode(!1);
104
- t && g.appendChild(t.cloneNode(!0));
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));
105
83
  for (const m of s) g.appendChild(m.cloneNode(!0));
106
- i.parentNode.insertBefore(g, i);
84
+ n.parentNode.insertBefore(g, n);
107
85
  }
108
- i.remove();
86
+ n.remove();
109
87
  }
110
88
  }
111
- function P(r, n) {
112
- const i = r.ownerDocument;
113
- for (const e of Array.from(r.querySelectorAll(":scope > *"))) {
114
- const o = e;
115
- if (o.offsetHeight <= n || o.tagName === "TABLE")
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")
116
93
  continue;
117
- const t = o.tagName, a = o.getAttribute("style") || "", c = i.defaultView.getComputedStyle(o).width, l = (o.textContent || "").split(/\s+/).filter(Boolean), d = i.createElement(t);
118
- d.setAttribute("style", a), Object.assign(d.style, {
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, {
119
96
  position: "absolute",
120
97
  visibility: "hidden",
121
- width: c
122
- }), r.appendChild(d);
123
- const f = [];
124
- let h = 0;
125
- for (; h < l.length; ) {
126
- let s = h + 1, g = l.length;
127
- for (; s < g; ) {
128
- const u = Math.ceil((s + g) / 2);
129
- d.textContent = l.slice(h, u).join(" "), d.offsetHeight <= n ? s = u : g = u - 1;
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;
130
107
  }
131
- const m = i.createElement(t);
132
- m.setAttribute("style", a), m.textContent = l.slice(h, s).join(" "), f.push(m), h = s;
108
+ const g = document.createElement(r);
109
+ g.setAttribute("style", e), g.textContent = c.slice(l, d).join(" "), h.push(g), l = d;
133
110
  }
134
- d.remove();
135
- for (const s of f)
136
- o.parentNode.insertBefore(s, o);
137
- o.remove();
111
+ f.remove();
112
+ for (const d of h)
113
+ t.parentNode.insertBefore(d, t);
114
+ t.remove();
138
115
  }
139
116
  }
140
- function W(r, n) {
141
- const i = r.ownerDocument, e = Array.from(r.children);
142
- for (const o of e) {
143
- const t = o.offsetTop, a = t + o.offsetHeight, c = (Math.floor(t / n) + 1) * n;
144
- if (a > c && o.offsetHeight <= n) {
145
- const l = i.createElement("div");
146
- l.style.height = c - t + 1 + "px", o.parentNode.insertBefore(l, o);
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);
147
124
  }
148
125
  }
149
126
  }
150
- function T(r, n = {}) {
151
- const i = x(n), e = v(r, i.pageWidth);
152
- A(e);
153
- const o = C(e, i);
154
- return E(e, o.pageContentPx), P(e, o.pageContentPx), W(e, o.pageContentPx), {
155
- clone: e,
156
- layout: o,
157
- options: i,
158
- cleanup: () => e.remove()
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()
159
136
  };
160
137
  }
161
- async function F(r, n, i = {}) {
162
- const { clone: e, layout: o, options: t, cleanup: a } = T(n, i);
138
+ async function D(i, o, n = {}) {
139
+ const { clone: t, layout: r, options: e, cleanup: a } = j(o, n);
163
140
  try {
164
141
  await new Promise((c) => {
165
- r.html(e, {
142
+ i.html(t, {
166
143
  callback: () => c(),
167
- width: o.contentWidthMm,
168
- windowWidth: o.renderedWidth,
144
+ width: r.contentWidthMm,
145
+ windowWidth: r.renderedWidth,
169
146
  margin: [
170
- t.margin.top,
171
- t.margin.right,
172
- t.margin.bottom,
173
- t.margin.left
147
+ e.margin.top,
148
+ e.margin.right,
149
+ e.margin.bottom,
150
+ e.margin.left
174
151
  ]
175
152
  });
176
153
  });
177
154
  } finally {
178
155
  a();
179
156
  }
180
- return r;
157
+ return i;
181
158
  }
182
- async function B(r, n = {}) {
183
- const { imageFormat: i = "JPEG", imageQuality: e = 1, scale: o = 2 } = n, t = x(n), a = v(r, t.pageWidth);
184
- A(a);
185
- const c = C(a, t);
186
- E(a, c.pageContentPx), P(a, c.pageContentPx), W(a, c.pageContentPx);
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);
187
164
  try {
188
- const l = await D(a, {
189
- scale: o,
165
+ const f = await N(a, {
166
+ scale: r,
190
167
  backgroundColor: "#ffffff"
191
- }), { jsPDF: d } = await import("jspdf"), f = t.pageWidth - t.margin.left - t.margin.right, h = t.pageHeight - t.margin.top - t.margin.bottom, s = l.width, g = h / f * s, m = Math.ceil(l.height / g), u = t.pageWidth > t.pageHeight ? "l" : "p", H = new d({
192
- orientation: u,
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({
169
+ orientation: H,
193
170
  unit: "mm",
194
- format: [t.pageWidth, t.pageHeight]
171
+ format: [e.pageWidth, e.pageHeight]
195
172
  });
196
- for (let p = 0; p < m; p++) {
197
- const y = Math.min(
173
+ for (let u = 0; u < m; u++) {
174
+ const p = Math.min(
198
175
  g,
199
- l.height - p * g
200
- ), b = document.createElement("canvas");
201
- b.width = s, b.height = y;
202
- const w = b.getContext("2d");
203
- if (!w) throw new Error("Could not get canvas context");
204
- w.fillStyle = "#ffffff", w.fillRect(0, 0, s, y), w.drawImage(
205
- l,
176
+ f.height - u * g
177
+ ), y = document.createElement("canvas");
178
+ y.width = s, y.height = p;
179
+ const b = y.getContext("2d");
180
+ if (!b) throw new Error("Could not get canvas context");
181
+ b.fillStyle = "#ffffff", b.fillRect(0, 0, s, p), b.drawImage(
182
+ f,
206
183
  0,
207
- p * g,
184
+ u * g,
208
185
  s,
209
- y,
186
+ p,
210
187
  0,
211
188
  0,
212
189
  s,
213
- y
190
+ p
214
191
  );
215
- const S = b.toDataURL(
216
- `image/${i.toLowerCase()}`,
217
- e
192
+ const E = y.toDataURL(
193
+ `image/${n.toLowerCase()}`,
194
+ t
218
195
  );
219
- p > 0 && H.addPage([t.pageWidth, t.pageHeight], u);
220
- const M = y / s * f;
221
- H.addImage(
196
+ u > 0 && w.addPage([e.pageWidth, e.pageHeight], H);
197
+ const S = p / s * l;
198
+ w.addImage(
199
+ E,
200
+ n,
201
+ e.margin.left,
202
+ e.margin.top,
203
+ l,
222
204
  S,
223
- i,
224
- t.margin.left,
225
- t.margin.top,
226
- f,
227
- M,
228
205
  void 0,
229
206
  "FAST"
230
207
  );
231
208
  }
232
- return H;
209
+ return w;
233
210
  } finally {
234
211
  a.remove();
235
212
  }
236
213
  }
237
214
  export {
238
- R as PAGE_MARGINS,
239
- N as PAGE_SIZES,
240
- C as computeLayout,
215
+ B as PAGE_MARGINS,
216
+ T as PAGE_SIZES,
217
+ A as computeLayout,
241
218
  v as createPrintClone,
242
- W as insertPageBreakSpacers,
243
- A as normalizeTableAttributes,
244
- T as prepare,
245
- F as renderHTML,
246
- B as renderImagePDF,
247
- E as splitOversizedTables,
219
+ M as insertPageBreakSpacers,
220
+ W as normalizeTableAttributes,
221
+ j as prepare,
222
+ D as renderHTML,
223
+ F as renderImagePDF,
224
+ C as splitOversizedTables,
248
225
  P as splitOversizedText
249
226
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jspdf-utils",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
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",