UNPKG

@ceereals/vue-pdf

Version:

Vue PDF - Create PDF documents using Vue components

587 lines (586 loc) 12.8 kB
import * as i from "@react-pdf/primitives"; import { defineComponent as M, inject as V, onUpdated as z, h as A, shallowRef as k, getCurrentInstance as D, ref as T, readonly as x, provide as L, onMounted as X, toValue as K, isVNode as W, createRenderer as q } from "vue"; import { useObjectUrl as $, tryOnBeforeMount as H, tryOnBeforeUnmount as J } from "@vueuse/core"; import Q from "defu"; import Y from "@react-pdf/font"; import Z from "@react-pdf/layout"; import ee from "@react-pdf/render"; import te from "@react-pdf/pdfkit"; function a(r, e, n) { return M( (o, { slots: t }) => { const s = V("execute", () => { }); return z(() => { s(); }), () => { var u; return A(r, o, (u = t.default) == null ? void 0 : u.call(t)); }; }, { props: n, name: e, inheritAttrs: !1 } ); } const me = a(i.Document, "Document", { author: String, creator: { type: String, default: "vue-pdf" }, keywords: String, language: String, // @ts-expect-error pageLayout: { type: String, default: "singlePage" }, // @ts-expect-error pageMode: { type: String, default: "useNone" }, pdfVersion: { type: String, default: "1.3" }, producer: { type: String, default: "vue-pdf" }, subject: String, title: String }), Se = a(i.Page, "Page", { bookmark: [String, Object], debug: { type: Boolean, default: !1 }, dpi: { type: Number, default: 72 }, id: String, // @ts-expect-error orientation: { type: String, default: "portrait" }, size: { type: [String, Array, Number, Object], default: "A4" }, style: [Object, Array], wrap: { type: Boolean, default: !0 } }), be = a(i.View, "View", { id: String, style: [Object, Array], fixed: { type: Boolean, default: !1 }, wrap: Boolean, debug: { type: Boolean, default: !1 }, // @ts-expect-error render: Function }), Ne = a(i.Text, "Text", { id: String, style: [Object, Array], fixed: { type: Boolean, default: !1 }, wrap: { type: Boolean, default: !0 }, debug: { type: Boolean, default: !1 }, // @ts-expect-error hyphenationCallback: Function, // @ts-expect-error render: Function }), ve = a(i.Link, "Link", { style: [Object, Array], fixed: { type: Boolean, default: !1 }, wrap: { type: Boolean, default: !0 }, debug: { type: Boolean, default: !1 }, src: String }), xe = a(i.Note, "Note", { style: [Object, Array], fixed: { type: Boolean, default: !1 } }), he = a(i.Path, "Path", { style: Object, d: String, fill: String, stroke: String, transform: String, strokeDasharray: String, opacity: { type: [String, Number], default: 1 }, strokeWidth: { type: [String, Number], default: 1 }, fillOpacity: { type: [String, Number], default: 1 }, // @ts-expect-error fillRule: { type: String, default: "nonzero" }, strokeOpacity: { type: [String, Number], default: 1 }, // @ts-expect-error textAnchor: String, // @ts-expect-error strokeLinecap: { type: String, default: "butt" }, // @ts-expect-error strokeLinejoin: { type: String, default: "miter" }, // @ts-expect-error visibility: { type: String, default: "visible" }, // @ts-expect-error dominantBaseline: { type: String, default: "auto" } }), Oe = a(i.Rect, "Rect", { x: [String, Number], y: [String, Number], width: [String, Number], height: [String, Number], rx: [String, Number], ry: [String, Number] }), Pe = a(i.Line, "Line", { x1: [String, Number], x2: [String, Number], y1: [String, Number], y2: [String, Number] }), Te = a(i.Stop, "Stop", { offset: [String, Number], stopColor: String, stopOpacity: { type: [String, Number], default: 1 } }), Ce = a(i.Defs, "Defs"), we = a(i.Image, "Image", { style: [Object, Array], fixed: { type: Boolean, default: !1 }, debug: { type: Boolean, default: !1 }, cache: { type: Boolean, default: !0 }, // @ts-expect-error src: [String, Function, Object], // @ts-expect-error source: [String, Function, Object] }), je = a(i.Tspan, "Tspan", { x: [String, Number], y: [String, Number] }), Be = a(i.Canvas, "Canvas", { style: [Object, Array], fixed: { type: Boolean, default: !1 }, debug: { type: Boolean, default: !1 }, // @ts-expect-error paint: Function }), Ae = a(i.Svg, "Svg", { width: [String, Number], height: [String, Number], viewBox: String, preserveAspectRatio: String, style: [Object, Array] }), Ee = a(i.Text, "Text", { x: [String, Number], y: [String, Number] }), ke = a(i.Circle, "Circle", { cx: [String, Number], cy: [String, Number], r: [String, Number] }), De = a(i.Ellipse, "Ellipse", { cx: [String, Number], cy: [String, Number], rx: [String, Number], ry: [String, Number] }), Le = a(i.Polygon, "Polygon", { points: String }), Re = a(i.Polyline, "Polyline", { points: String }), _e = a(i.ClipPath, "ClipPath", { id: String }), Fe = a(i.G, "G", { color: String, // @ts-expect-error dominantBaseline: { type: String, default: "auto" }, fill: String, fillOpacity: { type: [String, Number], default: 1 }, // @ts-expect-error fillRule: { type: String, default: "nonzero" }, opacity: { type: [String, Number], default: 1 }, stroke: String, strokeWidth: { type: [String, Number], default: 1 }, strokeOpacity: { type: [String, Number], default: 1 }, // @ts-expect-error strokeLinecap: { type: String, default: "butt" }, // @ts-expect-error strokeLinejoin: { type: String, default: "miter" }, strokeDasharray: String, transform: String, // @ts-expect-error textAnchor: String, // @ts-expect-error visibility: { type: String, default: "visible" } }), Me = a( i.LinearGradient, "LinearGradient", { x1: [String, Number], x2: [String, Number], y1: [String, Number], y2: [String, Number] } ), Ge = a( i.RadialGradient, "RadialGradient", { cx: [String, Number], cy: [String, Number], fr: [String, Number], fx: [String, Number], fy: [String, Number] } ), re = (r) => r.type === "TEXT" || r.type === "LINK" || r.type === "TSPAN" || r.type === "NOTE", R = (r) => r.type === "TEXT_INSTANCE", ne = (r, e, n) => { var t, s, u; if (R(e) && !re(r)) { console.warn( `Invalid '${e.value}' string child outside <Text> component` ); return; } if (!R(r)) { if (e.parentNode = r, n) { const p = (t = r.children) == null ? void 0 : t.indexOf(n); if (p !== -1 && p !== void 0) { (s = r.children) == null || s.splice(p, 0, e); return; } } (u = r.children) == null || u.push(e); } }, oe = (r) => ({ insert: (e, n, o) => { if (e.type === "COMMENT") return; let t = n; if (!t) if (e.type === "DOCUMENT" && r) t = r; else return; t.type === "ROOT" ? t.document = e : ne(t, e, o); }, remove: (e) => { var o, t, s, u; const n = (t = (o = e.parentNode) == null ? void 0 : o.children) == null ? void 0 : t.indexOf(e); n !== void 0 && n !== -1 && ((u = (s = e.parentNode) == null ? void 0 : s.children) == null || u.splice(n, 1), e.parentNode = null); }, createElement: (e, n, o, t) => { const { style: s, render: u, ...p } = t || {}; return { uid: crypto.randomUUID(), type: e, children: [], parentNode: null, style: s || {}, /* v8 ignore next */ props: u ? { ...p, render: u } : p || {}, box: {} }; }, createText: (e) => ({ type: "TEXT_INSTANCE", value: e }), createComment: () => ({ type: "COMMENT" }), setText: (e, n) => { e.value = n; }, setElementText: (e, n) => { e.value = n; }, /* v8 ignore next */ querySelector: () => null, /* v8 ignore next */ setScopeId(e, n) { }, parentNode(e) { return e.parentNode; }, nextSibling: (e) => { const n = e.parentNode, o = (n == null ? void 0 : n.children) || [], t = o.indexOf(e); return t < 0 || t >= o.length - 1 ? null : o[t + 1]; }, patchProp: (e, n, o, t, s, u) => { if (n === "style") e.style = t; else { if (n === "render" && t === void 0) return e; e.props[n] = t; } return e; } }); function ie(r) { const { promise: e, resolve: n, reject: o } = Promise.withResolvers(), t = []; return r.on("data", (s) => { t.push(s); }), r.on("end", () => { try { n(new Blob(t, { type: "application/pdf" })); } catch (s) { o(s); } }), r.on("error", (s) => { o(s); }), e; } const ae = (r) => Object.fromEntries( Object.entries(r).filter(([, e]) => e !== void 0) ), _ = (r) => r.charAt(0).toUpperCase() + r.slice(1), se = new Y(), ue = (r, { compress: e, signal: n } = { compress: !0 }) => { const { promise: o, resolve: t, reject: s } = Promise.withResolvers(), { pdfVersion: u = "1.3", language: p = "en", pageLayout: m = "singlePage", pageMode: d = "useNone", title: C, author: S, subject: w, keywords: j, creator: b = "vue-pdf", producer: N = "vue-pdf", creationDate: h = /* @__PURE__ */ new Date(), modificationDate: O /* v8 ignore next */ } = r.document.props ?? {}, f = new te({ compress: e, pdfVersion: u, lang: p, displayTitle: !0, autoFirstPage: !1, info: ae({ Title: C, Author: S, Subject: w, Keywords: j, Creator: b, Producer: N, CreationDate: h, ModificationDate: O }) }); m && (f._root.data.PageLayout = _(m)), d && (f._root.data.PageMode = _(d)); const v = Z(r.document, se); return n == null || n.addEventListener("abort", () => { typeof v.abort == "function" && (v.abort(), s(new Error("Cancelled"))); }), v.then((P) => ee(f, P)).then(t).catch(s), o; }; function Ue(r, e) { const n = k({ type: "ROOT", document: {} }), o = D(), t = Q(e, { worker: !1, reactive: !0, enableProvideBridge: !!o }), s = T(!1), u = k(null), p = T(!!t.reactive), m = T(!t.reactive), d = T(null), C = $(u), S = (l) => { p.value = l, m.value = !l; }, w = () => M({ setup() { var E; const l = (E = D()) == null ? void 0 : E.appContext; l && o && (l.app = o == null ? void 0 : o.appContext.app); const c = {}; function g(y) { y && (y.parent && g(y.parent), y.provides && Object.assign(c, y.provides)); } if (o != null && o.parent && t.enableProvideBridge) { g(o.parent); for (const [y, I] of Object.entries(c)) L(y, I); } return L("execute", () => { t.reactive && N(); }), X(() => { s.value = !0, t.reactive && N(); }), () => j(); } }), j = () => { const l = K(r); return W(l) ? l : A(l); }; let b = new AbortController(); const N = async (l = !1) => { if (t.reactive || h(), b.abort(), b = new AbortController(), s.value) { S(!0); try { const c = await ue(n.value, { signal: b.signal, compress: !0 }), g = await ie(c); u.value = g, S(!1), P(f); } catch (c) { if (c.message === "Cancelled") { if (t.onError && (d.value = await t.onError(c) ?? c), l) throw d.value; return; } if (u.value = null, d.value = c, t.onError) { const g = await t.onError(c); g && (d.value = g); } if (S(!1), l) throw d.value; U(d.value); return; } } }, h = () => { F(A(w()), n.value); }, O = () => { F(null, n.value); }, f = { isLoading: x(p), isFinished: x(m), error: x(d), blob: x(u), url: C, execute: N, unmount: O, root: x(n) }, { promise: v, resolve: P, reject: U } = Promise.withResolvers(); return H(() => { t.reactive && h(), t.reactive || P(f); }), J(() => { O(); }), { ...f, then(l, c) { return v.then(l, c); } }; } let B; function G(r) { return B || (B = q(oe(r))), B; } const F = (...r) => { G(r[1]).render(r[0], r[1]); }, Ie = (...r) => { const e = G().createApp(...r), { mount: n } = e; return e.mount = (o) => n(o, !1), e; }; export { Be as C, me as D, De as E, Fe as G, we as I, ve as L, xe as N, Se as P, Oe as R, Te as S, Ne as T, be as V, he as a, Pe as b, Ce as c, je as d, Ae as e, Ee as f, ke as g, Le as h, Re as i, _e as j, Me as k, Ge as l, G as m, Ie as n, se as o, ue as p, F as r, Ue as u };