@formepdf/core 0.16.0 → 0.18.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.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * PDF attachments + Factur-X/ZUGFeRD e-invoice container options.
3
+ *
4
+ * Environment-neutral (Node, browser, workers): used by every entry
5
+ * point so the option mapping cannot drift between them.
6
+ *
7
+ * Tier-1 container support only: the caller supplies the invoice XML;
8
+ * Forme embeds it as a PDF/A-3 associated file with the Factur-X XMP
9
+ * identification. Forme does NOT generate or validate EN 16931 semantic
10
+ * content.
11
+ */
12
+ /** `/AFRelationship` values (PDF 2.0 §14.13, required by PDF/A-3). */
13
+ export type AfRelationship = 'Data' | 'Source' | 'Alternative' | 'Supplement' | 'Unspecified';
14
+ export interface AttachmentOptions {
15
+ /** Filename recorded in the PDF (e.g. `factur-x.xml`). */
16
+ name: string;
17
+ /** File content: bytes, or a string treated as UTF-8 text. */
18
+ data: Uint8Array | string;
19
+ /** MIME type (PDF/A-3 requires one); default `application/octet-stream`. */
20
+ mimeType?: string;
21
+ /** Default `Unspecified` (the Factur-X path derives it from the profile). */
22
+ relationship?: AfRelationship;
23
+ /** Optional human-readable description. */
24
+ description?: string;
25
+ /**
26
+ * `/Params /ModDate` as a PDF date string (`D:YYYYMMDDHHmmSSZ`).
27
+ * Defaults to a fixed constant — never wall-clock — so output stays
28
+ * byte-deterministic.
29
+ */
30
+ modDate?: string;
31
+ }
32
+ /** Factur-X / ZUGFeRD profile names, exactly as XMP spells them. */
33
+ export type FacturXProfile = 'MINIMUM' | 'BASIC WL' | 'BASIC' | 'EN 16931' | 'EXTENDED' | 'XRECHNUNG';
34
+ export interface FacturXOptions {
35
+ /** The invoice XML (caller-supplied; Forme does not generate or validate it). */
36
+ xml: Uint8Array | string;
37
+ /** Factur-X profile — drives XMP `fx:ConformanceLevel` and the default `/AFRelationship`. */
38
+ profile: FacturXProfile;
39
+ /** Attachment filename; default `factur-x.xml` (`xrechnung.xml` for XRECHNUNG). */
40
+ filename?: string;
41
+ /** Override the profile-derived `/AFRelationship`. */
42
+ relationship?: AfRelationship;
43
+ /** XMP `fx:Version`; default `1.0`. */
44
+ version?: string;
45
+ /** `/Params /ModDate`; deterministic default. */
46
+ modDate?: string;
47
+ }
48
+ /**
49
+ * Map `attachments` / `facturX` options onto the serialized document.
50
+ * Shared by the Node, browser, and worker entries.
51
+ */
52
+ export declare function applyAttachmentOptions(doc: Record<string, unknown>, options?: {
53
+ attachments?: AttachmentOptions[];
54
+ facturX?: FacturXOptions;
55
+ }): void;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * PDF attachments + Factur-X/ZUGFeRD e-invoice container options.
3
+ *
4
+ * Environment-neutral (Node, browser, workers): used by every entry
5
+ * point so the option mapping cannot drift between them.
6
+ *
7
+ * Tier-1 container support only: the caller supplies the invoice XML;
8
+ * Forme embeds it as a PDF/A-3 associated file with the Factur-X XMP
9
+ * identification. Forme does NOT generate or validate EN 16931 semantic
10
+ * content.
11
+ */
12
+ function toBase64(data) {
13
+ const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
14
+ // Node fast path; browser/workers fall back to btoa over a binary string.
15
+ const B = globalThis.Buffer;
16
+ if (B)
17
+ return B.from(bytes).toString('base64');
18
+ let bin = '';
19
+ for (let i = 0; i < bytes.length; i += 0x8000) {
20
+ bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
21
+ }
22
+ return btoa(bin);
23
+ }
24
+ /**
25
+ * Map `attachments` / `facturX` options onto the serialized document.
26
+ * Shared by the Node, browser, and worker entries.
27
+ */
28
+ export function applyAttachmentOptions(doc, options) {
29
+ const attachments = [];
30
+ for (const a of options?.attachments ?? []) {
31
+ attachments.push({
32
+ name: a.name,
33
+ src: toBase64(a.data),
34
+ mimeType: a.mimeType,
35
+ relationship: a.relationship,
36
+ description: a.description,
37
+ modDate: a.modDate,
38
+ });
39
+ }
40
+ const fx = options?.facturX;
41
+ if (fx) {
42
+ const filename = fx.filename ?? (fx.profile === 'XRECHNUNG' ? 'xrechnung.xml' : 'factur-x.xml');
43
+ attachments.push({
44
+ name: filename,
45
+ src: toBase64(fx.xml),
46
+ mimeType: 'text/xml',
47
+ relationship: fx.relationship,
48
+ modDate: fx.modDate,
49
+ });
50
+ doc.zugferd = {
51
+ conformanceLevel: fx.profile,
52
+ documentFileName: filename,
53
+ version: fx.version,
54
+ };
55
+ }
56
+ if (attachments.length > 0) {
57
+ doc.attachments = attachments;
58
+ }
59
+ }
package/dist/browser.d.ts CHANGED
@@ -73,3 +73,4 @@ export declare function redactText(pdfBytes: Uint8Array, patterns: RedactionPatt
73
73
  export declare function mergePdfs(pdfs: Uint8Array[]): Promise<Uint8Array>;
74
74
  /** Extract embedded JSON data from a Forme-generated PDF. */
75
75
  export declare function extractData(pdfBytes: Uint8Array): Promise<unknown | null>;
76
+ export type { AttachmentOptions, FacturXOptions, AfRelationship, FacturXProfile } from './attachments.js';
package/dist/browser.js CHANGED
@@ -12,6 +12,7 @@
12
12
  */
13
13
  import { certify_pdf as wasmCertifyPdf, find_text_regions as wasmFindTextRegions, merge_pdfs as wasmMergePdfs, redact_pdf as wasmRedactPdf, redact_text as wasmRedactText, render_pdf as wasmRenderPdf, render_pdf_with_layout as wasmRenderPdfWithLayout, render_template_pdf as wasmRenderTemplatePdf, render_template_pdf_with_layout as wasmRenderTemplatePdfWithLayout, } from '../pkg/forme.js';
14
14
  import { resolveFonts, resolveImages, extractDataFromPdf, } from './shared/browserHelpers.js';
15
+ import { applyAttachmentOptions } from './attachments.js';
15
16
  // ── WASM initialization ────────────────────────────────────────────
16
17
  //
17
18
  // Kept as a no-op for backward compatibility. The bundler-target build
@@ -44,6 +45,7 @@ export async function renderDocument(element, options) {
44
45
  if (options?.flattenForms) {
45
46
  doc.flattenForms = true;
46
47
  }
48
+ applyAttachmentOptions(doc, options);
47
49
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
48
50
  return renderPdf(JSON.stringify(doc));
49
51
  }
@@ -56,6 +58,7 @@ export async function renderDocumentWithLayout(element, options) {
56
58
  if (options?.flattenForms) {
57
59
  doc.flattenForms = true;
58
60
  }
61
+ applyAttachmentOptions(doc, options);
59
62
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
60
63
  return renderPdfWithLayout(JSON.stringify(doc));
61
64
  }
@@ -75,6 +78,7 @@ export async function renderSerializedDoc(doc, options) {
75
78
  if (options?.flattenForms) {
76
79
  doc.flattenForms = true;
77
80
  }
81
+ applyAttachmentOptions(doc, options);
78
82
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
79
83
  return renderPdf(JSON.stringify(doc));
80
84
  }
@@ -88,6 +92,7 @@ export async function renderSerializedDocWithLayout(doc, options) {
88
92
  if (options?.flattenForms) {
89
93
  doc.flattenForms = true;
90
94
  }
95
+ applyAttachmentOptions(doc, options);
91
96
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
92
97
  return renderPdfWithLayout(JSON.stringify(doc));
93
98
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { ReactElement } from 'react';
2
+ import { type AttachmentOptions, type FacturXOptions } from './attachments.js';
2
3
  export interface Color {
3
4
  r: number;
4
5
  g: number;
@@ -223,6 +224,19 @@ export interface RenderDocumentOptions {
223
224
  embedData?: unknown;
224
225
  /** When true, form field values are rendered as static text. No interactive fields in output. */
225
226
  flattenForms?: boolean;
227
+ /**
228
+ * Files to embed as PDF attachments (associated files). Under a
229
+ * PDF/A-3 level each becomes a conformant associated file; PDF/A-2
230
+ * refuses attachments (it only allows other PDF/A files).
231
+ */
232
+ attachments?: AttachmentOptions[];
233
+ /**
234
+ * Factur-X / ZUGFeRD e-invoice container: embeds the caller-supplied
235
+ * invoice XML with the spec filename, MIME type, `/AFRelationship`,
236
+ * and XMP identification. Requires `pdfa: "3b"` (or 3a/3u) on the
237
+ * `<Document>`. Forme does not generate or validate the XML itself.
238
+ */
239
+ facturX?: FacturXOptions;
226
240
  }
227
241
  export declare function renderDocument(element: ReactElement, options?: RenderDocumentOptions): Promise<Uint8Array>;
228
242
  export declare function renderDocumentWithLayout(element: ReactElement, options?: RenderDocumentOptions): Promise<RenderWithLayoutResult>;
@@ -294,3 +308,4 @@ export declare function redactText(pdfBytes: Uint8Array, patterns: RedactionPatt
294
308
  */
295
309
  export declare function mergePdfs(pdfs: Uint8Array[]): Promise<Uint8Array>;
296
310
  export { extractData } from './extract.js';
311
+ export type { AttachmentOptions, FacturXOptions, AfRelationship, FacturXProfile } from './attachments.js';
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@
4
4
  import { render_pdf as wasmRenderPdf } from '../pkg-node/forme.js';
5
5
  import { readFile } from 'node:fs/promises';
6
6
  import { resolve } from 'node:path';
7
+ import { applyAttachmentOptions } from './attachments.js';
7
8
  // ── Font resolution ──────────────────────────────────────────────
8
9
  function uint8ArrayToBase64(bytes) {
9
10
  return Buffer.from(bytes).toString('base64');
@@ -90,6 +91,7 @@ export async function renderSerializedDoc(doc, options) {
90
91
  if (options?.flattenForms) {
91
92
  doc.flattenForms = true;
92
93
  }
94
+ applyAttachmentOptions(doc, options);
93
95
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
94
96
  return renderPdf(JSON.stringify(doc));
95
97
  }
@@ -103,6 +105,7 @@ export async function renderSerializedDocWithLayout(doc, options) {
103
105
  if (options?.flattenForms) {
104
106
  doc.flattenForms = true;
105
107
  }
108
+ applyAttachmentOptions(doc, options);
106
109
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
107
110
  return renderPdfWithLayout(JSON.stringify(doc));
108
111
  }
package/dist/worker.d.ts CHANGED
@@ -66,3 +66,4 @@ export declare function redactText(pdfBytes: Uint8Array, patterns: RedactionPatt
66
66
  export declare function mergePdfs(pdfs: Uint8Array[]): Promise<Uint8Array>;
67
67
  /** Extract embedded JSON data from a Forme-generated PDF. */
68
68
  export declare function extractData(pdfBytes: Uint8Array): Promise<unknown | null>;
69
+ export type { AttachmentOptions, FacturXOptions, AfRelationship, FacturXProfile } from './attachments.js';
package/dist/worker.js CHANGED
@@ -26,6 +26,7 @@
26
26
  */
27
27
  import __wbg_init, { certify_pdf as wasmCertifyPdf, find_text_regions as wasmFindTextRegions, merge_pdfs as wasmMergePdfs, redact_pdf as wasmRedactPdf, redact_text as wasmRedactText, render_pdf as wasmRenderPdf, render_pdf_with_layout as wasmRenderPdfWithLayout, render_template_pdf as wasmRenderTemplatePdf, render_template_pdf_with_layout as wasmRenderTemplatePdfWithLayout, } from '../pkg-web/forme.js';
28
28
  import { resolveFonts, resolveImages, extractDataFromPdf, } from './shared/browserHelpers.js';
29
+ import { applyAttachmentOptions } from './attachments.js';
29
30
  // ── WASM initialization ────────────────────────────────────────────
30
31
  let initPromise = null;
31
32
  /**
@@ -72,6 +73,7 @@ export async function renderDocument(element, options) {
72
73
  if (options?.flattenForms) {
73
74
  doc.flattenForms = true;
74
75
  }
76
+ applyAttachmentOptions(doc, options);
75
77
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
76
78
  return renderPdf(JSON.stringify(doc));
77
79
  }
@@ -84,6 +86,7 @@ export async function renderDocumentWithLayout(element, options) {
84
86
  if (options?.flattenForms) {
85
87
  doc.flattenForms = true;
86
88
  }
89
+ applyAttachmentOptions(doc, options);
87
90
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
88
91
  return renderPdfWithLayout(JSON.stringify(doc));
89
92
  }
@@ -95,6 +98,7 @@ export async function renderSerializedDoc(doc, options) {
95
98
  if (options?.flattenForms) {
96
99
  doc.flattenForms = true;
97
100
  }
101
+ applyAttachmentOptions(doc, options);
98
102
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
99
103
  return renderPdf(JSON.stringify(doc));
100
104
  }
@@ -105,6 +109,7 @@ export async function renderSerializedDocWithLayout(doc, options) {
105
109
  if (options?.flattenForms) {
106
110
  doc.flattenForms = true;
107
111
  }
112
+ applyAttachmentOptions(doc, options);
108
113
  await Promise.all([resolveFonts(doc), resolveImages(doc)]);
109
114
  return renderPdfWithLayout(JSON.stringify(doc));
110
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formepdf/core",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
4
4
  "description": "WASM-powered PDF rendering engine for Forme",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -64,21 +64,21 @@
64
64
  "prepublishOnly": "npm run build && ./scripts/assert-tarball.sh"
65
65
  },
66
66
  "dependencies": {
67
- "@formepdf/react": "0.16.0"
67
+ "@formepdf/react": "0.18.0"
68
68
  },
69
69
  "peerDependencies": {
70
70
  "react": "^18 || ^19"
71
71
  },
72
72
  "devDependencies": {
73
- "@cloudflare/vitest-pool-workers": "^0.12.21",
74
- "@formepdf/templates": "0.16.0",
73
+ "@cloudflare/vitest-pool-workers": "^0.22.0",
74
+ "@formepdf/templates": "0.18.0",
75
75
  "@pdf-testkit/vitest": "^0.1.1",
76
76
  "@types/node": "^22.0.0",
77
77
  "@types/react": "^19.0.0",
78
78
  "react": "^19.0.0",
79
79
  "typescript": "^5.7.0",
80
- "vitest": "^3.0.0",
81
- "@formepdf/fonts-standard": "0.16.0"
80
+ "vitest": "^4.1.0",
81
+ "@formepdf/fonts-standard": "0.18.0"
82
82
  },
83
83
  "files": [
84
84
  "dist/",
package/pkg/forme_bg.wasm CHANGED
Binary file
package/pkg/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "forme-pdf",
3
3
  "type": "module",
4
4
  "description": "A page-native PDF rendering engine. Layout INTO pages, not onto an infinite canvas.",
5
- "version": "0.16.0",
5
+ "version": "0.18.0",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
Binary file
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "forme-pdf",
3
3
  "description": "A page-native PDF rendering engine. Layout INTO pages, not onto an infinite canvas.",
4
- "version": "0.16.0",
4
+ "version": "0.18.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
Binary file
@@ -2,7 +2,7 @@
2
2
  "name": "forme-pdf",
3
3
  "type": "module",
4
4
  "description": "A page-native PDF rendering engine. Layout INTO pages, not onto an infinite canvas.",
5
- "version": "0.16.0",
5
+ "version": "0.18.0",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",