@lpdfio/lpdf 0.0.3

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,368 @@
1
+ /* @ts-self-types="./lpdf.d.ts" */
2
+
3
+ class LpdfEngine {
4
+ __destroy_into_raw() {
5
+ const ptr = this.__wbg_ptr;
6
+ this.__wbg_ptr = 0;
7
+ LpdfEngineFinalization.unregister(this);
8
+ return ptr;
9
+ }
10
+ free() {
11
+ const ptr = this.__destroy_into_raw();
12
+ wasm.__wbg_lpdfengine_free(ptr, 0);
13
+ }
14
+ /**
15
+ * Remove any previously configured encryption.
16
+ */
17
+ clear_encryption() {
18
+ wasm.lpdfengine_clear_encryption(this.__wbg_ptr);
19
+ }
20
+ /**
21
+ * Register raw font bytes (TTF/OTF) for a custom font name.
22
+ * Call this once per font before calling `render_pdf`.
23
+ * Glyph advance-width metrics are extracted automatically from the font
24
+ * bytes so the layout engine can measure text accurately — no separate
25
+ * `set_font_metrics` call is required.
26
+ * @param {string} name
27
+ * @param {Uint8Array} bytes
28
+ */
29
+ load_font(name, bytes) {
30
+ const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
31
+ const len0 = WASM_VECTOR_LEN;
32
+ const ptr1 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
33
+ const len1 = WASM_VECTOR_LEN;
34
+ wasm.lpdfengine_load_font(this.__wbg_ptr, ptr0, len0, ptr1, len1);
35
+ }
36
+ /**
37
+ * Register raw image bytes (JPEG or PNG) for an image name.
38
+ * Call this for every image referenced by `<img name="…">` nodes.
39
+ * @param {string} name
40
+ * @param {Uint8Array} bytes
41
+ */
42
+ load_image(name, bytes) {
43
+ const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
44
+ const len0 = WASM_VECTOR_LEN;
45
+ const ptr1 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
46
+ const len1 = WASM_VECTOR_LEN;
47
+ wasm.lpdfengine_load_image(this.__wbg_ptr, ptr0, len0, ptr1, len1);
48
+ }
49
+ /**
50
+ * @param {string} license_key
51
+ */
52
+ constructor(license_key) {
53
+ const ptr0 = passStringToWasm0(license_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
54
+ const len0 = WASM_VECTOR_LEN;
55
+ const ret = wasm.lpdfengine_new(ptr0, len0);
56
+ this.__wbg_ptr = ret >>> 0;
57
+ LpdfEngineFinalization.register(this, this.__wbg_ptr, this);
58
+ return this;
59
+ }
60
+ /**
61
+ * @param {string} xml
62
+ * @returns {string}
63
+ */
64
+ render(xml) {
65
+ let deferred2_0;
66
+ let deferred2_1;
67
+ try {
68
+ const ptr0 = passStringToWasm0(xml, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
69
+ const len0 = WASM_VECTOR_LEN;
70
+ const ret = wasm.lpdfengine_render(this.__wbg_ptr, ptr0, len0);
71
+ deferred2_0 = ret[0];
72
+ deferred2_1 = ret[1];
73
+ return getStringFromWasm0(ret[0], ret[1]);
74
+ } finally {
75
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
76
+ }
77
+ }
78
+ /**
79
+ * Render `xml` to binary PDF bytes.
80
+ *
81
+ * Any custom fonts referenced in `<font src="…">` declarations must have
82
+ * their bytes registered via `load_font` before calling this method.
83
+ *
84
+ * `json_data` is an optional JSON string used to resolve `data-*`
85
+ * attributes in the template. Pass `None` (or `null` / `undefined` from
86
+ * JavaScript) to render the template with its inline fallback content.
87
+ * @param {string} xml
88
+ * @param {string | null} [json_data]
89
+ * @returns {Uint8Array}
90
+ */
91
+ render_pdf(xml, json_data) {
92
+ const ptr0 = passStringToWasm0(xml, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
93
+ const len0 = WASM_VECTOR_LEN;
94
+ var ptr1 = isLikeNone(json_data) ? 0 : passStringToWasm0(json_data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
95
+ var len1 = WASM_VECTOR_LEN;
96
+ const ret = wasm.lpdfengine_render_pdf(this.__wbg_ptr, ptr0, len0, ptr1, len1);
97
+ if (ret[3]) {
98
+ throw takeFromExternrefTable0(ret[2]);
99
+ }
100
+ var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
101
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
102
+ return v3;
103
+ }
104
+ /**
105
+ * @param {string} json
106
+ * @returns {string}
107
+ */
108
+ render_tree(json) {
109
+ let deferred2_0;
110
+ let deferred2_1;
111
+ try {
112
+ const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
113
+ const len0 = WASM_VECTOR_LEN;
114
+ const ret = wasm.lpdfengine_render_tree(this.__wbg_ptr, ptr0, len0);
115
+ deferred2_0 = ret[0];
116
+ deferred2_1 = ret[1];
117
+ return getStringFromWasm0(ret[0], ret[1]);
118
+ } finally {
119
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
120
+ }
121
+ }
122
+ /**
123
+ * Render a JSON kit-tree or canvas-tree document to PDF bytes.
124
+ *
125
+ * This is the JSON counterpart of `render_pdf`. The Node adapter uses it
126
+ * when an `LpdfDocument` Kit tree is passed to `renderPdf()`, avoiding an
127
+ * intermediate XML serialisation step. PHP, Python, and .NET adapters also
128
+ * use this entry point.
129
+ * @param {string} json
130
+ * @returns {Uint8Array}
131
+ */
132
+ render_tree_pdf(json) {
133
+ const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
134
+ const len0 = WASM_VECTOR_LEN;
135
+ const ret = wasm.lpdfengine_render_tree_pdf(this.__wbg_ptr, ptr0, len0);
136
+ if (ret[3]) {
137
+ throw takeFromExternrefTable0(ret[2]);
138
+ }
139
+ var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
140
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
141
+ return v2;
142
+ }
143
+ /**
144
+ * Set an optional ISO 8601 creation timestamp (e.g. `"2024-06-01T12:00:00"`).
145
+ * When provided, written as `/CreationDate` in the PDF info dictionary.
146
+ * Omitting this keeps builds reproducible (no embedded timestamp).
147
+ * @param {string} iso
148
+ */
149
+ set_created_on(iso) {
150
+ const ptr0 = passStringToWasm0(iso, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
151
+ const len0 = WASM_VECTOR_LEN;
152
+ wasm.lpdfengine_set_created_on(this.__wbg_ptr, ptr0, len0);
153
+ }
154
+ /**
155
+ * Configure RC4-128 encryption applied to every subsequent `render_pdf` call.
156
+ *
157
+ * `permissions_json` is a JSON object with boolean fields matching the
158
+ * `Permissions` struct (`print`, `modify`, `copy`, `annotate`, `fill_forms`,
159
+ * `accessibility`, `assemble`, `print_hq`). Omitted fields default to `true`.
160
+ *
161
+ * To apply permissions without an open password, pass an empty `user_password`
162
+ * and a non-empty `owner_password`.
163
+ * @param {string} user_password
164
+ * @param {string} owner_password
165
+ * @param {string} permissions_json
166
+ */
167
+ set_encryption(user_password, owner_password, permissions_json) {
168
+ const ptr0 = passStringToWasm0(user_password, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
169
+ const len0 = WASM_VECTOR_LEN;
170
+ const ptr1 = passStringToWasm0(owner_password, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
171
+ const len1 = WASM_VECTOR_LEN;
172
+ const ptr2 = passStringToWasm0(permissions_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
173
+ const len2 = WASM_VECTOR_LEN;
174
+ wasm.lpdfengine_set_encryption(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
175
+ }
176
+ /**
177
+ * Inject glyph advance-width tables for custom fonts.
178
+ *
179
+ * Call this *before* `render_pdf` / `render` when the document uses custom
180
+ * fonts (declared via `<font src="…"`). The adapter extracts these widths
181
+ * from the font binary and passes them as a JSON object:
182
+ *
183
+ * ```json
184
+ * { "fontName": { "default": 500, "ascii": [260, 285, …] } }
185
+ * ```
186
+ *
187
+ * `ascii` is a 95-element array for code points 32–126. `default` is used
188
+ * for code points outside that range. All values are in 1/1000 em units.
189
+ * @param {string} json
190
+ */
191
+ set_font_metrics(json) {
192
+ const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
193
+ const len0 = WASM_VECTOR_LEN;
194
+ wasm.lpdfengine_set_font_metrics(this.__wbg_ptr, ptr0, len0);
195
+ }
196
+ /**
197
+ * Set the current Unix timestamp (seconds) for license expiry checking.
198
+ * Must be called before `render_pdf` when using a time-limited token.
199
+ * If not set (default `0`), expiry is not checked.
200
+ * @param {bigint} unix
201
+ */
202
+ set_now(unix) {
203
+ wasm.lpdfengine_set_now(this.__wbg_ptr, unix);
204
+ }
205
+ }
206
+ if (Symbol.dispose) LpdfEngine.prototype[Symbol.dispose] = LpdfEngine.prototype.free;
207
+ exports.LpdfEngine = LpdfEngine;
208
+
209
+ /**
210
+ * Convert a JSON kit-tree (produced by `LpdfKit` in any adapter) to an lpdf
211
+ * XML string.
212
+ *
213
+ * Useful for debugging Kit-generated documents, saving them as `.xml` files,
214
+ * or feeding them into the XML render path. The output is equivalent to
215
+ * hand-authored XML and passes through `render_pdf` without modification.
216
+ * @param {string} json
217
+ * @returns {string}
218
+ */
219
+ function kit_to_xml(json) {
220
+ let deferred3_0;
221
+ let deferred3_1;
222
+ try {
223
+ const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
224
+ const len0 = WASM_VECTOR_LEN;
225
+ const ret = wasm.kit_to_xml(ptr0, len0);
226
+ var ptr2 = ret[0];
227
+ var len2 = ret[1];
228
+ if (ret[3]) {
229
+ ptr2 = 0; len2 = 0;
230
+ throw takeFromExternrefTable0(ret[2]);
231
+ }
232
+ deferred3_0 = ptr2;
233
+ deferred3_1 = len2;
234
+ return getStringFromWasm0(ptr2, len2);
235
+ } finally {
236
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
237
+ }
238
+ }
239
+ exports.kit_to_xml = kit_to_xml;
240
+ function __wbg_get_imports() {
241
+ const import0 = {
242
+ __proto__: null,
243
+ __wbg___wbindgen_throw_6b64449b9b9ed33c: function(arg0, arg1) {
244
+ throw new Error(getStringFromWasm0(arg0, arg1));
245
+ },
246
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
247
+ // Cast intrinsic for `Ref(String) -> Externref`.
248
+ const ret = getStringFromWasm0(arg0, arg1);
249
+ return ret;
250
+ },
251
+ __wbindgen_init_externref_table: function() {
252
+ const table = wasm.__wbindgen_externrefs;
253
+ const offset = table.grow(4);
254
+ table.set(0, undefined);
255
+ table.set(offset + 0, undefined);
256
+ table.set(offset + 1, null);
257
+ table.set(offset + 2, true);
258
+ table.set(offset + 3, false);
259
+ },
260
+ };
261
+ return {
262
+ __proto__: null,
263
+ "./lpdf_bg.js": import0,
264
+ };
265
+ }
266
+
267
+ const LpdfEngineFinalization = (typeof FinalizationRegistry === 'undefined')
268
+ ? { register: () => {}, unregister: () => {} }
269
+ : new FinalizationRegistry(ptr => wasm.__wbg_lpdfengine_free(ptr >>> 0, 1));
270
+
271
+ function getArrayU8FromWasm0(ptr, len) {
272
+ ptr = ptr >>> 0;
273
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
274
+ }
275
+
276
+ function getStringFromWasm0(ptr, len) {
277
+ ptr = ptr >>> 0;
278
+ return decodeText(ptr, len);
279
+ }
280
+
281
+ let cachedUint8ArrayMemory0 = null;
282
+ function getUint8ArrayMemory0() {
283
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
284
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
285
+ }
286
+ return cachedUint8ArrayMemory0;
287
+ }
288
+
289
+ function isLikeNone(x) {
290
+ return x === undefined || x === null;
291
+ }
292
+
293
+ function passArray8ToWasm0(arg, malloc) {
294
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
295
+ getUint8ArrayMemory0().set(arg, ptr / 1);
296
+ WASM_VECTOR_LEN = arg.length;
297
+ return ptr;
298
+ }
299
+
300
+ function passStringToWasm0(arg, malloc, realloc) {
301
+ if (realloc === undefined) {
302
+ const buf = cachedTextEncoder.encode(arg);
303
+ const ptr = malloc(buf.length, 1) >>> 0;
304
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
305
+ WASM_VECTOR_LEN = buf.length;
306
+ return ptr;
307
+ }
308
+
309
+ let len = arg.length;
310
+ let ptr = malloc(len, 1) >>> 0;
311
+
312
+ const mem = getUint8ArrayMemory0();
313
+
314
+ let offset = 0;
315
+
316
+ for (; offset < len; offset++) {
317
+ const code = arg.charCodeAt(offset);
318
+ if (code > 0x7F) break;
319
+ mem[ptr + offset] = code;
320
+ }
321
+ if (offset !== len) {
322
+ if (offset !== 0) {
323
+ arg = arg.slice(offset);
324
+ }
325
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
326
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
327
+ const ret = cachedTextEncoder.encodeInto(arg, view);
328
+
329
+ offset += ret.written;
330
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
331
+ }
332
+
333
+ WASM_VECTOR_LEN = offset;
334
+ return ptr;
335
+ }
336
+
337
+ function takeFromExternrefTable0(idx) {
338
+ const value = wasm.__wbindgen_externrefs.get(idx);
339
+ wasm.__externref_table_dealloc(idx);
340
+ return value;
341
+ }
342
+
343
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
344
+ cachedTextDecoder.decode();
345
+ function decodeText(ptr, len) {
346
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
347
+ }
348
+
349
+ const cachedTextEncoder = new TextEncoder();
350
+
351
+ if (!('encodeInto' in cachedTextEncoder)) {
352
+ cachedTextEncoder.encodeInto = function (arg, view) {
353
+ const buf = cachedTextEncoder.encode(arg);
354
+ view.set(buf);
355
+ return {
356
+ read: arg.length,
357
+ written: buf.length
358
+ };
359
+ };
360
+ }
361
+
362
+ let WASM_VECTOR_LEN = 0;
363
+
364
+ const wasmPath = `${__dirname}/lpdf_bg.wasm`;
365
+ const wasmBytes = require('fs').readFileSync(wasmPath);
366
+ const wasmModule = new WebAssembly.Module(wasmBytes);
367
+ let wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports;
368
+ wasm.__wbindgen_start();
Binary file
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@lpdfio/lpdf",
3
+ "version": "0.0.3",
4
+ "description": "Node.js adapter for lpdf — pixel-perfect, lightweight, and consistent PDF rendering",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ },
12
+ "./browser": {
13
+ "import": "./dist/browser.js",
14
+ "types": "./dist/browser.d.ts"
15
+ }
16
+ },
17
+ "scripts": {
18
+ "build": "tsc && node build-browser.mjs && node -e \"require('node:fs').cpSync('./wasm', './dist/wasm', {recursive:true})\"",
19
+ "build:node": "tsc",
20
+ "build:browser": "node build-browser.mjs",
21
+ "test": "node --test test/render.test.mjs"
22
+ },
23
+ "author": "lpdf.io",
24
+ "homepage": "https://lpdf.io",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/lpdfio/lpdf-js"
28
+ },
29
+ "keywords": [
30
+ "pdf",
31
+ "wasm",
32
+ "xml",
33
+ "document",
34
+ "lpdf"
35
+ ],
36
+ "license": "SEE LICENSE IN LICENSE",
37
+ "files": [
38
+ "dist/"
39
+ ],
40
+ "devDependencies": {
41
+ "@types/node": "^20.0.0",
42
+ "esbuild": "^0.25.0",
43
+ "typescript": "^5.0.0"
44
+ }
45
+ }