turbo-html2pdf 0.1.3 → 0.1.7

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/README.md CHANGED
@@ -201,7 +201,7 @@ fs.writeFileSync('out.pdf', pdf) // %PDF-1.7
201
201
 
202
202
  ## Status
203
203
 
204
- `v0.1.3`. The core engine is complete and heavily tested (the `turbo-pdf-core`
204
+ `v0.1.7`. The core engine is complete and heavily tested (the `turbo-pdf-core`
205
205
  crate holds 100% line coverage with a cyclomatic-complexity ≤ 5 gate). Bindings:
206
206
  Node (napi) and WebAssembly. See [`docs/`](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/) for the full guide and
207
207
  [`benches/competitive/`](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/benches/competitive/) for the benchmark harness.
package/index.d.ts CHANGED
@@ -6,54 +6,54 @@
6
6
 
7
7
  /** A source location: 1-based line/column (0 when unknown) and byte offset. */
8
8
  export interface Span {
9
- line: number
10
- col: number
11
- byteOffset: number
9
+ line: number;
10
+ col: number;
11
+ byteOffset: number;
12
12
  }
13
13
 
14
14
  /** A fatal compile/render fault. Thrown by `compile`, `program.render`, and the
15
15
  * one-shot `render`. Non-fatal lints are NOT thrown — they come back in
16
16
  * `RenderResult.diagnostics`. */
17
17
  export class TurboPdfError extends Error {
18
- name: 'TurboPdfError'
18
+ name: "TurboPdfError";
19
19
  /** Stable machine-readable code, e.g. `"TemplateSyntax"`, `"UndefinedValue"`. */
20
- code: string
20
+ code: string;
21
21
  /** Source span of the offending construct. */
22
- span: Span
22
+ span: Span;
23
23
  }
24
24
 
25
25
  /** A non-fatal diagnostic (lint) collected during render. */
26
26
  export interface Diagnostic {
27
27
  /** Stable lint code, e.g. `"UnsupportedCss"`, `"RegionOverflow"`. */
28
- code: string
29
- message: string
28
+ code: string;
29
+ message: string;
30
30
  /** 1-based source line (0 when unknown). */
31
- line: number
31
+ line: number;
32
32
  /** 1-based source column (0 when unknown). */
33
- col: number
33
+ col: number;
34
34
  }
35
35
 
36
36
  /** PDF document-info metadata. Every field is optional. */
37
37
  export interface DocMeta {
38
- title?: string
39
- author?: string
40
- subject?: string
41
- keywords?: string
38
+ title?: string;
39
+ author?: string;
40
+ subject?: string;
41
+ keywords?: string;
42
42
  /** Creation date as Unix seconds. Omit for the reproducible sentinel date. */
43
- creationDate?: number
43
+ creationDate?: number;
44
44
  }
45
45
 
46
46
  /** One font face: the program bytes plus the selection metadata the cascade
47
47
  * matches `font-family` / `font-weight` / italic against. */
48
48
  export interface FontFace {
49
49
  /** The font program bytes (`.ttf`/`.otf`). */
50
- data: Buffer
50
+ data: Buffer;
51
51
  /** The CSS `font-family` name this face answers to. */
52
- family: string
52
+ family: string;
53
53
  /** CSS `font-weight` (100..=900). Defaults to 400 (normal). */
54
- weight?: number
54
+ weight?: number;
55
55
  /** Whether this is the italic/oblique face. Defaults to `false`. */
56
- italic?: boolean
56
+ italic?: boolean;
57
57
  }
58
58
 
59
59
  /** One named raster image: a template name plus its encoded PNG/JPEG bytes.
@@ -61,9 +61,9 @@ export interface FontFace {
61
61
  * bytes as a PDF image XObject. */
62
62
  export interface NamedImage {
63
63
  /** The name the template refers to this image by. */
64
- name: string
64
+ name: string;
65
65
  /** The encoded image bytes (PNG or JPEG). */
66
- data: Buffer
66
+ data: Buffer;
67
67
  }
68
68
 
69
69
  /** A page watermark stamped behind the body on every page. Either a shaped-word
@@ -71,28 +71,28 @@ export interface NamedImage {
71
71
  * `RenderOptions.images`); set `image` for the raster form. */
72
72
  export interface Watermark {
73
73
  /** Word to stamp (text mark). Defaults to `DRAFT`. */
74
- text?: string
74
+ text?: string;
75
75
  /** Fill color `#rrggbb` (text mark). Defaults to gray. */
76
- color?: string
76
+ color?: string;
77
77
  /** Image name (image mark), resolved against `RenderOptions.images`. */
78
- image?: string
78
+ image?: string;
79
79
  /** Fill opacity `0.0..=1.0`. Defaults to 0.25 (text) / 1.0 (image). */
80
- opacity?: number
80
+ opacity?: number;
81
81
  /** Rotation in degrees (text mark). Defaults to 45. */
82
- angle?: number
82
+ angle?: number;
83
83
  /** Tile the image mark across the page instead of centering it. */
84
- tiled?: boolean
84
+ tiled?: boolean;
85
85
  }
86
86
 
87
87
  /** Compile-time knobs: partials, missing-value policy, and include depth. */
88
88
  export interface CompileOptions {
89
89
  /** Partial templates by name, for `{% include %}`. */
90
- partials?: Record<string, string>
90
+ partials?: Record<string, string>;
91
91
  /** `"strict"` (default) throws on a missing value; `"empty"`/`"lenient"`
92
92
  * renders it as empty. */
93
- missingPolicy?: 'strict' | 'empty' | 'lenient'
93
+ missingPolicy?: "strict" | "empty" | "lenient";
94
94
  /** Maximum `{% include %}` nesting depth (defaults to the core default). */
95
- includeMaxDepth?: number
95
+ includeMaxDepth?: number;
96
96
  }
97
97
 
98
98
  /** AES-256 password-encryption settings. `userPassword` is required to open the
@@ -101,70 +101,70 @@ export interface CompileOptions {
101
101
  * open. Encrypted output is intentionally non-deterministic. */
102
102
  export interface Encryption {
103
103
  /** Password required to open the document. */
104
- userPassword: string
104
+ userPassword: string;
105
105
  /** Owner password granting full permissions. Defaults to the user password. */
106
- ownerPassword?: string
106
+ ownerPassword?: string;
107
107
  /** Allow printing. Defaults to `true`. */
108
- print?: boolean
108
+ print?: boolean;
109
109
  /** Allow modifying contents. Defaults to `true`. */
110
- modify?: boolean
110
+ modify?: boolean;
111
111
  /** Allow copying/extracting text and graphics. Defaults to `true`. */
112
- copy?: boolean
112
+ copy?: boolean;
113
113
  /** Allow adding/modifying annotations. Defaults to `true`. */
114
- annotate?: boolean
114
+ annotate?: boolean;
115
115
  /** Allow filling existing form fields. Defaults to `true`. */
116
- fillForms?: boolean
116
+ fillForms?: boolean;
117
117
  /** Allow accessibility text extraction. Defaults to `true`. */
118
- accessibility?: boolean
118
+ accessibility?: boolean;
119
119
  /** Allow document assembly (insert/rotate/delete pages). Defaults to `true`. */
120
- assemble?: boolean
120
+ assemble?: boolean;
121
121
  /** Allow full-resolution printing. Defaults to `true`. */
122
- highQualityPrint?: boolean
122
+ highQualityPrint?: boolean;
123
123
  }
124
124
 
125
125
  /** Options for a single render pass. All fields optional. */
126
126
  export interface RenderOptions {
127
127
  /** Data object interpolated into the template (`{{ data.* }}`). */
128
- data?: unknown
128
+ data?: unknown;
129
129
  /** Author CSS; also supplies `@page` geometry (size/margins). */
130
- css?: string
130
+ css?: string;
131
131
  /** Font faces, each `{ data, family, weight?, italic? }`. */
132
- fonts?: FontFace[]
132
+ fonts?: FontFace[];
133
133
  /** Named raster images embedded by template name. */
134
- images?: NamedImage[]
134
+ images?: NamedImage[];
135
135
  /** PDF document metadata. */
136
- meta?: DocMeta
136
+ meta?: DocMeta;
137
137
  /** A faded watermark stamped behind the body on every page. */
138
- watermark?: Watermark
138
+ watermark?: Watermark;
139
139
  /** Pins the `now()` clock (Unix seconds) for deterministic output. */
140
- now?: number
140
+ now?: number;
141
141
  /** Emit PDF/A-2b archival conformance (sRGB `/OutputIntents`, XMP `pdfaid`,
142
142
  * trailer `/ID`). Defaults to `false`. */
143
- pdfA?: boolean
143
+ pdfA?: boolean;
144
144
  /** Emit tagged / accessible PDF/UA-1 machinery (`/StructTreeRoot`, `/MarkInfo`,
145
145
  * per-face `/ToUnicode`, `/Lang`). Defaults to `false`. */
146
- pdfUa?: boolean
146
+ pdfUa?: boolean;
147
147
  /** Natural-language tag (RFC 3066, e.g. `en-US`) written as the catalog
148
148
  * `/Lang`. Only meaningful with `pdfUa`. */
149
- lang?: string
149
+ lang?: string;
150
150
  /** Emit fills in DeviceCMYK instead of DeviceRGB. Defaults to `false`. */
151
- cmyk?: boolean
151
+ cmyk?: boolean;
152
152
  /** AES-256 password protection. Omit for an unencrypted, byte-deterministic
153
153
  * PDF. */
154
- encryption?: Encryption
154
+ encryption?: Encryption;
155
155
  /** Foreign PDF documents glued (page by page) AFTER the rendered pages, in
156
156
  * order. Each entry is a complete PDF. */
157
- appendPdfs?: Buffer[]
157
+ appendPdfs?: Buffer[];
158
158
  }
159
159
 
160
160
  /** The result of a render. */
161
161
  export interface RenderResult {
162
162
  /** The rendered PDF 1.7 document. */
163
- pdf: Buffer
163
+ pdf: Buffer;
164
164
  /** Non-fatal diagnostics (lints) — never thrown. */
165
- diagnostics: Diagnostic[]
165
+ diagnostics: Diagnostic[];
166
166
  /** Number of pages. */
167
- pageCount: number
167
+ pageCount: number;
168
168
  }
169
169
 
170
170
  /** A reusable, pre-parsed set of fonts. Build it ONCE — e.g. warm it at server
@@ -174,28 +174,28 @@ export interface RenderResult {
174
174
  export class Fonts {
175
175
  /** Parse `fonts` (each `{ data, family, weight?, italic? }`) once into a
176
176
  * reusable handle. Do this at startup, then reuse. */
177
- static load(fonts: FontFace[]): Fonts
177
+ static load(fonts: FontFace[]): Fonts;
178
178
  }
179
179
 
180
180
  /** A compiled, reusable template program (thread-safe native handle). */
181
181
  export interface Program {
182
182
  /** Render this program. Pass a prebuilt {@link Fonts} handle to reuse parsed
183
183
  * fonts across calls. Throws `TurboPdfError` on a fatal fault. */
184
- render(opts?: RenderOptions, fonts?: Fonts): RenderResult
184
+ render(opts?: RenderOptions, fonts?: Fonts): RenderResult;
185
185
  /** Whether the source declared a `<t:running-header>`. */
186
- hasHeader(): boolean
186
+ hasHeader(): boolean;
187
187
  /** Whether the source declared a `<t:running-footer>`. */
188
- hasFooter(): boolean
188
+ hasFooter(): boolean;
189
189
  }
190
190
 
191
191
  /** Compile a template into a reusable {@link Program}. Throws `TurboPdfError`. */
192
- export function compile(templateHtml: string, opts?: CompileOptions): Program
192
+ export function compile(templateHtml: string, opts?: CompileOptions): Program;
193
193
 
194
194
  /** One-shot convenience: compile + render in a single call. Pass a prebuilt
195
195
  * {@link Fonts} handle to reuse parsed fonts. */
196
- export function render(templateHtml: string, opts?: RenderOptions, fonts?: Fonts): RenderResult
196
+ export function render(templateHtml: string, opts?: RenderOptions, fonts?: Fonts): RenderResult;
197
197
 
198
198
  /** Glue one or more foreign PDF documents after `base`, page by page, returning
199
199
  * the merged PDF. Equivalent to `RenderOptions.appendPdfs` but usable on
200
200
  * already-emitted bytes. Throws `TurboPdfError` if any input fails to parse. */
201
- export function appendPdf(base: Buffer, extras: Buffer[]): Buffer
201
+ export function appendPdf(base: Buffer, extras: Buffer[]): Buffer;
package/index.js CHANGED
@@ -6,10 +6,10 @@
6
6
  * `result.diagnostics` by the native layer and are passed through untouched.
7
7
  */
8
8
 
9
- 'use strict'
9
+ "use strict";
10
10
 
11
- const { existsSync } = require('node:fs')
12
- const { join } = require('node:path')
11
+ const { existsSync } = require("node:fs");
12
+ const { join } = require("node:path");
13
13
 
14
14
  // --- locate the native addon -------------------------------------------------
15
15
  // In a published package, EVERY platform's prebuilt `.node` sits next to this
@@ -21,112 +21,112 @@ const { join } = require('node:path')
21
21
  // target/{release,debug} (plain `cargo build -p turbo-pdf-napi`).
22
22
  const CANDIDATES = [
23
23
  join(__dirname, `turbo-pdf-napi.${napiPlatform()}.node`),
24
- join(__dirname, 'turbo-pdf-napi.node'),
25
- join(__dirname, '..', '..', 'target', 'release', addonName()),
26
- join(__dirname, '..', '..', 'target', 'debug', addonName()),
27
- ]
24
+ join(__dirname, "turbo-pdf-napi.node"),
25
+ join(__dirname, "..", "..", "target", "release", addonName()),
26
+ join(__dirname, "..", "..", "target", "debug", addonName()),
27
+ ];
28
28
 
29
29
  function isMusl() {
30
30
  // glibc builds report a glibc runtime version; musl builds do not.
31
- if (!process.report || typeof process.report.getReport !== 'function') {
31
+ if (!process.report || typeof process.report.getReport !== "function") {
32
32
  try {
33
- const ldd = require('node:child_process').execSync('which ldd').toString().trim()
34
- return require('node:fs').readFileSync(ldd, 'utf8').includes('musl')
33
+ const ldd = require("node:child_process").execSync("which ldd").toString().trim();
34
+ return require("node:fs").readFileSync(ldd, "utf8").includes("musl");
35
35
  } catch {
36
- return true
36
+ return true;
37
37
  }
38
38
  }
39
- const { glibcVersionRuntime } = process.report.getReport().header
40
- return !glibcVersionRuntime
39
+ const { glibcVersionRuntime } = process.report.getReport().header;
40
+ return !glibcVersionRuntime;
41
41
  }
42
42
 
43
43
  // The platform suffix used by @napi-rs/cli for the bundled `.node` filenames.
44
44
  // Mirrors the naming in NAPI-RS's generated loader, for the matrix this repo
45
45
  // ships: linux x64 gnu/musl, linux arm64, darwin arm64, win32 x64 msvc.
46
46
  function napiPlatform() {
47
- const { platform, arch } = process
48
- if (platform === 'darwin') return `darwin-${arch}` // arm64 -> darwin-arm64
49
- if (platform === 'win32') return `win32-${arch}-msvc` // x64 -> win32-x64-msvc
50
- if (platform === 'linux') {
51
- const abi = isMusl() ? 'musl' : 'gnu'
52
- return `linux-${arch}-${abi}` // x64 -> linux-x64-gnu | linux-x64-musl; arm64 -> linux-arm64-gnu
47
+ const { platform, arch } = process;
48
+ if (platform === "darwin") return `darwin-${arch}`; // arm64 -> darwin-arm64
49
+ if (platform === "win32") return `win32-${arch}-msvc`; // x64 -> win32-x64-msvc
50
+ if (platform === "linux") {
51
+ const abi = isMusl() ? "musl" : "gnu";
52
+ return `linux-${arch}-${abi}`; // x64 -> linux-x64-gnu | linux-x64-musl; arm64 -> linux-arm64-gnu
53
53
  }
54
- return `${platform}-${arch}`
54
+ return `${platform}-${arch}`;
55
55
  }
56
56
 
57
57
  function addonName() {
58
58
  // napi-rs emits a platform cdylib; Node loads it regardless of extension.
59
- if (process.platform === 'darwin') return 'libturbo_pdf_napi.dylib'
60
- if (process.platform === 'win32') return 'turbo_pdf_napi.dll'
61
- return 'libturbo_pdf_napi.so'
59
+ if (process.platform === "darwin") return "libturbo_pdf_napi.dylib";
60
+ if (process.platform === "win32") return "turbo_pdf_napi.dll";
61
+ return "libturbo_pdf_napi.so";
62
62
  }
63
63
 
64
64
  function loadNative() {
65
65
  for (const p of CANDIDATES) {
66
- if (existsSync(p)) return require(p)
66
+ if (existsSync(p)) return require(p);
67
67
  }
68
68
  throw new Error(
69
- 'turbo-pdf-napi: native addon not found. Run `napi build --release` (or ' +
70
- '`cargo build -p turbo-pdf-napi --release`) first. Looked in:\n ' +
71
- CANDIDATES.join('\n '),
72
- )
69
+ "turbo-pdf-napi: native addon not found. Run `napi build --release` (or " +
70
+ "`cargo build -p turbo-pdf-napi --release`) first. Looked in:\n " +
71
+ CANDIDATES.join("\n "),
72
+ );
73
73
  }
74
74
 
75
- const native = loadNative()
75
+ const native = loadNative();
76
76
 
77
77
  // --- typed error -------------------------------------------------------------
78
- const SENTINEL = 'TURBO_PDF_ERR:'
78
+ const SENTINEL = "TURBO_PDF_ERR:";
79
79
 
80
80
  /** A fatal compile/render fault, carrying a machine-readable code and a source
81
81
  * span ({line, col, byteOffset}). Thrown by `compile` / `render`. */
82
82
  class TurboPdfError extends Error {
83
83
  constructor(payload) {
84
- super(payload.message)
85
- this.name = 'TurboPdfError'
86
- this.code = payload.code
87
- this.span = payload.span
84
+ super(payload.message);
85
+ this.name = "TurboPdfError";
86
+ this.code = payload.code;
87
+ this.span = payload.span;
88
88
  }
89
89
  }
90
90
 
91
91
  /** If `err` is a sentinel-encoded native fault, rethrow it as a TurboPdfError;
92
92
  * otherwise rethrow it unchanged. */
93
93
  function rethrow(err) {
94
- const msg = err && typeof err.message === 'string' ? err.message : ''
95
- const at = msg.indexOf(SENTINEL)
96
- if (at === -1) throw err
97
- const json = msg.slice(at + SENTINEL.length)
98
- let payload
94
+ const msg = err && typeof err.message === "string" ? err.message : "";
95
+ const at = msg.indexOf(SENTINEL);
96
+ if (at === -1) throw err;
97
+ const json = msg.slice(at + SENTINEL.length);
98
+ let payload;
99
99
  try {
100
- payload = JSON.parse(json)
100
+ payload = JSON.parse(json);
101
101
  } catch {
102
- throw err
102
+ throw err;
103
103
  }
104
- throw new TurboPdfError(payload)
104
+ throw new TurboPdfError(payload);
105
105
  }
106
106
 
107
107
  function guard(fn) {
108
108
  return (...args) => {
109
109
  try {
110
- return fn(...args)
110
+ return fn(...args);
111
111
  } catch (err) {
112
- rethrow(err)
112
+ rethrow(err);
113
113
  }
114
- }
114
+ };
115
115
  }
116
116
 
117
117
  // --- public surface ----------------------------------------------------------
118
118
  const compile = guard((templateHtml, opts) => {
119
- const program = native.compile(templateHtml, opts)
119
+ const program = native.compile(templateHtml, opts);
120
120
  // Wrap `program.render` so render-time faults are typed too.
121
- const render = program.render.bind(program)
122
- program.render = guard(render)
123
- return program
124
- })
121
+ const render = program.render.bind(program);
122
+ program.render = guard(render);
123
+ return program;
124
+ });
125
125
 
126
- const render = guard((templateHtml, opts, fonts) => native.render(templateHtml, opts, fonts))
126
+ const render = guard((templateHtml, opts, fonts) => native.render(templateHtml, opts, fonts));
127
127
 
128
128
  // Glue foreign PDFs after `base`; parse faults surface as a typed TurboPdfError.
129
- const appendPdf = guard((base, extras) => native.appendPdf(base, extras))
129
+ const appendPdf = guard((base, extras) => native.appendPdf(base, extras));
130
130
 
131
- module.exports = { compile, render, appendPdf, Fonts: native.Fonts, TurboPdfError }
132
- module.exports.default = module.exports
131
+ module.exports = { compile, render, appendPdf, Fonts: native.Fonts, TurboPdfError };
132
+ module.exports.default = module.exports;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turbo-html2pdf",
3
- "version": "0.1.3",
3
+ "version": "0.1.7",
4
4
  "description": "Fast native HTML/CSS + Jinja templating to PDF for Node — no headless browser, no Chromium.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
Binary file
Binary file
Binary file
Binary file
Binary file