@formepdf/core 0.7.4 → 0.7.6
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/dist/extract.d.ts +8 -0
- package/dist/extract.js +56 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +10 -2
- package/package.json +2 -2
- package/pkg/forme_bg.wasm +0 -0
- package/pkg/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract embedded JSON data from a Forme-generated PDF.
|
|
3
|
+
*
|
|
4
|
+
* Scans the PDF bytes for a `forme-data.json` EmbeddedFile attachment,
|
|
5
|
+
* decompresses the stream, and parses the JSON. Returns `null` if the
|
|
6
|
+
* PDF doesn't contain embedded data (e.g. non-Forme PDF).
|
|
7
|
+
*/
|
|
8
|
+
export declare function extractData(pdfBytes: Uint8Array): unknown | null;
|
package/dist/extract.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { inflateSync } from 'node:zlib';
|
|
2
|
+
/**
|
|
3
|
+
* Extract embedded JSON data from a Forme-generated PDF.
|
|
4
|
+
*
|
|
5
|
+
* Scans the PDF bytes for a `forme-data.json` EmbeddedFile attachment,
|
|
6
|
+
* decompresses the stream, and parses the JSON. Returns `null` if the
|
|
7
|
+
* PDF doesn't contain embedded data (e.g. non-Forme PDF).
|
|
8
|
+
*/
|
|
9
|
+
export function extractData(pdfBytes) {
|
|
10
|
+
const text = new TextDecoder('latin1').decode(pdfBytes);
|
|
11
|
+
// Find the FileSpec referencing forme-data.json
|
|
12
|
+
const fsMatch = text.match(/\/F\s*\(forme-data\.json\)/);
|
|
13
|
+
if (!fsMatch)
|
|
14
|
+
return null;
|
|
15
|
+
// Extract the EmbeddedFile stream object number from /EF << /F N 0 R >>
|
|
16
|
+
// Search around the FileSpec location for the /EF reference
|
|
17
|
+
const fsStart = fsMatch.index;
|
|
18
|
+
const fsRegion = text.slice(Math.max(0, fsStart - 200), fsStart + 200);
|
|
19
|
+
const efMatch = fsRegion.match(/\/EF\s*<<\s*\/F\s+(\d+)\s+0\s+R\s*>>/);
|
|
20
|
+
if (!efMatch)
|
|
21
|
+
return null;
|
|
22
|
+
const streamObjId = efMatch[1];
|
|
23
|
+
// Find the stream object: "N 0 obj ... stream\n...\nendstream"
|
|
24
|
+
const objPattern = new RegExp(streamObjId + '\\s+0\\s+obj\\b');
|
|
25
|
+
const objMatch = text.match(objPattern);
|
|
26
|
+
if (!objMatch)
|
|
27
|
+
return null;
|
|
28
|
+
const objStart = objMatch.index;
|
|
29
|
+
// Find stream start (after "stream\r\n" or "stream\n")
|
|
30
|
+
const streamKeyword = text.indexOf('stream', objStart);
|
|
31
|
+
if (streamKeyword === -1)
|
|
32
|
+
return null;
|
|
33
|
+
let streamDataStart = streamKeyword + 6; // "stream".length
|
|
34
|
+
// Skip \r\n or \n after "stream"
|
|
35
|
+
if (pdfBytes[streamDataStart] === 0x0d)
|
|
36
|
+
streamDataStart++; // \r
|
|
37
|
+
if (pdfBytes[streamDataStart] === 0x0a)
|
|
38
|
+
streamDataStart++; // \n
|
|
39
|
+
// Find endstream
|
|
40
|
+
const endstreamPos = text.indexOf('\nendstream', streamDataStart);
|
|
41
|
+
if (endstreamPos === -1)
|
|
42
|
+
return null;
|
|
43
|
+
const compressedBytes = pdfBytes.slice(streamDataStart, endstreamPos);
|
|
44
|
+
// Check if FlateDecode is used
|
|
45
|
+
const objRegion = text.slice(objStart, streamKeyword);
|
|
46
|
+
const isCompressed = objRegion.includes('/FlateDecode');
|
|
47
|
+
let jsonBytes;
|
|
48
|
+
if (isCompressed) {
|
|
49
|
+
jsonBytes = inflateSync(Buffer.from(compressedBytes));
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
jsonBytes = compressedBytes;
|
|
53
|
+
}
|
|
54
|
+
const jsonString = new TextDecoder('utf-8').decode(jsonBytes);
|
|
55
|
+
return JSON.parse(jsonString);
|
|
56
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -72,7 +72,12 @@ export interface RenderWithLayoutResult {
|
|
|
72
72
|
}
|
|
73
73
|
export declare function renderPdf(json: string): Promise<Uint8Array>;
|
|
74
74
|
export declare function renderPdfWithLayout(json: string): Promise<RenderWithLayoutResult>;
|
|
75
|
-
export
|
|
76
|
-
|
|
75
|
+
export interface RenderDocumentOptions {
|
|
76
|
+
/** Data to embed as a hidden JSON attachment in the PDF. */
|
|
77
|
+
embedData?: unknown;
|
|
78
|
+
}
|
|
79
|
+
export declare function renderDocument(element: ReactElement, options?: RenderDocumentOptions): Promise<Uint8Array>;
|
|
80
|
+
export declare function renderDocumentWithLayout(element: ReactElement, options?: RenderDocumentOptions): Promise<RenderWithLayoutResult>;
|
|
77
81
|
export declare function renderTemplate(templateJson: string, dataJson: string): Promise<Uint8Array>;
|
|
78
82
|
export declare function renderTemplateWithLayout(templateJson: string, dataJson: string): Promise<RenderWithLayoutResult>;
|
|
83
|
+
export { extractData } from './extract.js';
|
package/dist/index.js
CHANGED
|
@@ -73,15 +73,21 @@ export async function renderPdfWithLayout(json) {
|
|
|
73
73
|
const result = render_pdf_with_layout(json);
|
|
74
74
|
return result;
|
|
75
75
|
}
|
|
76
|
-
export async function renderDocument(element) {
|
|
76
|
+
export async function renderDocument(element, options) {
|
|
77
77
|
const { serialize } = await import('@formepdf/react');
|
|
78
78
|
const doc = serialize(element);
|
|
79
|
+
if (options?.embedData !== undefined) {
|
|
80
|
+
doc.embeddedData = JSON.stringify(options.embedData);
|
|
81
|
+
}
|
|
79
82
|
await Promise.all([resolveFonts(doc), resolveImages(doc)]);
|
|
80
83
|
return renderPdf(JSON.stringify(doc));
|
|
81
84
|
}
|
|
82
|
-
export async function renderDocumentWithLayout(element) {
|
|
85
|
+
export async function renderDocumentWithLayout(element, options) {
|
|
83
86
|
const { serialize } = await import('@formepdf/react');
|
|
84
87
|
const doc = serialize(element);
|
|
88
|
+
if (options?.embedData !== undefined) {
|
|
89
|
+
doc.embeddedData = JSON.stringify(options.embedData);
|
|
90
|
+
}
|
|
85
91
|
await Promise.all([resolveFonts(doc), resolveImages(doc)]);
|
|
86
92
|
return renderPdfWithLayout(JSON.stringify(doc));
|
|
87
93
|
}
|
|
@@ -97,3 +103,5 @@ export async function renderTemplateWithLayout(templateJson, dataJson) {
|
|
|
97
103
|
const result = render_template_pdf_with_layout(templateJson, dataJson);
|
|
98
104
|
return result;
|
|
99
105
|
}
|
|
106
|
+
// ── Data extraction ──────────────────────────────────────────────────
|
|
107
|
+
export { extractData } from './extract.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formepdf/core",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.6",
|
|
4
4
|
"description": "WASM-powered PDF rendering engine for Forme",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"test": "vitest run"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@formepdf/react": "0.7.
|
|
26
|
+
"@formepdf/react": "0.7.6"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"react": "^18 || ^19"
|
package/pkg/forme_bg.wasm
CHANGED
|
Binary file
|