@imggion/html2realpdf 0.1.0
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.
- package/README.md +292 -0
- package/dist/LICENSE.md +465 -0
- package/dist/compat.d.ts +113 -0
- package/dist/compat.js +215 -0
- package/dist/compat.js.map +1 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.js +73 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/libhtml2realpdf.wasm +0 -0
- package/dist/page.d.ts +23 -0
- package/dist/page.js +54 -0
- package/dist/page.js.map +1 -0
- package/dist/pdf-document.d.ts +51 -0
- package/dist/pdf-document.js +107 -0
- package/dist/pdf-document.js.map +1 -0
- package/dist/preview.d.ts +74 -0
- package/dist/preview.js +369 -0
- package/dist/preview.js.map +1 -0
- package/dist/renderer.d.ts +64 -0
- package/dist/renderer.js +276 -0
- package/dist/renderer.js.map +1 -0
- package/dist/snapshot.d.ts +79 -0
- package/dist/snapshot.js +2972 -0
- package/dist/snapshot.js.map +1 -0
- package/dist/types.d.ts +273 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/vendor/pdf.min.mjs +28 -0
- package/dist/vendor/pdf.worker.min.mjs +28 -0
- package/dist/wasm.d.ts +39 -0
- package/dist/wasm.js +176 -0
- package/dist/wasm.js.map +1 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +30 -0
- package/dist/worker.js.map +1 -0
- package/package.json +57 -0
- package/skills/html2realpdf/SKILL.md +260 -0
- package/skills/html2realpdf/agents/openai.yaml +4 -0
package/dist/compat.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PDF-oriented html2pdf.js compatibility chain over the native renderer.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import { UnsupportedCompatibilityFeatureError } from "./errors.js";
|
|
7
|
+
import { PdfDocument } from "./pdf-document.js";
|
|
8
|
+
import { renderPdf } from "./renderer.js";
|
|
9
|
+
/**
|
|
10
|
+
* Stateful, Promise-like compatibility chain.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* The worker caches one `PdfDocument` until its source or options change.
|
|
14
|
+
* Raster-only html2pdf.js stages fail explicitly rather than silently changing
|
|
15
|
+
* the native PDF model.
|
|
16
|
+
*/
|
|
17
|
+
export class CompatWorker {
|
|
18
|
+
source;
|
|
19
|
+
options = {};
|
|
20
|
+
pdfPromise;
|
|
21
|
+
tail = Promise.resolve();
|
|
22
|
+
/** Sets the HTML source; canvas and image input stages are unsupported. */
|
|
23
|
+
from(source, type) {
|
|
24
|
+
if (type === "canvas" || type === "img") {
|
|
25
|
+
throw new UnsupportedCompatibilityFeatureError(`from(..., ${type})`);
|
|
26
|
+
}
|
|
27
|
+
this.source = source;
|
|
28
|
+
this.pdfPromise = undefined;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
/** Shallow-merges supported compatibility options and invalidates cached output. */
|
|
32
|
+
set(options) {
|
|
33
|
+
if (options.html2canvas !== undefined) {
|
|
34
|
+
throw new UnsupportedCompatibilityFeatureError("html2canvas options");
|
|
35
|
+
}
|
|
36
|
+
if (options.image !== undefined) {
|
|
37
|
+
throw new UnsupportedCompatibilityFeatureError("image options");
|
|
38
|
+
}
|
|
39
|
+
this.options = { ...this.options, ...options };
|
|
40
|
+
this.pdfPromise = undefined;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/** Alias for `set`, retained for html2pdf.js chain compatibility. */
|
|
44
|
+
using(options) {
|
|
45
|
+
return this.set(options);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Advances to PDF output for `pdf` or `container` targets.
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* The wrapper has no public intermediate container. A `container` target
|
|
52
|
+
* therefore schedules the PDF directly; canvas and image targets throw.
|
|
53
|
+
*/
|
|
54
|
+
to(target) {
|
|
55
|
+
if (target === "pdf" || target === "container")
|
|
56
|
+
return this.toPdf();
|
|
57
|
+
throw new UnsupportedCompatibilityFeatureError(`to(${target})`);
|
|
58
|
+
}
|
|
59
|
+
/** Schedules PDF rendering while preserving the fluent chain. */
|
|
60
|
+
toPdf() {
|
|
61
|
+
this.tail = this.ensurePdf();
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
/** Compatibility no-op; no intermediate snapshot container is exposed. */
|
|
65
|
+
toContainer() {
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/** Always throws because full-page canvas output is intentionally unsupported. */
|
|
69
|
+
toCanvas() {
|
|
70
|
+
throw new UnsupportedCompatibilityFeatureError("toCanvas");
|
|
71
|
+
}
|
|
72
|
+
/** Always throws because raster image output is intentionally unsupported. */
|
|
73
|
+
toImg() {
|
|
74
|
+
throw new UnsupportedCompatibilityFeatureError("toImg");
|
|
75
|
+
}
|
|
76
|
+
/** Schedules a browser download using the explicit or configured filename. */
|
|
77
|
+
save(filename = this.options.filename ?? "file.pdf") {
|
|
78
|
+
this.tail = this.ensurePdf().then((pdf) => pdf.download(filename));
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
/** Alias for `save`. */
|
|
82
|
+
saveAs(filename) {
|
|
83
|
+
return this.save(filename);
|
|
84
|
+
}
|
|
85
|
+
async outputPdf(type = "blob") {
|
|
86
|
+
const pdf = await this.ensurePdf();
|
|
87
|
+
switch (type) {
|
|
88
|
+
case "arraybuffer": return pdf.toArrayBuffer();
|
|
89
|
+
case "blob": return pdf.toBlob();
|
|
90
|
+
case "bloburl":
|
|
91
|
+
case "bloburi": return pdf.createObjectURL();
|
|
92
|
+
case "datauristring":
|
|
93
|
+
case "dataurlstring": return blobToDataUrl(pdf.toBlob());
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
output(type, _options, source = "pdf") {
|
|
97
|
+
if (source === "img")
|
|
98
|
+
return Promise.reject(new UnsupportedCompatibilityFeatureError("outputImg"));
|
|
99
|
+
return type === undefined ? this.outputPdf() : this.outputPdf(type);
|
|
100
|
+
}
|
|
101
|
+
export(type, options, source) {
|
|
102
|
+
return this.output(type, options, source);
|
|
103
|
+
}
|
|
104
|
+
/** Returns a rejected promise because raster image output is unsupported. */
|
|
105
|
+
outputImg() {
|
|
106
|
+
return Promise.reject(new UnsupportedCompatibilityFeatureError("outputImg"));
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Resolves the cached PDF for `pdf`, or a configured compatibility option for
|
|
110
|
+
* any other key, then optionally invokes the legacy callback.
|
|
111
|
+
*/
|
|
112
|
+
get(key, callback) {
|
|
113
|
+
const value = key === "pdf" ? this.ensurePdf() : Promise.resolve(this.options[key]);
|
|
114
|
+
return value.then((resolved) => {
|
|
115
|
+
callback?.(resolved);
|
|
116
|
+
return resolved;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/** Interrupts a chain by throwing the supplied message as an `Error`. */
|
|
120
|
+
error(message) {
|
|
121
|
+
throw new Error(message);
|
|
122
|
+
}
|
|
123
|
+
/** Makes the chain awaitable and resolves it to the cached `PdfDocument`. */
|
|
124
|
+
then(onFulfilled, onRejected) {
|
|
125
|
+
return this.tail.then(() => this.ensurePdf()).then(onFulfilled, onRejected);
|
|
126
|
+
}
|
|
127
|
+
/** Compatibility alias for `then`. */
|
|
128
|
+
thenCore(onFulfilled, onRejected) {
|
|
129
|
+
return this.then(onFulfilled, onRejected);
|
|
130
|
+
}
|
|
131
|
+
/** Compatibility alias for `then`. */
|
|
132
|
+
thenExternal(onFulfilled, onRejected) {
|
|
133
|
+
return this.then(onFulfilled, onRejected);
|
|
134
|
+
}
|
|
135
|
+
/** Compatibility alias for `then`. */
|
|
136
|
+
run(onFulfilled, onRejected) {
|
|
137
|
+
return this.then(onFulfilled, onRejected);
|
|
138
|
+
}
|
|
139
|
+
/** Handles a failed chain while preserving `PdfDocument` as the success type. */
|
|
140
|
+
catch(onRejected) {
|
|
141
|
+
return this.then(undefined, onRejected);
|
|
142
|
+
}
|
|
143
|
+
/** Compatibility alias for `catch`. */
|
|
144
|
+
catchExternal(onRejected) {
|
|
145
|
+
return this.catch(onRejected);
|
|
146
|
+
}
|
|
147
|
+
ensurePdf() {
|
|
148
|
+
if (!this.source)
|
|
149
|
+
return Promise.reject(new Error("No HTML source was provided"));
|
|
150
|
+
this.pdfPromise ??= renderPdf(this.source, compatRenderOptions(this.options));
|
|
151
|
+
return this.pdfPromise;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Creates an html2pdf.js-compatible PDF chain.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* Passing a source directly preserves html2pdf.js shorthand behavior and also
|
|
159
|
+
* schedules `save`. Call without a source when composing an explicit chain.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* await html2pdf()
|
|
164
|
+
* .set({ filename: "invoice.pdf", margin: [10, 12] })
|
|
165
|
+
* .from(element)
|
|
166
|
+
* .save();
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
export default function html2pdf(source, options) {
|
|
170
|
+
const worker = new CompatWorker();
|
|
171
|
+
if (options)
|
|
172
|
+
worker.set(options);
|
|
173
|
+
if (source)
|
|
174
|
+
worker.from(source).save();
|
|
175
|
+
return worker;
|
|
176
|
+
}
|
|
177
|
+
html2pdf.Worker = CompatWorker;
|
|
178
|
+
function compatRenderOptions(options) {
|
|
179
|
+
const page = { unit: options.jsPDF?.unit ?? "mm" };
|
|
180
|
+
if (options.jsPDF?.format !== undefined)
|
|
181
|
+
page.format = options.jsPDF.format;
|
|
182
|
+
if (options.jsPDF?.orientation !== undefined)
|
|
183
|
+
page.orientation = options.jsPDF.orientation;
|
|
184
|
+
if (options.margin !== undefined)
|
|
185
|
+
page.margin = options.margin;
|
|
186
|
+
const renderOptions = { page };
|
|
187
|
+
if (options.enableLinks !== undefined)
|
|
188
|
+
renderOptions.enableLinks = options.enableLinks;
|
|
189
|
+
if (options.pagebreak) {
|
|
190
|
+
const modes = typeof options.pagebreak.mode === "string"
|
|
191
|
+
? [options.pagebreak.mode]
|
|
192
|
+
: options.pagebreak.mode ?? ["css", "legacy"];
|
|
193
|
+
const pageBreak = {
|
|
194
|
+
legacy: modes.includes("legacy"),
|
|
195
|
+
avoidAll: modes.includes("avoid-all"),
|
|
196
|
+
};
|
|
197
|
+
if (options.pagebreak.before !== undefined)
|
|
198
|
+
pageBreak.before = options.pagebreak.before;
|
|
199
|
+
if (options.pagebreak.after !== undefined)
|
|
200
|
+
pageBreak.after = options.pagebreak.after;
|
|
201
|
+
if (options.pagebreak.avoid !== undefined)
|
|
202
|
+
pageBreak.avoid = options.pagebreak.avoid;
|
|
203
|
+
renderOptions.pageBreak = pageBreak;
|
|
204
|
+
}
|
|
205
|
+
return renderOptions;
|
|
206
|
+
}
|
|
207
|
+
function blobToDataUrl(blob) {
|
|
208
|
+
return new Promise((resolve, reject) => {
|
|
209
|
+
const reader = new FileReader();
|
|
210
|
+
reader.addEventListener("load", () => resolve(String(reader.result)), { once: true });
|
|
211
|
+
reader.addEventListener("error", () => reject(reader.error), { once: true });
|
|
212
|
+
reader.readAsDataURL(blob);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=compat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compat.js","sourceRoot":"","sources":["../src/compat.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,oCAAoC,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAG1C;;;;;;;GAOG;AACH,MAAM,OAAO,YAAY;IACf,MAAM,CAAc;IACpB,OAAO,GAAoB,EAAE,CAAC;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AAS,CAAC,KAAK,GAAG,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC;QACrF,IAAI,OAAO,CAAC,SAAS,CAAC,KAAK,KAAK,SAAS;YAAE,SAAS,CAAC,KAAK,GAAG,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC;QACrF,aAAa,CAAC,SAAS,GAAG,SAAS,CAAC;IACtC,CAAC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC;AAID,SAAS,aAAa,CAAC,IAAU;IAC/B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;QAChC,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACtF,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAC7E,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["/**\n * PDF-oriented html2pdf.js compatibility chain over the native renderer.\n *\n * @packageDocumentation\n */\n\nimport { UnsupportedCompatibilityFeatureError } from \"./errors.js\";\nimport { PdfDocument } from \"./pdf-document.js\";\nimport { renderPdf } from \"./renderer.js\";\nimport type { Html2PdfOptions, HtmlSource, PageOptions, PdfOutputType, RenderOptions } from \"./types.js\";\n\n/**\n * Stateful, Promise-like compatibility chain.\n *\n * @remarks\n * The worker caches one `PdfDocument` until its source or options change.\n * Raster-only html2pdf.js stages fail explicitly rather than silently changing\n * the native PDF model.\n */\nexport class CompatWorker implements PromiseLike<PdfDocument> {\n private source?: HtmlSource;\n private options: Html2PdfOptions = {};\n private pdfPromise: Promise<PdfDocument> | undefined;\n private tail: Promise<unknown> = Promise.resolve();\n\n /** Sets the HTML source; canvas and image input stages are unsupported. */\n from(source: HtmlSource, type?: \"string\" | \"element\" | \"canvas\" | \"img\"): this {\n if (type === \"canvas\" || type === \"img\") {\n throw new UnsupportedCompatibilityFeatureError(`from(..., ${type})`);\n }\n this.source = source;\n this.pdfPromise = undefined;\n return this;\n }\n\n /** Shallow-merges supported compatibility options and invalidates cached output. */\n set(options: Html2PdfOptions): this {\n if (options.html2canvas !== undefined) {\n throw new UnsupportedCompatibilityFeatureError(\"html2canvas options\");\n }\n if (options.image !== undefined) {\n throw new UnsupportedCompatibilityFeatureError(\"image options\");\n }\n this.options = { ...this.options, ...options };\n this.pdfPromise = undefined;\n return this;\n }\n\n /** Alias for `set`, retained for html2pdf.js chain compatibility. */\n using(options: Html2PdfOptions): this {\n return this.set(options);\n }\n\n /**\n * Advances to PDF output for `pdf` or `container` targets.\n *\n * @remarks\n * The wrapper has no public intermediate container. A `container` target\n * therefore schedules the PDF directly; canvas and image targets throw.\n */\n to(target: \"pdf\" | \"container\" | \"canvas\" | \"img\"): this {\n if (target === \"pdf\" || target === \"container\") return this.toPdf();\n throw new UnsupportedCompatibilityFeatureError(`to(${target})`);\n }\n\n /** Schedules PDF rendering while preserving the fluent chain. */\n toPdf(): this {\n this.tail = this.ensurePdf();\n return this;\n }\n\n /** Compatibility no-op; no intermediate snapshot container is exposed. */\n toContainer(): this {\n return this;\n }\n\n /** Always throws because full-page canvas output is intentionally unsupported. */\n toCanvas(): never {\n throw new UnsupportedCompatibilityFeatureError(\"toCanvas\");\n }\n\n /** Always throws because raster image output is intentionally unsupported. */\n toImg(): never {\n throw new UnsupportedCompatibilityFeatureError(\"toImg\");\n }\n\n /** Schedules a browser download using the explicit or configured filename. */\n save(filename = this.options.filename ?? \"file.pdf\"): this {\n this.tail = this.ensurePdf().then((pdf) => pdf.download(filename));\n return this;\n }\n\n /** Alias for `save`. */\n saveAs(filename?: string): this {\n return this.save(filename);\n }\n\n /**\n * Resolves PDF output in a compatibility encoding.\n *\n * @remarks\n * Blob URL outputs remain owned by the cached `PdfDocument`. Obtain it with\n * `get(\"pdf\")` to revoke the URL, or dispose the document when finished.\n */\n outputPdf(type?: \"blob\"): Promise<Blob>;\n outputPdf(type: \"arraybuffer\"): Promise<ArrayBuffer>;\n outputPdf(type: StringPdfOutputType): Promise<string>;\n outputPdf(type: PdfOutputType): Promise<ArrayBuffer | Blob | string>;\n async outputPdf(type: PdfOutputType = \"blob\"): Promise<ArrayBuffer | Blob | string> {\n const pdf = await this.ensurePdf();\n switch (type) {\n case \"arraybuffer\": return pdf.toArrayBuffer();\n case \"blob\": return pdf.toBlob();\n case \"bloburl\":\n case \"bloburi\": return pdf.createObjectURL();\n case \"datauristring\":\n case \"dataurlstring\": return blobToDataUrl(pdf.toBlob());\n }\n }\n\n /** Routes generic html2pdf.js output requests to PDF output. */\n output(type?: \"blob\", options?: unknown, source?: \"pdf\"): Promise<Blob>;\n output(type: \"arraybuffer\", options?: unknown, source?: \"pdf\"): Promise<ArrayBuffer>;\n output(type: StringPdfOutputType, options?: unknown, source?: \"pdf\"): Promise<string>;\n output(type: PdfOutputType | undefined, options: unknown, source: \"img\"): Promise<never>;\n output(type?: PdfOutputType, options?: unknown, source?: \"pdf\" | \"img\"): Promise<ArrayBuffer | Blob | string>;\n output(type?: PdfOutputType, _options?: unknown, source: \"pdf\" | \"img\" = \"pdf\"): Promise<ArrayBuffer | Blob | string> {\n if (source === \"img\") return Promise.reject(new UnsupportedCompatibilityFeatureError(\"outputImg\"));\n return type === undefined ? this.outputPdf() : this.outputPdf(type);\n }\n\n /** Alias for `output`. */\n export(type?: \"blob\", options?: unknown, source?: \"pdf\"): Promise<Blob>;\n export(type: \"arraybuffer\", options?: unknown, source?: \"pdf\"): Promise<ArrayBuffer>;\n export(type: StringPdfOutputType, options?: unknown, source?: \"pdf\"): Promise<string>;\n export(type: PdfOutputType | undefined, options: unknown, source: \"img\"): Promise<never>;\n export(type?: PdfOutputType, options?: unknown, source?: \"pdf\" | \"img\"): Promise<ArrayBuffer | Blob | string>;\n export(type?: PdfOutputType, options?: unknown, source?: \"pdf\" | \"img\"): Promise<ArrayBuffer | Blob | string> {\n return this.output(type, options, source);\n }\n\n /** Returns a rejected promise because raster image output is unsupported. */\n outputImg(): Promise<never> {\n return Promise.reject(new UnsupportedCompatibilityFeatureError(\"outputImg\"));\n }\n\n /**\n * Resolves the cached PDF for `pdf`, or a configured compatibility option for\n * any other key, then optionally invokes the legacy callback.\n */\n get(key: string, callback?: (value: unknown) => void): Promise<unknown> {\n const value = key === \"pdf\" ? this.ensurePdf() : Promise.resolve(this.options[key as keyof Html2PdfOptions]);\n return value.then((resolved) => {\n callback?.(resolved);\n return resolved;\n });\n }\n\n /** Interrupts a chain by throwing the supplied message as an `Error`. */\n error(message: string): never {\n throw new Error(message);\n }\n\n /** Makes the chain awaitable and resolves it to the cached `PdfDocument`. */\n then<TResult1 = PdfDocument, TResult2 = never>(\n onFulfilled?: ((value: PdfDocument) => TResult1 | PromiseLike<TResult1>) | null,\n onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n return this.tail.then(() => this.ensurePdf()).then(onFulfilled, onRejected);\n }\n\n /** Compatibility alias for `then`. */\n thenCore<TResult1 = PdfDocument, TResult2 = never>(\n onFulfilled?: ((value: PdfDocument) => TResult1 | PromiseLike<TResult1>) | null,\n onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n return this.then(onFulfilled, onRejected);\n }\n\n /** Compatibility alias for `then`. */\n thenExternal<TResult1 = PdfDocument, TResult2 = never>(\n onFulfilled?: ((value: PdfDocument) => TResult1 | PromiseLike<TResult1>) | null,\n onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n return this.then(onFulfilled, onRejected);\n }\n\n /** Compatibility alias for `then`. */\n run<TResult1 = PdfDocument, TResult2 = never>(\n onFulfilled?: ((value: PdfDocument) => TResult1 | PromiseLike<TResult1>) | null,\n onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n return this.then(onFulfilled, onRejected);\n }\n\n /** Handles a failed chain while preserving `PdfDocument` as the success type. */\n catch<TResult = never>(onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): Promise<PdfDocument | TResult> {\n return this.then(undefined, onRejected);\n }\n\n /** Compatibility alias for `catch`. */\n catchExternal<TResult = never>(onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): Promise<PdfDocument | TResult> {\n return this.catch(onRejected);\n }\n\n private ensurePdf(): Promise<PdfDocument> {\n if (!this.source) return Promise.reject(new Error(\"No HTML source was provided\"));\n this.pdfPromise ??= renderPdf(this.source, compatRenderOptions(this.options));\n return this.pdfPromise;\n }\n}\n\n/**\n * Creates an html2pdf.js-compatible PDF chain.\n *\n * @remarks\n * Passing a source directly preserves html2pdf.js shorthand behavior and also\n * schedules `save`. Call without a source when composing an explicit chain.\n *\n * @example\n * ```ts\n * await html2pdf()\n * .set({ filename: \"invoice.pdf\", margin: [10, 12] })\n * .from(element)\n * .save();\n * ```\n */\nexport default function html2pdf(source?: HtmlSource, options?: Html2PdfOptions): CompatWorker {\n const worker = new CompatWorker();\n if (options) worker.set(options);\n if (source) worker.from(source).save();\n return worker;\n}\n\nhtml2pdf.Worker = CompatWorker;\n\nfunction compatRenderOptions(options: Html2PdfOptions): RenderOptions {\n const page: PageOptions = { unit: options.jsPDF?.unit ?? \"mm\" };\n if (options.jsPDF?.format !== undefined) page.format = options.jsPDF.format;\n if (options.jsPDF?.orientation !== undefined) page.orientation = options.jsPDF.orientation;\n if (options.margin !== undefined) page.margin = options.margin;\n\n const renderOptions: RenderOptions = { page };\n if (options.enableLinks !== undefined) renderOptions.enableLinks = options.enableLinks;\n if (options.pagebreak) {\n const modes = typeof options.pagebreak.mode === \"string\"\n ? [options.pagebreak.mode]\n : options.pagebreak.mode ?? [\"css\", \"legacy\"];\n const pageBreak: NonNullable<RenderOptions[\"pageBreak\"]> = {\n legacy: modes.includes(\"legacy\"),\n avoidAll: modes.includes(\"avoid-all\"),\n };\n if (options.pagebreak.before !== undefined) pageBreak.before = options.pagebreak.before;\n if (options.pagebreak.after !== undefined) pageBreak.after = options.pagebreak.after;\n if (options.pagebreak.avoid !== undefined) pageBreak.avoid = options.pagebreak.avoid;\n renderOptions.pageBreak = pageBreak;\n }\n return renderOptions;\n}\n\ntype StringPdfOutputType = Exclude<PdfOutputType, \"arraybuffer\" | \"blob\">;\n\nfunction blobToDataUrl(blob: Blob): Promise<string> {\n return new Promise((resolve, reject) => {\n const reader = new FileReader();\n reader.addEventListener(\"load\", () => resolve(String(reader.result)), { once: true });\n reader.addEventListener(\"error\", () => reject(reader.error), { once: true });\n reader.readAsDataURL(blob);\n });\n}\n"]}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable error classes exposed by the package root.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
/** Base error carrying a machine-readable wrapper error code. */
|
|
7
|
+
export declare class Html2RealPdfError extends Error {
|
|
8
|
+
/** Stable value suitable for control flow and telemetry. */
|
|
9
|
+
readonly code: string;
|
|
10
|
+
constructor(message: string,
|
|
11
|
+
/** Stable value suitable for control flow and telemetry. */
|
|
12
|
+
code: string, options?: ErrorOptions);
|
|
13
|
+
}
|
|
14
|
+
/** Rendering was requested where browser DOM APIs are unavailable. */
|
|
15
|
+
export declare class UnsupportedEnvironmentError extends Html2RealPdfError {
|
|
16
|
+
constructor(message?: string);
|
|
17
|
+
}
|
|
18
|
+
/** The source is not a string, mounted element, or non-null ref-like object. */
|
|
19
|
+
export declare class InvalidSourceError extends Html2RealPdfError {
|
|
20
|
+
constructor(message: string);
|
|
21
|
+
}
|
|
22
|
+
/** Authored CSS or SVG cannot satisfy the selected strictness policy. */
|
|
23
|
+
export declare class UnsupportedCssError extends Html2RealPdfError {
|
|
24
|
+
constructor(message: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The WASM bridge or native renderer failed.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* `status` preserves the native or bridge status code for logging and
|
|
31
|
+
* operational classification.
|
|
32
|
+
*/
|
|
33
|
+
export declare class WasmRenderError extends Html2RealPdfError {
|
|
34
|
+
/** Native renderer status or a negative bridge status. */
|
|
35
|
+
readonly status: number;
|
|
36
|
+
constructor(message: string,
|
|
37
|
+
/** Native renderer status or a negative bridge status. */
|
|
38
|
+
status: number, options?: ErrorOptions);
|
|
39
|
+
}
|
|
40
|
+
/** An image, stylesheet, canvas, or SVG resource could not be materialized. */
|
|
41
|
+
export declare class ResourceLoadError extends Html2RealPdfError {
|
|
42
|
+
constructor(resource: string, options?: ErrorOptions);
|
|
43
|
+
}
|
|
44
|
+
/** A canvas-to-SVG adapter threw or returned an invalid result. */
|
|
45
|
+
export declare class CanvasToSvgError extends Html2RealPdfError {
|
|
46
|
+
/** Snapshot path of the canvas whose adapter failed. */
|
|
47
|
+
readonly nodePath: string;
|
|
48
|
+
constructor(message: string,
|
|
49
|
+
/** Snapshot path of the canvas whose adapter failed. */
|
|
50
|
+
nodePath: string, options?: ErrorOptions);
|
|
51
|
+
}
|
|
52
|
+
/** A requested html2pdf.js stage belongs to its unsupported raster pipeline. */
|
|
53
|
+
export declare class UnsupportedCompatibilityFeatureError extends Html2RealPdfError {
|
|
54
|
+
constructor(feature: string);
|
|
55
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable error classes exposed by the package root.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
/** Base error carrying a machine-readable wrapper error code. */
|
|
7
|
+
export class Html2RealPdfError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
constructor(message,
|
|
10
|
+
/** Stable value suitable for control flow and telemetry. */
|
|
11
|
+
code, options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.name = new.target.name;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Rendering was requested where browser DOM APIs are unavailable. */
|
|
18
|
+
export class UnsupportedEnvironmentError extends Html2RealPdfError {
|
|
19
|
+
constructor(message = "html2realpdf rendering requires a browser environment") {
|
|
20
|
+
super(message, "UNSUPPORTED_ENVIRONMENT");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** The source is not a string, mounted element, or non-null ref-like object. */
|
|
24
|
+
export class InvalidSourceError extends Html2RealPdfError {
|
|
25
|
+
constructor(message) {
|
|
26
|
+
super(message, "INVALID_SOURCE");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Authored CSS or SVG cannot satisfy the selected strictness policy. */
|
|
30
|
+
export class UnsupportedCssError extends Html2RealPdfError {
|
|
31
|
+
constructor(message) {
|
|
32
|
+
super(message, "UNSUPPORTED_CSS");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The WASM bridge or native renderer failed.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* `status` preserves the native or bridge status code for logging and
|
|
40
|
+
* operational classification.
|
|
41
|
+
*/
|
|
42
|
+
export class WasmRenderError extends Html2RealPdfError {
|
|
43
|
+
status;
|
|
44
|
+
constructor(message,
|
|
45
|
+
/** Native renderer status or a negative bridge status. */
|
|
46
|
+
status, options) {
|
|
47
|
+
super(message, "WASM_RENDER_FAILED", options);
|
|
48
|
+
this.status = status;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** An image, stylesheet, canvas, or SVG resource could not be materialized. */
|
|
52
|
+
export class ResourceLoadError extends Html2RealPdfError {
|
|
53
|
+
constructor(resource, options) {
|
|
54
|
+
super(`Could not load resource: ${resource}`, "RESOURCE_LOAD_FAILED", options);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** A canvas-to-SVG adapter threw or returned an invalid result. */
|
|
58
|
+
export class CanvasToSvgError extends Html2RealPdfError {
|
|
59
|
+
nodePath;
|
|
60
|
+
constructor(message,
|
|
61
|
+
/** Snapshot path of the canvas whose adapter failed. */
|
|
62
|
+
nodePath, options) {
|
|
63
|
+
super(`${message}: ${nodePath}`, "CANVAS_TO_SVG_FAILED", options);
|
|
64
|
+
this.nodePath = nodePath;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** A requested html2pdf.js stage belongs to its unsupported raster pipeline. */
|
|
68
|
+
export class UnsupportedCompatibilityFeatureError extends Html2RealPdfError {
|
|
69
|
+
constructor(feature) {
|
|
70
|
+
super(`${feature} belongs to the raster html2canvas/jsPDF pipeline and is not supported`, "UNSUPPORTED_COMPAT_FEATURE");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,iEAAiE;AACjE,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAI/B,IAAI;IAHf,YACE,OAAe;IACf,4DAA4D;IACnD,IAAY,EACrB,OAAsB;QAEtB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;oBAHf,IAAI;QAIb,IAAI,CAAC,IAAI,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC;IAC9B,CAAC;CACF;AAED,sEAAsE;AACtE,MAAM,OAAO,2BAA4B,SAAQ,iBAAiB;IAChE,YAAY,OAAO,GAAG,uDAAuD;QAC3E,KAAK,CAAC,OAAO,EAAE,yBAAyB,CAAC,CAAC;IAC5C,CAAC;CACF;AAED,gFAAgF;AAChF,MAAM,OAAO,kBAAmB,SAAQ,iBAAiB;IACvD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC;IACnC,CAAC;CACF;AAED,yEAAyE;AACzE,MAAM,OAAO,mBAAoB,SAAQ,iBAAiB;IACxD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IACpC,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,OAAO,eAAgB,SAAQ,iBAAiB;IAIzC,MAAM;IAHjB,YACE,OAAe;IACf,0DAA0D;IACjD,MAAc,EACvB,OAAsB;QAEtB,KAAK,CAAC,OAAO,EAAE,oBAAoB,EAAE,OAAO,CAAC,CAAC;sBAHrC,MAAM;IAIjB,CAAC;CACF;AAED,+EAA+E;AAC/E,MAAM,OAAO,iBAAkB,SAAQ,iBAAiB;IACtD,YAAY,QAAgB,EAAE,OAAsB;QAClD,KAAK,CAAC,4BAA4B,QAAQ,EAAE,EAAE,sBAAsB,EAAE,OAAO,CAAC,CAAC;IACjF,CAAC;CACF;AAED,mEAAmE;AACnE,MAAM,OAAO,gBAAiB,SAAQ,iBAAiB;IAI1C,QAAQ;IAHnB,YACE,OAAe;IACf,wDAAwD;IAC/C,QAAgB,EACzB,OAAsB;QAEtB,KAAK,CAAC,GAAG,OAAO,KAAK,QAAQ,EAAE,EAAE,sBAAsB,EAAE,OAAO,CAAC,CAAC;wBAHzD,QAAQ;IAInB,CAAC;CACF;AAED,gFAAgF;AAChF,MAAM,OAAO,oCAAqC,SAAQ,iBAAiB;IACzE,YAAY,OAAe;QACzB,KAAK,CAAC,GAAG,OAAO,wEAAwE,EAAE,4BAA4B,CAAC,CAAC;IAC1H,CAAC;CACF","sourcesContent":["/**\n * Stable error classes exposed by the package root.\n *\n * @packageDocumentation\n */\n\n/** Base error carrying a machine-readable wrapper error code. */\nexport class Html2RealPdfError extends Error {\n constructor(\n message: string,\n /** Stable value suitable for control flow and telemetry. */\n readonly code: string,\n options?: ErrorOptions,\n ) {\n super(message, options);\n this.name = new.target.name;\n }\n}\n\n/** Rendering was requested where browser DOM APIs are unavailable. */\nexport class UnsupportedEnvironmentError extends Html2RealPdfError {\n constructor(message = \"html2realpdf rendering requires a browser environment\") {\n super(message, \"UNSUPPORTED_ENVIRONMENT\");\n }\n}\n\n/** The source is not a string, mounted element, or non-null ref-like object. */\nexport class InvalidSourceError extends Html2RealPdfError {\n constructor(message: string) {\n super(message, \"INVALID_SOURCE\");\n }\n}\n\n/** Authored CSS or SVG cannot satisfy the selected strictness policy. */\nexport class UnsupportedCssError extends Html2RealPdfError {\n constructor(message: string) {\n super(message, \"UNSUPPORTED_CSS\");\n }\n}\n\n/**\n * The WASM bridge or native renderer failed.\n *\n * @remarks\n * `status` preserves the native or bridge status code for logging and\n * operational classification.\n */\nexport class WasmRenderError extends Html2RealPdfError {\n constructor(\n message: string,\n /** Native renderer status or a negative bridge status. */\n readonly status: number,\n options?: ErrorOptions,\n ) {\n super(message, \"WASM_RENDER_FAILED\", options);\n }\n}\n\n/** An image, stylesheet, canvas, or SVG resource could not be materialized. */\nexport class ResourceLoadError extends Html2RealPdfError {\n constructor(resource: string, options?: ErrorOptions) {\n super(`Could not load resource: ${resource}`, \"RESOURCE_LOAD_FAILED\", options);\n }\n}\n\n/** A canvas-to-SVG adapter threw or returned an invalid result. */\nexport class CanvasToSvgError extends Html2RealPdfError {\n constructor(\n message: string,\n /** Snapshot path of the canvas whose adapter failed. */\n readonly nodePath: string,\n options?: ErrorOptions,\n ) {\n super(`${message}: ${nodePath}`, \"CANVAS_TO_SVG_FAILED\", options);\n }\n}\n\n/** A requested html2pdf.js stage belongs to its unsupported raster pipeline. */\nexport class UnsupportedCompatibilityFeatureError extends Html2RealPdfError {\n constructor(feature: string) {\n super(`${feature} belongs to the raster html2canvas/jsPDF pipeline and is not supported`, \"UNSUPPORTED_COMPAT_FEATURE\");\n }\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-first HTML-to-PDF rendering with selectable text, native vectors,
|
|
3
|
+
* typed configuration, and an html2pdf.js compatibility layer.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
import html2pdf from "./compat.js";
|
|
8
|
+
export default html2pdf;
|
|
9
|
+
export { CompatWorker } from "./compat.js";
|
|
10
|
+
export * from "./errors.js";
|
|
11
|
+
export { PdfDocument } from "./pdf-document.js";
|
|
12
|
+
export { PdfPreview, type PdfPreviewOptions } from "./preview.js";
|
|
13
|
+
export { createRenderer, Html2RealPdf, renderPdf } from "./renderer.js";
|
|
14
|
+
export type * from "./types.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-first HTML-to-PDF rendering with selectable text, native vectors,
|
|
3
|
+
* typed configuration, and an html2pdf.js compatibility layer.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
import html2pdf from "./compat.js";
|
|
8
|
+
export default html2pdf;
|
|
9
|
+
export { CompatWorker } from "./compat.js";
|
|
10
|
+
export * from "./errors.js";
|
|
11
|
+
export { PdfDocument } from "./pdf-document.js";
|
|
12
|
+
export { PdfPreview } from "./preview.js";
|
|
13
|
+
export { createRenderer, Html2RealPdf, renderPdf } from "./renderer.js";
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,QAAQ,MAAM,aAAa,CAAC;AAEnC,eAAe,QAAQ,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,UAAU,EAA0B,MAAM,cAAc,CAAC;AAClE,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC","sourcesContent":["/**\n * Browser-first HTML-to-PDF rendering with selectable text, native vectors,\n * typed configuration, and an html2pdf.js compatibility layer.\n *\n * @packageDocumentation\n */\nimport html2pdf from \"./compat.js\";\n\nexport default html2pdf;\nexport { CompatWorker } from \"./compat.js\";\nexport * from \"./errors.js\";\nexport { PdfDocument } from \"./pdf-document.js\";\nexport { PdfPreview, type PdfPreviewOptions } from \"./preview.js\";\nexport { createRenderer, Html2RealPdf, renderPdf } from \"./renderer.js\";\nexport type * from \"./types.js\";\n"]}
|
|
Binary file
|
package/dist/page.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conversion of public page settings into the point-based WASM contract.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import type { PageOptions } from "./types.js";
|
|
7
|
+
/** Page box and margins normalized to PDF points in CSS edge order. */
|
|
8
|
+
export interface NormalizedPage {
|
|
9
|
+
widthPoints: number;
|
|
10
|
+
heightPoints: number;
|
|
11
|
+
marginTopPoints: number;
|
|
12
|
+
marginRightPoints: number;
|
|
13
|
+
marginBottomPoints: number;
|
|
14
|
+
marginLeftPoints: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Resolves named or custom page geometry and validates a positive content box.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* Named formats retain their physical size regardless of `unit`; the unit only
|
|
21
|
+
* scales custom dimensions and margins.
|
|
22
|
+
*/
|
|
23
|
+
export declare function normalizePage(options?: PageOptions): NormalizedPage;
|
package/dist/page.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conversion of public page settings into the point-based WASM contract.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
const PAGE_SIZES = {
|
|
7
|
+
a4: [595.2756, 841.8898],
|
|
8
|
+
letter: [612, 792],
|
|
9
|
+
};
|
|
10
|
+
const POINTS_PER_UNIT = {
|
|
11
|
+
pt: 1,
|
|
12
|
+
px: 0.75,
|
|
13
|
+
mm: 72 / 25.4,
|
|
14
|
+
cm: 72 / 2.54,
|
|
15
|
+
in: 72,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Resolves named or custom page geometry and validates a positive content box.
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* Named formats retain their physical size regardless of `unit`; the unit only
|
|
22
|
+
* scales custom dimensions and margins.
|
|
23
|
+
*/
|
|
24
|
+
export function normalizePage(options = {}) {
|
|
25
|
+
const unit = options.unit ?? "pt";
|
|
26
|
+
const scale = POINTS_PER_UNIT[unit];
|
|
27
|
+
const format = options.format ?? "a4";
|
|
28
|
+
const rawSize = typeof format === "string" ? PAGE_SIZES[format] : [format[0] * scale, format[1] * scale];
|
|
29
|
+
let width = rawSize[0];
|
|
30
|
+
let height = rawSize[1];
|
|
31
|
+
if ((options.orientation ?? "portrait") === "landscape")
|
|
32
|
+
[width, height] = [height, width];
|
|
33
|
+
const [top, right, bottom, left] = normalizeMargins(options.margin ?? 0).map((value) => value * scale);
|
|
34
|
+
if (top + bottom >= height || left + right >= width) {
|
|
35
|
+
throw new RangeError("Page margins must leave a positive content area");
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
widthPoints: width,
|
|
39
|
+
heightPoints: height,
|
|
40
|
+
marginTopPoints: top,
|
|
41
|
+
marginRightPoints: right,
|
|
42
|
+
marginBottomPoints: bottom,
|
|
43
|
+
marginLeftPoints: left,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function normalizeMargins(margin) {
|
|
47
|
+
if (typeof margin === "number")
|
|
48
|
+
return [margin, margin, margin, margin];
|
|
49
|
+
if (margin.length === 2)
|
|
50
|
+
return [margin[0], margin[1], margin[0], margin[1]];
|
|
51
|
+
// html2pdf.js uses [top, left, bottom, right]. Normalize to CSS edge order.
|
|
52
|
+
return [margin[0], margin[3], margin[2], margin[1]];
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=page.js.map
|
package/dist/page.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAcH,MAAM,UAAU,GAAG;IACjB,EAAE,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC;IACxB,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;CACV,CAAC;AAEX,MAAM,eAAe,GAA+B;IAClD,EAAE,EAAE,CAAC;IACL,EAAE,EAAE,IAAI;IACR,EAAE,EAAE,EAAE,GAAG,IAAI;IACb,EAAE,EAAE,EAAE,GAAG,IAAI;IACb,EAAE,EAAE,EAAE;CACP,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,OAAO,GAAgB,EAAE;IACrD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC;IAClC,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IACpC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC;IACtC,MAAM,OAAO,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;IACzG,IAAI,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IACvB,IAAI,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IACxB,IAAI,CAAC,OAAO,CAAC,WAAW,IAAI,UAAU,CAAC,KAAK,WAAW;QAAE,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAE3F,MAAM,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,gBAAgB,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,CAAqC,CAAC;IAC3I,IAAI,GAAG,GAAG,MAAM,IAAI,MAAM,IAAI,IAAI,GAAG,KAAK,IAAI,KAAK,EAAE,CAAC;QACpD,MAAM,IAAI,UAAU,CAAC,iDAAiD,CAAC,CAAC;IAC1E,CAAC;IAED,OAAO;QACL,WAAW,EAAE,KAAK;QAClB,YAAY,EAAE,MAAM;QACpB,eAAe,EAAE,GAAG;QACpB,iBAAiB,EAAE,KAAK;QACxB,kBAAkB,EAAE,MAAM;QAC1B,gBAAgB,EAAE,IAAI;KACvB,CAAC;AACJ,CAAC;AAED,SAAS,gBAAgB,CAAC,MAAc;IACtC,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACxE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7E,4EAA4E;IAC5E,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AACtD,CAAC","sourcesContent":["/**\n * Conversion of public page settings into the point-based WASM contract.\n *\n * @packageDocumentation\n */\n\nimport type { LengthUnit, Margin, PageOptions } from \"./types.js\";\n\n/** Page box and margins normalized to PDF points in CSS edge order. */\nexport interface NormalizedPage {\n widthPoints: number;\n heightPoints: number;\n marginTopPoints: number;\n marginRightPoints: number;\n marginBottomPoints: number;\n marginLeftPoints: number;\n}\n\nconst PAGE_SIZES = {\n a4: [595.2756, 841.8898],\n letter: [612, 792],\n} as const;\n\nconst POINTS_PER_UNIT: Record<LengthUnit, number> = {\n pt: 1,\n px: 0.75,\n mm: 72 / 25.4,\n cm: 72 / 2.54,\n in: 72,\n};\n\n/**\n * Resolves named or custom page geometry and validates a positive content box.\n *\n * @remarks\n * Named formats retain their physical size regardless of `unit`; the unit only\n * scales custom dimensions and margins.\n */\nexport function normalizePage(options: PageOptions = {}): NormalizedPage {\n const unit = options.unit ?? \"pt\";\n const scale = POINTS_PER_UNIT[unit];\n const format = options.format ?? \"a4\";\n const rawSize = typeof format === \"string\" ? PAGE_SIZES[format] : [format[0] * scale, format[1] * scale];\n let width = rawSize[0];\n let height = rawSize[1];\n if ((options.orientation ?? \"portrait\") === \"landscape\") [width, height] = [height, width];\n\n const [top, right, bottom, left] = normalizeMargins(options.margin ?? 0).map((value) => value * scale) as [number, number, number, number];\n if (top + bottom >= height || left + right >= width) {\n throw new RangeError(\"Page margins must leave a positive content area\");\n }\n\n return {\n widthPoints: width,\n heightPoints: height,\n marginTopPoints: top,\n marginRightPoints: right,\n marginBottomPoints: bottom,\n marginLeftPoints: left,\n };\n}\n\nfunction normalizeMargins(margin: Margin): [number, number, number, number] {\n if (typeof margin === \"number\") return [margin, margin, margin, margin];\n if (margin.length === 2) return [margin[0], margin[1], margin[0], margin[1]];\n // html2pdf.js uses [top, left, bottom, right]. Normalize to CSS edge order.\n return [margin[0], margin[3], margin[2], margin[1]];\n}\n"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Immutable PDF output and ownership of previews and object URLs derived from it.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import type { Diagnostic } from "./types.js";
|
|
7
|
+
import { PdfPreview, type PdfPreviewOptions } from "./preview.js";
|
|
8
|
+
/**
|
|
9
|
+
* Successful PDF output returned by the renderer.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Byte exports are defensive copies. The document owns previews and object URLs
|
|
13
|
+
* created through it; `dispose` releases them and invalidates future exports.
|
|
14
|
+
* Instances are renderer-owned results and cannot be constructed directly.
|
|
15
|
+
*/
|
|
16
|
+
export declare class PdfDocument {
|
|
17
|
+
private readonly data;
|
|
18
|
+
/** Number of pages reported by the native renderer. */
|
|
19
|
+
readonly pageCount: number;
|
|
20
|
+
/** Diagnostics retained from both browser snapshotting and native rendering. */
|
|
21
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
22
|
+
private readonly objectUrls;
|
|
23
|
+
private readonly previews;
|
|
24
|
+
private disposed;
|
|
25
|
+
private constructor();
|
|
26
|
+
/** Returns a defensive copy of the PDF bytes. */
|
|
27
|
+
toUint8Array(): Uint8Array;
|
|
28
|
+
/** Returns a standalone copy suitable for storage or transfer APIs. */
|
|
29
|
+
toArrayBuffer(): ArrayBuffer;
|
|
30
|
+
/** Returns a new `application/pdf` Blob. */
|
|
31
|
+
toBlob(): Blob;
|
|
32
|
+
/**
|
|
33
|
+
* Creates an object URL tracked by this document.
|
|
34
|
+
*
|
|
35
|
+
* @remarks Revoke long-lived URLs explicitly or let `dispose` revoke them.
|
|
36
|
+
*/
|
|
37
|
+
createObjectURL(): string;
|
|
38
|
+
/** Revokes an object URL previously created by this document. */
|
|
39
|
+
revokeObjectURL(url: string): void;
|
|
40
|
+
/**
|
|
41
|
+
* Replaces a target's contents with the package canvas preview.
|
|
42
|
+
*
|
|
43
|
+
* @remarks The returned preview is owned by this document until separately disposed.
|
|
44
|
+
*/
|
|
45
|
+
preview(target: HTMLElement, options?: PdfPreviewOptions): Promise<PdfPreview>;
|
|
46
|
+
/** Starts a browser download and revokes its temporary object URL. */
|
|
47
|
+
download(filename?: string): void;
|
|
48
|
+
/** Disposes owned previews, revokes object URLs, and invalidates byte exports. */
|
|
49
|
+
dispose(): void;
|
|
50
|
+
private assertActive;
|
|
51
|
+
}
|