@qrcommunication/gigapdf-lib 0.1.0 → 0.8.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/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * @giga-pdf/wasm-engine — TypeScript SDK for the zero-dependency Rust→WASM PDF
3
- * engine (gigapdf-engine). Wraps the flat `extern "C"` `gp_*` ABI behind a typed,
2
+ * @qrcommunication/gigapdf-lib — TypeScript SDK for the zero-dependency Rust→WASM
3
+ * PDF engine (gigapdf-lib). Wraps the flat `extern "C"` `gp_*` ABI behind a typed,
4
4
  * ergonomic class. No third-party runtime deps; the `.wasm` is self-contained.
5
5
  *
6
6
  * Usage:
@@ -42,6 +42,8 @@ declare class GigaPdfEngine {
42
42
  _json<T = unknown>(call: (outLenPtr: number) => number): T;
43
43
  /** Pass a string argument; runs `fn(ptr, len)` then frees. */
44
44
  _withStr<T>(s: string, fn: (ptr: number, len: number) => T): T;
45
+ /** Pass an optional string; an absent/empty value runs `fn(0, 0)` (no alloc). */
46
+ _withOptStr<T>(s: string | undefined, fn: (ptr: number, len: number) => T): T;
45
47
  /** Pass a bytes argument; runs `fn(ptr, len)` then frees. */
46
48
  _withBytes<T>(bytes: Uint8Array, fn: (ptr: number, len: number) => T): T;
47
49
  /** Pass a `u32[]` argument (e.g. page numbers); runs `fn(ptr, count)` then frees. */
@@ -63,6 +65,102 @@ declare class GigaPdfEngine {
63
65
  fontRequestUrl(family: string, weight?: number, italic?: boolean): string;
64
66
  /** Extract the trusted gstatic font URL from a Google Fonts CSS2 response. */
65
67
  parseCssFontUrl(css: string): string;
68
+ /**
69
+ * Evaluate a JavaScript snippet with the built-in engine and return the
70
+ * result value as a string (or `Uncaught …` / `SyntaxError: …`).
71
+ */
72
+ evalJs(src: string): string;
73
+ /**
74
+ * Run a document's inline `<script>`s and return the resulting HTML. The
75
+ * `htmlRender`/`htmlNeededFonts` paths already do this automatically; use this
76
+ * only when you want the post-script HTML on its own.
77
+ */
78
+ runInlineScripts(html: string): string;
79
+ /**
80
+ * Phase 1 — the Google fonts the document references. Download each `url`
81
+ * (→ TTF) and pass the bytes back to {@link htmlRender} for an identical render.
82
+ */
83
+ htmlNeededFonts(html: string): HtmlFontRequest[];
84
+ /**
85
+ * Phase 2 — render HTML + CSS to PDF, with the supplied fonts embedded (real
86
+ * Google fonts, real metrics → identical or nearest match). Block, inline and
87
+ * table layout with pagination. Page size and margin are in points
88
+ * (US-Letter portrait, 0.5in margins by default). JavaScript is not executed.
89
+ */
90
+ htmlRender(html: string, fonts?: HtmlFont[], pageWidth?: number, pageHeight?: number, margin?: number): Uint8Array;
91
+ /**
92
+ * Resolve a named paper size — `"A4"`, `"a3-landscape"`, `"letter"`, `"legal"`,
93
+ * `"tabloid"`, `"b5"`, … — to `{ w, h }` in points (portrait unless the name
94
+ * has a `-landscape` suffix). Returns `null` for an unknown name.
95
+ */
96
+ pageSize(name: string): {
97
+ w: number;
98
+ h: number;
99
+ } | null;
100
+ /**
101
+ * Phase 1 variant that also scans the running `header`/`footer` HTML, so the
102
+ * Google fonts they reference are requested alongside the body's.
103
+ */
104
+ htmlNeededFontsWith(html: string, header?: string, footer?: string): HtmlFontRequest[];
105
+ /**
106
+ * Phase 2 with full page control: named/explicit size, per-side margins, and a
107
+ * running header/footer painted in the page margins. `{{page}}` and `{{pages}}`
108
+ * in the header/footer are replaced with the current / total page number.
109
+ *
110
+ * ```ts
111
+ * const fonts = await fetchFonts(giga.htmlNeededFontsWith(html, header, footer));
112
+ * const pdf = giga.htmlRenderWith(html, fonts, {
113
+ * pageSize: "A4",
114
+ * margin: { top: 72, bottom: 72, left: 54, right: 54 },
115
+ * header: `<div style="text-align:center">My Report</div>`,
116
+ * footer: `<div style="text-align:center">Page {{page}} / {{pages}}</div>`,
117
+ * });
118
+ * ```
119
+ */
120
+ htmlRenderWith(html: string, fonts?: HtmlFont[], options?: HtmlRenderOptions): Uint8Array;
121
+ }
122
+ /** A Google font the HTML engine needs (resolved from the catalogue). */
123
+ interface HtmlFontRequest {
124
+ family: string;
125
+ weight: number;
126
+ italic: boolean;
127
+ /** Google Fonts CSS URL — the host fetches it to obtain the TTF. */
128
+ url: string;
129
+ }
130
+ /** A downloaded font handed to {@link GigaPdfEngine.htmlRender}. */
131
+ interface HtmlFont {
132
+ family: string;
133
+ weight: number;
134
+ italic: boolean;
135
+ ttf: Uint8Array;
136
+ }
137
+ /** Per-side page margins in points; omitted sides default to 36pt. */
138
+ interface HtmlMargins {
139
+ top?: number;
140
+ right?: number;
141
+ bottom?: number;
142
+ left?: number;
143
+ }
144
+ /** Page setup for {@link GigaPdfEngine.htmlRenderWith}. */
145
+ interface HtmlRenderOptions {
146
+ /** Named paper size (`"A4"`, `"a3-landscape"`, `"letter"`, …) — wins over width/height. */
147
+ pageSize?: string;
148
+ /** Explicit page width in points (default 612 = US Letter). Ignored if `pageSize` is set. */
149
+ pageWidth?: number;
150
+ /** Explicit page height in points (default 792). Ignored if `pageSize` is set. */
151
+ pageHeight?: number;
152
+ /** Uniform margin (points) or per-side margins. Default 36pt (0.5in). */
153
+ margin?: number | HtmlMargins;
154
+ /** Running header HTML painted in the top margin (`{{page}}` / `{{pages}}` tokens). */
155
+ header?: string;
156
+ /** Running footer HTML painted in the bottom margin (same tokens). */
157
+ footer?: string;
158
+ /** Distance from the top edge to the header block, in points (default 18). */
159
+ headerOffset?: number;
160
+ /** Distance from the bottom edge to the footer block, in points (default 18). */
161
+ footerOffset?: number;
162
+ /** Number assigned to the first page for `{{page}}` (default 1). */
163
+ startPageNumber?: number;
66
164
  }
67
165
  interface FontInfo {
68
166
  family: string;
@@ -152,6 +250,26 @@ interface PageInfo {
152
250
  height: number;
153
251
  rotation: number;
154
252
  }
253
+ /** Visual styling for a newly-created form field. */
254
+ interface FieldStyle {
255
+ /** Text size in points; `0` (default) auto-sizes to the field box. */
256
+ fontSize?: number;
257
+ /** Text / mark colour `0xRRGGBB` (default black). */
258
+ color?: number;
259
+ /** Border colour `0xRRGGBB`, or `null` for no border (default black). */
260
+ border?: number | null;
261
+ /** Background fill `0xRRGGBB`, or `null` for transparent (default none). */
262
+ background?: number | null;
263
+ /** Border width in points (default `1`). */
264
+ borderWidth?: number;
265
+ }
266
+ /** One option of a radio group: its export value and on-page rectangle. */
267
+ interface RadioOption {
268
+ /** The export value stored in the field when this button is selected. */
269
+ export: string;
270
+ /** `[x0, y0, x1, y1]` in PDF user space. */
271
+ rect: [number, number, number, number];
272
+ }
155
273
  /** A live document handle. Call {@link close} when done. */
156
274
  declare class GigaPdfDoc {
157
275
  private readonly g;
@@ -179,7 +297,37 @@ declare class GigaPdfDoc {
179
297
  * Draw a vector rectangle. Pass an `0xRRGGBB` colour for `stroke`/`fill`, or
180
298
  * `null` to omit that paint. 0 → success.
181
299
  */
182
- addRectangle(page: number, x: number, y: number, w: number, h: number, stroke?: number | null, fill?: number | null, lineWidth?: number): boolean;
300
+ addRectangle(page: number, x: number, y: number, w: number, h: number, stroke?: number | null, fill?: number | null, lineWidth?: number, opacity?: number): boolean;
301
+ /** Draw a straight line from `(x1,y1)` to `(x2,y2)`. `stroke` is `0xRRGGBB`. */
302
+ drawLine(page: number, x1: number, y1: number, x2: number, y2: number, stroke?: number, lineWidth?: number, opacity?: number): boolean;
303
+ /**
304
+ * Draw an ellipse (circle when `rx === ry`) centred at `(cx,cy)`. Pass an
305
+ * `0xRRGGBB` colour for `stroke`/`fill`, or `null` to omit that paint.
306
+ */
307
+ addEllipse(page: number, cx: number, cy: number, rx: number, ry: number, stroke?: number | null, fill?: number | null, lineWidth?: number, opacity?: number): boolean;
308
+ /**
309
+ * Draw a polyline/polygon through flat `[x0,y0,x1,y1,…]` points. `close` joins
310
+ * the last vertex back to the first. `0xRRGGBB` colours, or `null` to omit.
311
+ */
312
+ addPolygon(page: number, points: number[], close?: boolean, stroke?: number | null, fill?: number | null, lineWidth?: number, opacity?: number): boolean;
313
+ /**
314
+ * Draw an SVG path (`M`/`L`/`C`/`Q`/`Z`…) anchored so the SVG origin maps to
315
+ * `(ox,oy)` with the Y axis flipped — same convention as `pdf-lib`'s
316
+ * `drawSvgPath`. Covers freeform/polygon/triangle shapes.
317
+ */
318
+ addPath(page: number, svgPath: string, ox: number, oy: number, stroke?: number | null, fill?: number | null, lineWidth?: number, opacity?: number): boolean;
319
+ /**
320
+ * Embed a raster image (PNG or JPEG bytes) at `(x,y)` sized `(w,h)` in PDF
321
+ * user space. PNG alpha is honoured; `opacity` (0..1) sets an overall alpha.
322
+ */
323
+ addImage(page: number, data: Uint8Array, x: number, y: number, w: number, h: number, opacity?: number): boolean;
324
+ /**
325
+ * Draw SVG markup on a page as **native vector paths** (crisp at any zoom, not
326
+ * rasterized), fitting its `viewBox` into the box `(x, y, w, h)` in PDF points
327
+ * (origin bottom-left). Supports shapes, `<path>`, groups, transforms and
328
+ * fill/stroke/opacity. Returns `false` if the SVG can't be parsed.
329
+ */
330
+ addSvg(page: number, svg: string, x: number, y: number, w: number, h: number): boolean;
183
331
  /** True redaction: delete content intersecting the region (no opaque cover by default). */
184
332
  redact(page: number, x: number, y: number, w: number, h: number, coverRgb?: number, hasCover?: boolean): number;
185
333
  rotatePage(page: number, degrees: number): boolean;
@@ -204,12 +352,26 @@ declare class GigaPdfDoc {
204
352
  toHtml(): string;
205
353
  toDocx(): Uint8Array;
206
354
  toPptx(): Uint8Array;
355
+ /** Convert to an editable OpenDocument Presentation (`.odp`). */
356
+ toOdp(): Uint8Array;
207
357
  toOdt(): Uint8Array;
208
358
  toXlsx(): Uint8Array;
209
359
  toOds(): Uint8Array;
210
360
  toRtf(): Uint8Array;
211
361
  toPdfA(): Uint8Array;
212
- saveEncrypted(password: string, fileId: string, permissions?: number): Uint8Array;
362
+ /**
363
+ * Serialize the document encrypted with the PDF Standard Security Handler.
364
+ * Defaults to **AES-256 (R6)**. `fileId` is the document `/ID` (any stable
365
+ * hex/string). For AES-256 a **secret 32-byte key** is required — it is taken
366
+ * from `opts.keySeed` or generated with Web Crypto; RC4/AES-128 derive their
367
+ * key from the password and ignore it.
368
+ */
369
+ saveEncrypted(password: string, fileId: string, opts?: {
370
+ ownerPassword?: string;
371
+ algorithm?: "rc4" | "aes128" | "aes256";
372
+ permissions?: number;
373
+ keySeed?: Uint8Array;
374
+ }): Uint8Array;
213
375
  /** Self-signed digital signature. `random` ≥ 256 bytes from crypto.getRandomValues. */
214
376
  sign(fields: string, random: Uint8Array, keyBits?: number): Uint8Array;
215
377
  getMetadata(key: string): string;
@@ -245,6 +407,42 @@ declare class GigaPdfDoc {
245
407
  setRadio(name: string, value: string): boolean;
246
408
  /** Set a choice field's selection (multi-select list boxes accept several values). */
247
409
  setChoice(name: string, values: string[]): boolean;
410
+ /**
411
+ * Create a text field on `page` covering `rect` = `[x0, y0, x1, y1]` (PDF
412
+ * user space). Options: `maxLen` character cap, `multiline`, `password`,
413
+ * and visual `style`.
414
+ */
415
+ addTextField(page: number, name: string, rect: [number, number, number, number], value?: string, opts?: {
416
+ maxLen?: number;
417
+ multiline?: boolean;
418
+ password?: boolean;
419
+ style?: FieldStyle;
420
+ }): boolean;
421
+ /** Create a checkbox. `export` is the on-state name (default `On`). */
422
+ addCheckbox(page: number, name: string, rect: [number, number, number, number], checked?: boolean, opts?: {
423
+ export?: string;
424
+ style?: FieldStyle;
425
+ }): boolean;
426
+ /**
427
+ * Create a radio-button group: one logical field whose `options` are the
428
+ * individual buttons. `selected` is the initially-chosen export value.
429
+ */
430
+ addRadioGroup(page: number, name: string, options: RadioOption[], opts?: {
431
+ selected?: string;
432
+ style?: FieldStyle;
433
+ }): boolean;
434
+ /** Create a drop-down combo box. `editable` permits values outside `options`. */
435
+ addComboBox(page: number, name: string, rect: [number, number, number, number], options: string[], opts?: {
436
+ selected?: string;
437
+ editable?: boolean;
438
+ style?: FieldStyle;
439
+ }): boolean;
440
+ /** Create a scrolling list box. `multi` allows selecting several options. */
441
+ addListBox(page: number, name: string, rect: [number, number, number, number], options: string[], opts?: {
442
+ selected?: string;
443
+ multi?: boolean;
444
+ style?: FieldStyle;
445
+ }): boolean;
248
446
  }
249
447
 
250
- export { type AnnotationInfo, type Box, type Element, type FieldInfo, type FieldKind, type FontInfo, GigaPdfDoc, GigaPdfEngine, type LayerInfo, type LinkInfo, type OcrWord, type OutlineEntry, type PageInfo, type SearchHit, type TextLine, type TextRunInfo };
448
+ export { type AnnotationInfo, type Box, type Element, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, GigaPdfDoc, GigaPdfEngine, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type LayerInfo, type LinkInfo, type OcrWord, type OutlineEntry, type PageInfo, type RadioOption, type SearchHit, type TextLine, type TextRunInfo };