UNPKG

vue-data-ui

Version:

A user-empowering data visualization Vue 3 components library for eloquent data storytelling

80 lines (79 loc) 2.73 kB
import { d as g } from "./dom-to-png-WXGPkYro.js"; async function E({ domElement: a, fileName: D, scale: r = 2, orientation: S = "auto", // 'auto' | 'portrait' | 'landscape' overflowTolerance: F = 0.2, // up to +n% height overflow gets squeezed onto 1 page aspectRatio: O = null // example: '1/14141' }) { if (!a) return Promise.reject("No domElement provided"); const W = typeof navigator < "u" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent); let T; try { T = (await import("jspdf")).default; } catch { return Promise.reject("jspdf is not installed. Run npm install jspdf"); } const b = { width: 595.28, height: 841.89 }, j = { width: 841.89, height: 595.28 }, I = 1e3; function v(i) { if (i == null) return null; if (typeof i == "number" && i > 0) return { w: 1, h: i }; if (typeof i == "string") { const o = i.split("/").map((t) => t.trim()); if (o.length === 2) { const t = Number(o[0]), s = Number(o[1]); if (t > 0 && s > 0) return { w: t, h: s }; } else if (o.length === 1) { const t = Number(o[0]); if (t > 0) return { w: 1, h: t }; } } return null; } const e = v(O), f = e ? { width: I, height: I * (e.h / e.w) } : null, C = e ? { width: f.height, height: f.width } : null; if (W) try { await g({ container: a, scale: r }), await g({ container: a, scale: r }), await g({ container: a, scale: r }), await g({ container: a, scale: r }); } catch { } const u = await g({ container: a, scale: r }); return await new Promise((i, o) => { const t = new window.Image(); t.onload = function() { const w = t.naturalWidth, c = t.naturalHeight, N = S === "auto" ? c >= w ? "p" : "l" : S, n = N === "l" ? e ? C : j : e ? f : b, d = n.width / w, M = n.height / c, y = c * d; let H = "single", h; y <= n.height + 0.5 ? h = d : y <= n.height * (1 + F) ? h = Math.min(d, M) : (H = "multi", h = d); const p = w * h, m = c * h, R = (n.width - p) / 2, l = new T({ orientation: N, unit: "pt", format: e ? [n.width, n.height] : "a4" }); if (H === "single") { const P = (n.height - m) / 2; l.addImage(u, "PNG", R, P, p, m, "", "FAST"); } else { const P = n.height / h; let A = c, _ = 0; for (; A > 0.5; ) l.addImage( u, "PNG", R, _, p, m, "", "FAST" ), A -= P, _ -= n.height, A > 0.5 && l.addPage(); } l.save(`${D}.pdf`), i(); }, t.onerror = (s) => o("Failed to load image for PDF: " + s), t.src = u; }); } export { E as default };