@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.
- package/LICENSE +131 -0
- package/README.md +60 -0
- package/dist/_shared.d.ts +28 -0
- package/dist/_shared.js +5 -0
- package/dist/browser.js +468 -0
- package/dist/canvas.d.ts +131 -0
- package/dist/canvas.js +181 -0
- package/dist/engine.d.ts +70 -0
- package/dist/engine.js +191 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +34 -0
- package/dist/kit-to-xml.d.ts +30 -0
- package/dist/kit-to-xml.js +37 -0
- package/dist/kit.d.ts +109 -0
- package/dist/kit.js +78 -0
- package/dist/layout.d.ts +260 -0
- package/dist/layout.js +131 -0
- package/dist/wasm/lpdf-web.js +460 -0
- package/dist/wasm/lpdf.d.ts +95 -0
- package/dist/wasm/lpdf.js +368 -0
- package/dist/wasm/lpdf_bg.wasm +0 -0
- package/package.json +45 -0
package/dist/browser.js
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
// wasm/lpdf-web.js
|
|
2
|
+
var LpdfEngine = class {
|
|
3
|
+
__destroy_into_raw() {
|
|
4
|
+
const ptr = this.__wbg_ptr;
|
|
5
|
+
this.__wbg_ptr = 0;
|
|
6
|
+
LpdfEngineFinalization.unregister(this);
|
|
7
|
+
return ptr;
|
|
8
|
+
}
|
|
9
|
+
free() {
|
|
10
|
+
const ptr = this.__destroy_into_raw();
|
|
11
|
+
wasm.__wbg_lpdfengine_free(ptr, 0);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Remove any previously configured encryption.
|
|
15
|
+
*/
|
|
16
|
+
clear_encryption() {
|
|
17
|
+
wasm.lpdfengine_clear_encryption(this.__wbg_ptr);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Register raw font bytes (TTF/OTF) for a custom font name.
|
|
21
|
+
* Call this once per font before calling `render_pdf`.
|
|
22
|
+
* Glyph advance-width metrics are extracted automatically from the font
|
|
23
|
+
* bytes so the layout engine can measure text accurately — no separate
|
|
24
|
+
* `set_font_metrics` call is required.
|
|
25
|
+
* @param {string} name
|
|
26
|
+
* @param {Uint8Array} bytes
|
|
27
|
+
*/
|
|
28
|
+
load_font(name, bytes) {
|
|
29
|
+
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
30
|
+
const len0 = WASM_VECTOR_LEN;
|
|
31
|
+
const ptr1 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
|
|
32
|
+
const len1 = WASM_VECTOR_LEN;
|
|
33
|
+
wasm.lpdfengine_load_font(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Register raw image bytes (JPEG or PNG) for an image name.
|
|
37
|
+
* Call this for every image referenced by `<img name="…">` nodes.
|
|
38
|
+
* @param {string} name
|
|
39
|
+
* @param {Uint8Array} bytes
|
|
40
|
+
*/
|
|
41
|
+
load_image(name, bytes) {
|
|
42
|
+
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
43
|
+
const len0 = WASM_VECTOR_LEN;
|
|
44
|
+
const ptr1 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
|
|
45
|
+
const len1 = WASM_VECTOR_LEN;
|
|
46
|
+
wasm.lpdfengine_load_image(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @param {string} license_key
|
|
50
|
+
*/
|
|
51
|
+
constructor(license_key) {
|
|
52
|
+
const ptr0 = passStringToWasm0(license_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
53
|
+
const len0 = WASM_VECTOR_LEN;
|
|
54
|
+
const ret = wasm.lpdfengine_new(ptr0, len0);
|
|
55
|
+
this.__wbg_ptr = ret >>> 0;
|
|
56
|
+
LpdfEngineFinalization.register(this, this.__wbg_ptr, this);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @param {string} xml
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
render(xml) {
|
|
64
|
+
let deferred2_0;
|
|
65
|
+
let deferred2_1;
|
|
66
|
+
try {
|
|
67
|
+
const ptr0 = passStringToWasm0(xml, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
68
|
+
const len0 = WASM_VECTOR_LEN;
|
|
69
|
+
const ret = wasm.lpdfengine_render(this.__wbg_ptr, ptr0, len0);
|
|
70
|
+
deferred2_0 = ret[0];
|
|
71
|
+
deferred2_1 = ret[1];
|
|
72
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
73
|
+
} finally {
|
|
74
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Render `xml` to binary PDF bytes.
|
|
79
|
+
*
|
|
80
|
+
* Any custom fonts referenced in `<font src="…">` declarations must have
|
|
81
|
+
* their bytes registered via `load_font` before calling this method.
|
|
82
|
+
*
|
|
83
|
+
* `json_data` is an optional JSON string used to resolve `data-*`
|
|
84
|
+
* attributes in the template. Pass `None` (or `null` / `undefined` from
|
|
85
|
+
* JavaScript) to render the template with its inline fallback content.
|
|
86
|
+
* @param {string} xml
|
|
87
|
+
* @param {string | null} [json_data]
|
|
88
|
+
* @returns {Uint8Array}
|
|
89
|
+
*/
|
|
90
|
+
render_pdf(xml, json_data) {
|
|
91
|
+
const ptr0 = passStringToWasm0(xml, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
92
|
+
const len0 = WASM_VECTOR_LEN;
|
|
93
|
+
var ptr1 = isLikeNone(json_data) ? 0 : passStringToWasm0(json_data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
94
|
+
var len1 = WASM_VECTOR_LEN;
|
|
95
|
+
const ret = wasm.lpdfengine_render_pdf(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
96
|
+
if (ret[3]) {
|
|
97
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
98
|
+
}
|
|
99
|
+
var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
100
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
101
|
+
return v3;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @param {string} json
|
|
105
|
+
* @returns {string}
|
|
106
|
+
*/
|
|
107
|
+
render_tree(json) {
|
|
108
|
+
let deferred2_0;
|
|
109
|
+
let deferred2_1;
|
|
110
|
+
try {
|
|
111
|
+
const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
112
|
+
const len0 = WASM_VECTOR_LEN;
|
|
113
|
+
const ret = wasm.lpdfengine_render_tree(this.__wbg_ptr, ptr0, len0);
|
|
114
|
+
deferred2_0 = ret[0];
|
|
115
|
+
deferred2_1 = ret[1];
|
|
116
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
117
|
+
} finally {
|
|
118
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Render a JSON kit-tree or canvas-tree document to PDF bytes.
|
|
123
|
+
*
|
|
124
|
+
* This is the JSON counterpart of `render_pdf`. The Node adapter uses it
|
|
125
|
+
* when an `LpdfDocument` Kit tree is passed to `renderPdf()`, avoiding an
|
|
126
|
+
* intermediate XML serialisation step. PHP, Python, and .NET adapters also
|
|
127
|
+
* use this entry point.
|
|
128
|
+
* @param {string} json
|
|
129
|
+
* @returns {Uint8Array}
|
|
130
|
+
*/
|
|
131
|
+
render_tree_pdf(json) {
|
|
132
|
+
const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
133
|
+
const len0 = WASM_VECTOR_LEN;
|
|
134
|
+
const ret = wasm.lpdfengine_render_tree_pdf(this.__wbg_ptr, ptr0, len0);
|
|
135
|
+
if (ret[3]) {
|
|
136
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
137
|
+
}
|
|
138
|
+
var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
139
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
140
|
+
return v2;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Set an optional ISO 8601 creation timestamp (e.g. `"2024-06-01T12:00:00"`).
|
|
144
|
+
* When provided, written as `/CreationDate` in the PDF info dictionary.
|
|
145
|
+
* Omitting this keeps builds reproducible (no embedded timestamp).
|
|
146
|
+
* @param {string} iso
|
|
147
|
+
*/
|
|
148
|
+
set_created_on(iso) {
|
|
149
|
+
const ptr0 = passStringToWasm0(iso, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
150
|
+
const len0 = WASM_VECTOR_LEN;
|
|
151
|
+
wasm.lpdfengine_set_created_on(this.__wbg_ptr, ptr0, len0);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Configure RC4-128 encryption applied to every subsequent `render_pdf` call.
|
|
155
|
+
*
|
|
156
|
+
* `permissions_json` is a JSON object with boolean fields matching the
|
|
157
|
+
* `Permissions` struct (`print`, `modify`, `copy`, `annotate`, `fill_forms`,
|
|
158
|
+
* `accessibility`, `assemble`, `print_hq`). Omitted fields default to `true`.
|
|
159
|
+
*
|
|
160
|
+
* To apply permissions without an open password, pass an empty `user_password`
|
|
161
|
+
* and a non-empty `owner_password`.
|
|
162
|
+
* @param {string} user_password
|
|
163
|
+
* @param {string} owner_password
|
|
164
|
+
* @param {string} permissions_json
|
|
165
|
+
*/
|
|
166
|
+
set_encryption(user_password, owner_password, permissions_json) {
|
|
167
|
+
const ptr0 = passStringToWasm0(user_password, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
168
|
+
const len0 = WASM_VECTOR_LEN;
|
|
169
|
+
const ptr1 = passStringToWasm0(owner_password, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
170
|
+
const len1 = WASM_VECTOR_LEN;
|
|
171
|
+
const ptr2 = passStringToWasm0(permissions_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
172
|
+
const len2 = WASM_VECTOR_LEN;
|
|
173
|
+
wasm.lpdfengine_set_encryption(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Inject glyph advance-width tables for custom fonts.
|
|
177
|
+
*
|
|
178
|
+
* Call this *before* `render_pdf` / `render` when the document uses custom
|
|
179
|
+
* fonts (declared via `<font src="…"`). The adapter extracts these widths
|
|
180
|
+
* from the font binary and passes them as a JSON object:
|
|
181
|
+
*
|
|
182
|
+
* ```json
|
|
183
|
+
* { "fontName": { "default": 500, "ascii": [260, 285, …] } }
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* `ascii` is a 95-element array for code points 32–126. `default` is used
|
|
187
|
+
* for code points outside that range. All values are in 1/1000 em units.
|
|
188
|
+
* @param {string} json
|
|
189
|
+
*/
|
|
190
|
+
set_font_metrics(json) {
|
|
191
|
+
const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
192
|
+
const len0 = WASM_VECTOR_LEN;
|
|
193
|
+
wasm.lpdfengine_set_font_metrics(this.__wbg_ptr, ptr0, len0);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Set the current Unix timestamp (seconds) for license expiry checking.
|
|
197
|
+
* Must be called before `render_pdf` when using a time-limited token.
|
|
198
|
+
* If not set (default `0`), expiry is not checked.
|
|
199
|
+
* @param {bigint} unix
|
|
200
|
+
*/
|
|
201
|
+
set_now(unix) {
|
|
202
|
+
wasm.lpdfengine_set_now(this.__wbg_ptr, unix);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
if (Symbol.dispose) LpdfEngine.prototype[Symbol.dispose] = LpdfEngine.prototype.free;
|
|
206
|
+
function __wbg_get_imports() {
|
|
207
|
+
const import0 = {
|
|
208
|
+
__proto__: null,
|
|
209
|
+
__wbg___wbindgen_throw_6b64449b9b9ed33c: function(arg0, arg1) {
|
|
210
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
211
|
+
},
|
|
212
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
213
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
214
|
+
return ret;
|
|
215
|
+
},
|
|
216
|
+
__wbindgen_init_externref_table: function() {
|
|
217
|
+
const table = wasm.__wbindgen_externrefs;
|
|
218
|
+
const offset = table.grow(4);
|
|
219
|
+
table.set(0, void 0);
|
|
220
|
+
table.set(offset + 0, void 0);
|
|
221
|
+
table.set(offset + 1, null);
|
|
222
|
+
table.set(offset + 2, true);
|
|
223
|
+
table.set(offset + 3, false);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
return {
|
|
227
|
+
__proto__: null,
|
|
228
|
+
"./lpdf_bg.js": import0
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
var LpdfEngineFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
|
|
232
|
+
}, unregister: () => {
|
|
233
|
+
} } : new FinalizationRegistry((ptr) => wasm.__wbg_lpdfengine_free(ptr >>> 0, 1));
|
|
234
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
235
|
+
ptr = ptr >>> 0;
|
|
236
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
237
|
+
}
|
|
238
|
+
function getStringFromWasm0(ptr, len) {
|
|
239
|
+
ptr = ptr >>> 0;
|
|
240
|
+
return decodeText(ptr, len);
|
|
241
|
+
}
|
|
242
|
+
var cachedUint8ArrayMemory0 = null;
|
|
243
|
+
function getUint8ArrayMemory0() {
|
|
244
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
245
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
246
|
+
}
|
|
247
|
+
return cachedUint8ArrayMemory0;
|
|
248
|
+
}
|
|
249
|
+
function isLikeNone(x) {
|
|
250
|
+
return x === void 0 || x === null;
|
|
251
|
+
}
|
|
252
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
253
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
254
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
255
|
+
WASM_VECTOR_LEN = arg.length;
|
|
256
|
+
return ptr;
|
|
257
|
+
}
|
|
258
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
259
|
+
if (realloc === void 0) {
|
|
260
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
261
|
+
const ptr2 = malloc(buf.length, 1) >>> 0;
|
|
262
|
+
getUint8ArrayMemory0().subarray(ptr2, ptr2 + buf.length).set(buf);
|
|
263
|
+
WASM_VECTOR_LEN = buf.length;
|
|
264
|
+
return ptr2;
|
|
265
|
+
}
|
|
266
|
+
let len = arg.length;
|
|
267
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
268
|
+
const mem = getUint8ArrayMemory0();
|
|
269
|
+
let offset = 0;
|
|
270
|
+
for (; offset < len; offset++) {
|
|
271
|
+
const code = arg.charCodeAt(offset);
|
|
272
|
+
if (code > 127) break;
|
|
273
|
+
mem[ptr + offset] = code;
|
|
274
|
+
}
|
|
275
|
+
if (offset !== len) {
|
|
276
|
+
if (offset !== 0) {
|
|
277
|
+
arg = arg.slice(offset);
|
|
278
|
+
}
|
|
279
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
280
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
281
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
282
|
+
offset += ret.written;
|
|
283
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
284
|
+
}
|
|
285
|
+
WASM_VECTOR_LEN = offset;
|
|
286
|
+
return ptr;
|
|
287
|
+
}
|
|
288
|
+
function takeFromExternrefTable0(idx) {
|
|
289
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
290
|
+
wasm.__externref_table_dealloc(idx);
|
|
291
|
+
return value;
|
|
292
|
+
}
|
|
293
|
+
var cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
|
|
294
|
+
cachedTextDecoder.decode();
|
|
295
|
+
var MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
296
|
+
var numBytesDecoded = 0;
|
|
297
|
+
function decodeText(ptr, len) {
|
|
298
|
+
numBytesDecoded += len;
|
|
299
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
300
|
+
cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
|
|
301
|
+
cachedTextDecoder.decode();
|
|
302
|
+
numBytesDecoded = len;
|
|
303
|
+
}
|
|
304
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
305
|
+
}
|
|
306
|
+
var cachedTextEncoder = new TextEncoder();
|
|
307
|
+
if (!("encodeInto" in cachedTextEncoder)) {
|
|
308
|
+
cachedTextEncoder.encodeInto = function(arg, view) {
|
|
309
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
310
|
+
view.set(buf);
|
|
311
|
+
return {
|
|
312
|
+
read: arg.length,
|
|
313
|
+
written: buf.length
|
|
314
|
+
};
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
var WASM_VECTOR_LEN = 0;
|
|
318
|
+
var wasmModule;
|
|
319
|
+
var wasm;
|
|
320
|
+
function __wbg_finalize_init(instance, module) {
|
|
321
|
+
wasm = instance.exports;
|
|
322
|
+
wasmModule = module;
|
|
323
|
+
cachedUint8ArrayMemory0 = null;
|
|
324
|
+
wasm.__wbindgen_start();
|
|
325
|
+
return wasm;
|
|
326
|
+
}
|
|
327
|
+
async function __wbg_load(module, imports) {
|
|
328
|
+
if (typeof Response === "function" && module instanceof Response) {
|
|
329
|
+
if (typeof WebAssembly.instantiateStreaming === "function") {
|
|
330
|
+
try {
|
|
331
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
332
|
+
} catch (e) {
|
|
333
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
334
|
+
if (validResponse && module.headers.get("Content-Type") !== "application/wasm") {
|
|
335
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
336
|
+
} else {
|
|
337
|
+
throw e;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const bytes = await module.arrayBuffer();
|
|
342
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
343
|
+
} else {
|
|
344
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
345
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
346
|
+
return { instance, module };
|
|
347
|
+
} else {
|
|
348
|
+
return instance;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
function expectedResponseType(type) {
|
|
352
|
+
switch (type) {
|
|
353
|
+
case "basic":
|
|
354
|
+
case "cors":
|
|
355
|
+
case "default":
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
async function __wbg_init(module_or_path) {
|
|
362
|
+
if (wasm !== void 0) return wasm;
|
|
363
|
+
if (module_or_path !== void 0) {
|
|
364
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
365
|
+
({ module_or_path } = module_or_path);
|
|
366
|
+
} else {
|
|
367
|
+
console.warn("using deprecated parameters for the initialization function; pass a single object instead");
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (module_or_path === void 0) {
|
|
371
|
+
module_or_path = new URL("lpdf_bg.wasm", import.meta.url);
|
|
372
|
+
}
|
|
373
|
+
const imports = __wbg_get_imports();
|
|
374
|
+
if (typeof module_or_path === "string" || typeof Request === "function" && module_or_path instanceof Request || typeof URL === "function" && module_or_path instanceof URL) {
|
|
375
|
+
module_or_path = fetch(module_or_path);
|
|
376
|
+
}
|
|
377
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
378
|
+
return __wbg_finalize_init(instance, module);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/kit.ts
|
|
382
|
+
function attrKey(camel) {
|
|
383
|
+
return camel.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase());
|
|
384
|
+
}
|
|
385
|
+
function buildAttrs(options) {
|
|
386
|
+
const result = {};
|
|
387
|
+
for (const [key, val] of Object.entries(options)) {
|
|
388
|
+
if (val !== void 0) {
|
|
389
|
+
result[attrKey(key)] = val;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return result;
|
|
393
|
+
}
|
|
394
|
+
function layout(nodes = []) {
|
|
395
|
+
return { type: "layout", nodes };
|
|
396
|
+
}
|
|
397
|
+
function canvas(layers = []) {
|
|
398
|
+
return { type: "canvas", nodes: layers };
|
|
399
|
+
}
|
|
400
|
+
function section(input = {}) {
|
|
401
|
+
return {
|
|
402
|
+
type: "section",
|
|
403
|
+
attrs: buildAttrs(input.options ?? {}),
|
|
404
|
+
nodes: input.nodes ?? []
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function document(input = {}) {
|
|
408
|
+
const { tokens, meta, ...restOpts } = input.options ?? {};
|
|
409
|
+
const attrs = {
|
|
410
|
+
...buildAttrs(restOpts)
|
|
411
|
+
};
|
|
412
|
+
if (tokens !== void 0) attrs["tokens"] = tokens;
|
|
413
|
+
if (meta !== void 0) attrs["meta"] = meta;
|
|
414
|
+
return {
|
|
415
|
+
version: 1,
|
|
416
|
+
type: "document",
|
|
417
|
+
attrs,
|
|
418
|
+
nodes: input.sections ?? []
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
var LpdfKit = Object.freeze({
|
|
422
|
+
layout,
|
|
423
|
+
canvas,
|
|
424
|
+
section,
|
|
425
|
+
document
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
// src/browser.ts
|
|
429
|
+
async function initLpdf(wasmSource, licenseKey = "", initOptions = {}) {
|
|
430
|
+
await __wbg_init(wasmSource);
|
|
431
|
+
const fontMap = /* @__PURE__ */ new Map();
|
|
432
|
+
const imageMap = /* @__PURE__ */ new Map();
|
|
433
|
+
if (initOptions.fontBytes) {
|
|
434
|
+
for (const [name, bytes] of Object.entries(initOptions.fontBytes)) {
|
|
435
|
+
fontMap.set(name, bytes);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return {
|
|
439
|
+
loadFont(name, bytes) {
|
|
440
|
+
fontMap.set(name, bytes);
|
|
441
|
+
},
|
|
442
|
+
loadImage(name, bytes) {
|
|
443
|
+
imageMap.set(name, bytes);
|
|
444
|
+
},
|
|
445
|
+
async renderPdf(xml, callOptions = {}) {
|
|
446
|
+
const engine = new LpdfEngine(licenseKey);
|
|
447
|
+
for (const [name, bytes] of fontMap) {
|
|
448
|
+
engine.load_font(name, bytes);
|
|
449
|
+
}
|
|
450
|
+
if (callOptions.fontBytes) {
|
|
451
|
+
for (const [name, bytes] of Object.entries(callOptions.fontBytes)) {
|
|
452
|
+
if (!fontMap.has(name)) engine.load_font(name, bytes);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
for (const [name, bytes] of imageMap) {
|
|
456
|
+
engine.load_image(name, bytes);
|
|
457
|
+
}
|
|
458
|
+
const dataJson = callOptions.data != null ? JSON.stringify(callOptions.data) : null;
|
|
459
|
+
const pdf = engine.render_pdf(xml, dataJson);
|
|
460
|
+
engine.free();
|
|
461
|
+
return pdf;
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
export {
|
|
466
|
+
LpdfKit,
|
|
467
|
+
initLpdf
|
|
468
|
+
};
|
package/dist/canvas.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { PageScope } from './_shared';
|
|
2
|
+
export type TextAlign = 'left' | 'center' | 'right' | 'justify';
|
|
3
|
+
export type LineCap = 'butt' | 'round' | 'square';
|
|
4
|
+
export type LineJoin = 'miter' | 'round' | 'bevel';
|
|
5
|
+
export interface CanvasRectStyle {
|
|
6
|
+
fill?: string;
|
|
7
|
+
stroke?: string;
|
|
8
|
+
strokeWidth?: number;
|
|
9
|
+
strokeDash?: number[];
|
|
10
|
+
borderRadius?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface CanvasLineStyle {
|
|
13
|
+
stroke?: string;
|
|
14
|
+
strokeWidth?: number;
|
|
15
|
+
strokeDash?: number[];
|
|
16
|
+
lineCap?: LineCap;
|
|
17
|
+
lineJoin?: LineJoin;
|
|
18
|
+
}
|
|
19
|
+
export interface CanvasEllipseStyle {
|
|
20
|
+
fill?: string;
|
|
21
|
+
stroke?: string;
|
|
22
|
+
strokeWidth?: number;
|
|
23
|
+
strokeDash?: number[];
|
|
24
|
+
}
|
|
25
|
+
export interface CanvasPathStyle {
|
|
26
|
+
fill?: string;
|
|
27
|
+
stroke?: string;
|
|
28
|
+
strokeWidth?: number;
|
|
29
|
+
strokeDash?: number[];
|
|
30
|
+
fillRuleEvenodd?: boolean;
|
|
31
|
+
lineCap?: LineCap;
|
|
32
|
+
lineJoin?: LineJoin;
|
|
33
|
+
}
|
|
34
|
+
export interface CanvasTextStyle {
|
|
35
|
+
font?: string;
|
|
36
|
+
size?: number;
|
|
37
|
+
color?: string;
|
|
38
|
+
align?: TextAlign;
|
|
39
|
+
lineHeight?: number;
|
|
40
|
+
width?: number;
|
|
41
|
+
}
|
|
42
|
+
export interface CanvasRun {
|
|
43
|
+
text: string;
|
|
44
|
+
font?: string;
|
|
45
|
+
size?: number;
|
|
46
|
+
color?: string;
|
|
47
|
+
}
|
|
48
|
+
export declare class CanvasTransform {
|
|
49
|
+
readonly matrix: number[];
|
|
50
|
+
constructor(matrix: number[]);
|
|
51
|
+
/** Rotate clockwise by `degrees` around the origin, or around (cx, cy). */
|
|
52
|
+
static rotate(degrees: number, cx?: number, cy?: number): CanvasTransform;
|
|
53
|
+
/** Scale by sx (and optionally sy; defaults to sx for uniform scale). */
|
|
54
|
+
static scale(sx: number, sy?: number): CanvasTransform;
|
|
55
|
+
/** Translate by (tx, ty). */
|
|
56
|
+
static translate(tx: number, ty: number): CanvasTransform;
|
|
57
|
+
/**
|
|
58
|
+
* Combine: apply `other` first, then `this`.
|
|
59
|
+
* Equivalent to matrix multiplication: this × other.
|
|
60
|
+
*/
|
|
61
|
+
then(other: CanvasTransform): CanvasTransform;
|
|
62
|
+
/** Serialise to the `"matrix(a,b,c,d,e,f)"` string form that Rust `jattr()` reads. */
|
|
63
|
+
toString(): string;
|
|
64
|
+
}
|
|
65
|
+
export interface LpdfCanvasRectNode {
|
|
66
|
+
type: 'canvas-rect';
|
|
67
|
+
attrs: Record<string, string>;
|
|
68
|
+
}
|
|
69
|
+
export interface LpdfCanvasLineNode {
|
|
70
|
+
type: 'canvas-line';
|
|
71
|
+
attrs: Record<string, string>;
|
|
72
|
+
}
|
|
73
|
+
export interface LpdfCanvasEllipseNode {
|
|
74
|
+
type: 'canvas-ellipse';
|
|
75
|
+
attrs: Record<string, string>;
|
|
76
|
+
}
|
|
77
|
+
export interface LpdfCanvasCircleNode {
|
|
78
|
+
type: 'canvas-circle';
|
|
79
|
+
attrs: Record<string, string>;
|
|
80
|
+
}
|
|
81
|
+
export interface LpdfCanvasPathNode {
|
|
82
|
+
type: 'canvas-path';
|
|
83
|
+
attrs: Record<string, string>;
|
|
84
|
+
}
|
|
85
|
+
export interface LpdfCanvasTextNode {
|
|
86
|
+
type: 'canvas-text';
|
|
87
|
+
text: string;
|
|
88
|
+
attrs: Record<string, string>;
|
|
89
|
+
runs?: {
|
|
90
|
+
text: string;
|
|
91
|
+
attrs: {
|
|
92
|
+
font?: string;
|
|
93
|
+
'font-size'?: string;
|
|
94
|
+
color?: string;
|
|
95
|
+
};
|
|
96
|
+
}[];
|
|
97
|
+
}
|
|
98
|
+
export interface LpdfCanvasImgNode {
|
|
99
|
+
type: 'canvas-img';
|
|
100
|
+
attrs: Record<string, string>;
|
|
101
|
+
}
|
|
102
|
+
export type LpdfCanvasPrimitiveNode = LpdfCanvasRectNode | LpdfCanvasLineNode | LpdfCanvasEllipseNode | LpdfCanvasCircleNode | LpdfCanvasPathNode | LpdfCanvasTextNode | LpdfCanvasImgNode;
|
|
103
|
+
export interface CanvasLayerOptions {
|
|
104
|
+
page?: PageScope | string;
|
|
105
|
+
opacity?: number;
|
|
106
|
+
transform?: CanvasTransform;
|
|
107
|
+
}
|
|
108
|
+
export interface LpdfCanvasLayerNode {
|
|
109
|
+
type: 'canvas-layer';
|
|
110
|
+
attrs: Record<string, string>;
|
|
111
|
+
nodes: LpdfCanvasPrimitiveNode[];
|
|
112
|
+
}
|
|
113
|
+
declare function rect(x: number, y: number, w: number, h: number, style?: CanvasRectStyle): LpdfCanvasRectNode;
|
|
114
|
+
declare function line(x1: number, y1: number, x2: number, y2: number, style?: CanvasLineStyle): LpdfCanvasLineNode;
|
|
115
|
+
declare function ellipse(cx: number, cy: number, rx: number, ry: number, style?: CanvasEllipseStyle): LpdfCanvasEllipseNode;
|
|
116
|
+
declare function circle(cx: number, cy: number, r: number, style?: CanvasEllipseStyle): LpdfCanvasCircleNode;
|
|
117
|
+
declare function path(d: string, style?: CanvasPathStyle): LpdfCanvasPathNode;
|
|
118
|
+
declare function canvasText(x: number, y: number, content: string, style?: CanvasTextStyle, runs?: CanvasRun[]): LpdfCanvasTextNode;
|
|
119
|
+
declare function img(x: number, y: number, w: number, h: number, name: string): LpdfCanvasImgNode;
|
|
120
|
+
declare function layer(nodes: LpdfCanvasPrimitiveNode[], options?: CanvasLayerOptions): LpdfCanvasLayerNode;
|
|
121
|
+
export declare const LpdfCanvas: Readonly<{
|
|
122
|
+
rect: typeof rect;
|
|
123
|
+
line: typeof line;
|
|
124
|
+
ellipse: typeof ellipse;
|
|
125
|
+
circle: typeof circle;
|
|
126
|
+
path: typeof path;
|
|
127
|
+
text: typeof canvasText;
|
|
128
|
+
img: typeof img;
|
|
129
|
+
layer: typeof layer;
|
|
130
|
+
}>;
|
|
131
|
+
export {};
|