@ceereals/vue-pdf
Version:
Vue PDF - Create PDF documents using Vue components
609 lines (608 loc) • 13 kB
JavaScript
import * as i from "@react-pdf/primitives";
import { defineComponent as A, inject as M, onUpdated as V, h as C, Suspense as z, mergeProps as X, shallowRef as D, getCurrentInstance as E, ref as T, readonly as x, provide as L, onMounted as q, toValue as K, isVNode as W, createRenderer as $ } from "vue";
import { useObjectUrl as H, tryOnBeforeMount as J, tryOnBeforeUnmount as Q } from "@vueuse/core";
import Y from "defu";
import Z from "@react-pdf/font";
import ee from "@react-pdf/layout";
import te from "@react-pdf/render";
import re from "@react-pdf/pdfkit";
function a(t, e, n) {
return A(
(o, { slots: r }) => {
const s = M("execute", () => {
});
return V(() => {
s();
}), () => C(t, o, r.default?.());
},
{ props: n, name: e, inheritAttrs: !1 }
);
}
const ne = A({
name: "PDFSuspense",
emits: ["pending", "resolve", "fallback"],
setup(t, { slots: e, emit: n }) {
const o = M("execute", () => {
});
function r() {
n("pending");
}
function s() {
n("fallback"), o();
}
function l() {
n("resolve"), o();
}
return () => C(
z,
X(t, { onPending: r, onFallback: s, onResolve: l }),
{
default: () => e.default?.(),
fallback: () => e.fallback?.()
}
);
}
}), be = 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
}), ve = 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
}
}), Ne = a(i.View, "View", {
id: String,
style: [Object, Array],
fixed: {
type: Boolean,
default: !1
},
wrap: {
type: Boolean,
default: !0
},
debug: {
type: Boolean,
default: !1
},
// @ts-expect-error
render: Function
}), xe = 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
}), he = a(i.Link, "Link", {
style: [Object, Array],
fixed: {
type: Boolean,
default: !1
},
wrap: {
type: Boolean,
default: !0
},
debug: {
type: Boolean,
default: !1
},
src: String
}), Pe = a(i.Note, "Note", {
style: [Object, Array],
fixed: {
type: Boolean,
default: !1
}
}), Oe = 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"
}
}), Te = a(i.Rect, "Rect", {
x: [String, Number],
y: [String, Number],
width: [String, Number],
height: [String, Number],
rx: [String, Number],
ry: [String, Number]
}), Ce = a(i.Line, "Line", {
x1: [String, Number],
x2: [String, Number],
y1: [String, Number],
y2: [String, Number]
}), we = a(i.Stop, "Stop", {
offset: [String, Number],
stopColor: String,
stopOpacity: {
type: [String, Number],
default: 1
}
}), je = a(i.Defs, "Defs"), ke = 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]
}), Be = a(i.Tspan, "Tspan", {
x: [String, Number],
y: [String, Number]
}), Ae = a(i.Canvas, "Canvas", {
style: [Object, Array],
fixed: {
type: Boolean,
default: !1
},
debug: {
type: Boolean,
default: !1
},
// @ts-expect-error
paint: Function
}), De = 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]
}), Le = a(i.Circle, "Circle", {
cx: [String, Number],
cy: [String, Number],
r: [String, Number]
}), Re = a(i.Ellipse, "Ellipse", {
cx: [String, Number],
cy: [String, Number],
rx: [String, Number],
ry: [String, Number]
}), Fe = a(i.Polygon, "Polygon", {
points: String
}), _e = a(i.Polyline, "Polyline", {
points: String
}), Me = a(i.ClipPath, "ClipPath", {
id: String
}), Ge = 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"
}
}), Ue = a(
i.LinearGradient,
"LinearGradient",
{
x1: [String, Number],
x2: [String, Number],
y1: [String, Number],
y2: [String, Number]
}
), Ie = a(
i.RadialGradient,
"RadialGradient",
{
cx: [String, Number],
cy: [String, Number],
fr: [String, Number],
fx: [String, Number],
fy: [String, Number]
}
), Ve = ne, oe = (t) => t.type === "TEXT" || t.type === "LINK" || t.type === "TSPAN" || t.type === "NOTE", R = (t) => t.type === "TEXT_INSTANCE", ie = (t, e, n) => {
if (R(e) && !oe(t)) {
console.warn(
`Invalid '${e.value}' string child outside <Text> component`
);
return;
}
if (!R(t)) {
if (e.parentNode = t, n) {
const r = t.children?.indexOf(n);
if (r !== -1 && r !== void 0) {
t.children?.splice(r, 0, e);
return;
}
}
t.children?.push(e);
}
}, ae = (t) => ({
insert: (e, n, o) => {
if (e.type === "COMMENT") return;
let r = n;
if (!r)
if (e.type === "DOCUMENT" && t)
r = t;
else return;
r.type === "ROOT" ? r.document = e : ie(r, e, o);
},
remove: (e) => {
const n = e.parentNode?.children?.indexOf(e);
n !== void 0 && n !== -1 && (e.parentNode?.children?.splice(n, 1), e.parentNode = null);
},
createElement: (e, n, o, r) => {
const { style: s, render: l, ...d } = r || {};
return {
uid: crypto.randomUUID(),
type: e,
children: [],
parentNode: null,
style: s || {},
/* v8 ignore next */
props: l ? { ...d, render: l } : d || {},
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 o = e.parentNode?.children || [], r = o.indexOf(e);
return r < 0 || r >= o.length - 1 ? null : o[r + 1];
},
patchProp: (e, n, o, r, s, l) => {
if (n === "style")
e.style = r;
else {
if (n === "render" && r === void 0) return e;
e.props[n] = r;
}
return e;
}
});
function se(t) {
const { promise: e, resolve: n, reject: o } = Promise.withResolvers(), r = [];
return t.on("data", (s) => {
r.push(s);
}), t.on("end", () => {
try {
n(new Blob(r, { type: "application/pdf" }));
} catch (s) {
o(s);
}
}), t.on("error", (s) => {
o(s);
}), e;
}
const ue = (t) => Object.fromEntries(
Object.entries(t).filter(([, e]) => e !== void 0)
), F = (t) => t.charAt(0).toUpperCase() + t.slice(1), le = new Z(), ce = (t, { compress: e, signal: n } = {
compress: !0
}) => {
const { promise: o, resolve: r, reject: s } = Promise.withResolvers(), {
pdfVersion: l = "1.3",
language: d = "en",
pageLayout: m = "singlePage",
pageMode: p = "useNone",
title: w,
author: S,
subject: j,
keywords: k,
creator: b = "vue-pdf",
producer: v = "vue-pdf",
creationDate: h = /* @__PURE__ */ new Date(),
modificationDate: P
/* v8 ignore next */
} = t.document.props ?? {}, f = new re({
compress: e,
pdfVersion: l,
lang: d,
displayTitle: !0,
autoFirstPage: !1,
info: ue({
Title: w,
Author: S,
Subject: j,
Keywords: k,
Creator: b,
Producer: v,
CreationDate: h,
ModificationDate: P
})
});
m && (f._root.data.PageLayout = F(m)), p && (f._root.data.PageMode = F(p));
const N = ee(t.document, le);
return n?.addEventListener("abort", () => {
typeof N.abort == "function" && (N.abort(), s(new Error("Cancelled")));
}), N.then((O) => te(f, O)).then(r).catch(s), o;
};
function ze(t, e) {
const n = D({
type: "ROOT",
document: {}
}), o = E(), r = Y(e, {
worker: !1,
reactive: !0,
enableProvideBridge: !!o
}), s = T(!1), l = D(null), d = T(!!r.reactive), m = T(!r.reactive), p = T(null), w = H(l), S = (u) => {
d.value = u, m.value = !u;
}, j = () => A({
setup() {
const u = E()?.appContext;
u && o && (u.app = o?.appContext.app);
const c = {};
function g(y) {
y && (y.parent && g(y.parent), y.provides && Object.assign(c, y.provides));
}
if (o?.parent && r.enableProvideBridge) {
g(o.parent);
for (const [y, I] of Object.entries(c))
L(y, I);
}
return L("execute", () => {
r.reactive && v();
}), q(() => {
s.value = !0, r.reactive && v();
}), () => k();
}
}), k = () => {
const u = K(t);
return W(u) ? u : C(u);
};
let b = new AbortController();
const v = async (u = !1) => {
if (r.reactive || h(), b.abort(), b = new AbortController(), s.value && "type" in n.value.document) {
S(!0);
try {
const c = await ce(n.value, {
signal: b.signal,
compress: !0
}), g = await se(c);
l.value = g, S(!1), O(f);
} catch (c) {
if (c.message === "Cancelled") {
if (r.onError && (p.value = await r.onError(c) ?? c), u) throw p.value;
return;
}
if (l.value = null, p.value = c, r.onError) {
const g = await r.onError(c);
g && (p.value = g);
}
if (S(!1), u) throw p.value;
U(p.value);
return;
}
}
}, h = () => {
_(C(j()), n.value);
}, P = () => {
_(null, n.value);
}, f = {
isLoading: x(d),
isFinished: x(m),
error: x(p),
blob: x(l),
url: w,
execute: v,
unmount: P,
root: x(n)
}, { promise: N, resolve: O, reject: U } = Promise.withResolvers();
return J(() => {
r.reactive && h(), r.reactive || O(f);
}), Q(() => {
P();
}), {
...f,
then(u, c) {
return N.then(u, c);
}
};
}
let B;
function G(t) {
return B || (B = $(ae(t))), B;
}
const _ = (...t) => {
G(t[1]).render(t[0], t[1]);
}, Xe = (...t) => {
const e = G().createApp(...t), { mount: n } = e;
return e.mount = (o) => n(o, !1), e;
};
export {
Ae as C,
be as D,
Re as E,
Ge as G,
ke as I,
he as L,
Pe as N,
ve as P,
Te as R,
we as S,
xe as T,
Ne as V,
Oe as a,
Ce as b,
je as c,
Be as d,
De as e,
le as f,
Ee as g,
Le as h,
Fe as i,
_e as j,
Me as k,
Ue as l,
Ie as m,
Ve as n,
G as o,
ce as p,
Xe as q,
_ as r,
ze as u
};