turbo-html2pdf 0.3.2 → 0.3.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/Cargo.toml +1 -0
- package/README.md +1 -1
- package/index.d.ts +36 -0
- package/index.js +13 -1
- package/package.json +1 -1
- package/src/errors.rs +14 -1
- package/src/lib.rs +93 -3
- package/turbo-pdf-napi.darwin-arm64.node +0 -0
- package/turbo-pdf-napi.linux-arm64-gnu.node +0 -0
- package/turbo-pdf-napi.linux-x64-gnu.node +0 -0
- package/turbo-pdf-napi.linux-x64-musl.node +0 -0
- package/turbo-pdf-napi.win32-x64-msvc.node +0 -0
package/Cargo.toml
CHANGED
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.3.
|
|
204
|
+
`v0.3.3`. The core engine is complete and heavily tested (the `turbo-html2pdf-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
|
@@ -88,6 +88,39 @@ export interface Watermark {
|
|
|
88
88
|
tiled?: boolean;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
/** A text-only watermark for {@link stamp}. Unlike {@link Watermark} (the
|
|
92
|
+
* render-time mark, which may be text or image), the post-emit overlay is
|
|
93
|
+
* text-only. Its text is shaped and embedded with a real, subsetted font —
|
|
94
|
+
* the bundled sans-serif by default, or {@link StampWatermark.font} when you
|
|
95
|
+
* supply one — so the output is self-contained and never depends on a base-14
|
|
96
|
+
* font the reader may or may not have installed. */
|
|
97
|
+
export interface StampWatermark {
|
|
98
|
+
/** The word to stamp. */
|
|
99
|
+
text: string;
|
|
100
|
+
/** Fill color `#rrggbb`. Defaults to gray. */
|
|
101
|
+
color?: string;
|
|
102
|
+
/** Fill opacity `0.0..=1.0`. Defaults to `0.15`. */
|
|
103
|
+
opacity?: number;
|
|
104
|
+
/** Rotation in degrees. Defaults to `45`. */
|
|
105
|
+
angle?: number;
|
|
106
|
+
/** Font size in CSS px. Defaults to `64`. */
|
|
107
|
+
fontSize?: number;
|
|
108
|
+
/** Font (TrueType/OTF bytes) to shape and embed the stamp text with. Omit
|
|
109
|
+
* for the bundled sans-serif. */
|
|
110
|
+
font?: Buffer;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Options for {@link stamp}: the watermark plus the optional decrypt/
|
|
114
|
+
* re-encrypt round trip. */
|
|
115
|
+
export interface StampOptions {
|
|
116
|
+
/** The watermark overlaid on every page. */
|
|
117
|
+
watermark: StampWatermark;
|
|
118
|
+
/** Opens an encrypted `pdf` input. Omit for a plaintext input. */
|
|
119
|
+
password?: string;
|
|
120
|
+
/** Re-encrypts the stamped output. Omit for plaintext output. */
|
|
121
|
+
encryption?: Encryption;
|
|
122
|
+
}
|
|
123
|
+
|
|
91
124
|
/** Compile-time knobs: partials, missing-value policy, and include depth. */
|
|
92
125
|
export interface CompileOptions {
|
|
93
126
|
/** Partial templates by name, for `{% include %}`. */
|
|
@@ -214,3 +247,6 @@ export function appendPdf(base: Buffer, extras: Buffer[]): Buffer;
|
|
|
214
247
|
* flex/grid/table items, replaced `<img>`) resolve a `data-cid`; a
|
|
215
248
|
* `display:inline` element is flattened during box generation and drops it. */
|
|
216
249
|
export function layoutBoxes(html: string, css: string, width: number, height: number): string;
|
|
250
|
+
/** Overlay a watermark on EVERY page of an existing PDF, optionally decrypting
|
|
251
|
+
* the input and re-encrypting the output. Throws `TurboPdfError` on failure. */
|
|
252
|
+
export function stamp(pdf: Buffer, opts: StampOptions): Buffer;
|
package/index.js
CHANGED
|
@@ -139,5 +139,17 @@ const layoutBoxes = guard((html, css, width, height) =>
|
|
|
139
139
|
native.layoutBoxes(html, css, width, height),
|
|
140
140
|
);
|
|
141
141
|
|
|
142
|
-
|
|
142
|
+
// Overlay a watermark on every page of an existing PDF; faults surface as a
|
|
143
|
+
// typed TurboPdfError.
|
|
144
|
+
const stamp = guard((pdf, opts) => native.stamp(pdf, opts));
|
|
145
|
+
|
|
146
|
+
module.exports = {
|
|
147
|
+
compile,
|
|
148
|
+
render,
|
|
149
|
+
appendPdf,
|
|
150
|
+
layoutBoxes,
|
|
151
|
+
stamp,
|
|
152
|
+
Fonts: native.Fonts,
|
|
153
|
+
TurboPdfError,
|
|
154
|
+
};
|
|
143
155
|
module.exports.default = module.exports;
|
package/package.json
CHANGED
package/src/errors.rs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
//! JS wrapper (`index.js`) detects the prefix and rethrows a typed
|
|
8
8
|
//! `TurboPdfError` whose `.code` and `.span` mirror this payload.
|
|
9
9
|
|
|
10
|
-
use turbo_html2pdf_core::{AppendError, CompileError, ErrorCode, RenderError, Span};
|
|
10
|
+
use turbo_html2pdf_core::{AppendError, CompileError, ErrorCode, RenderError, Span, StampError};
|
|
11
11
|
|
|
12
12
|
/// Sentinel that marks a `reason` string as a structured turbo-pdf error. The JS
|
|
13
13
|
/// wrapper splits on this to recover the JSON payload.
|
|
@@ -55,3 +55,16 @@ pub fn from_render(e: RenderError) -> napi::Error {
|
|
|
55
55
|
pub fn from_append(e: AppendError) -> napi::Error {
|
|
56
56
|
encode(ErrorCode::Render, &e.to_string(), Span::default())
|
|
57
57
|
}
|
|
58
|
+
|
|
59
|
+
/// Map a PDF stamp failure to a typed N-API error. Stamp errors carry no source
|
|
60
|
+
/// span, so a zeroed span is used under the generic `Render` code.
|
|
61
|
+
pub fn from_stamp(e: StampError) -> napi::Error {
|
|
62
|
+
encode(ErrorCode::Render, &e.to_string(), Span::default())
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// Map a caller-supplied stamp watermark font that failed to parse to a typed
|
|
66
|
+
/// N-API error. Mirrors [`from_stamp`]'s shape (generic `Render` code, zeroed
|
|
67
|
+
/// span) since font parsing carries no source span either.
|
|
68
|
+
pub fn from_invalid_font(message: &str) -> napi::Error {
|
|
69
|
+
encode(ErrorCode::Render, message, Span::default())
|
|
70
|
+
}
|
package/src/lib.rs
CHANGED
|
@@ -34,9 +34,9 @@ use std::collections::HashMap;
|
|
|
34
34
|
use turbo_html2pdf_core::style::TokenSet;
|
|
35
35
|
use turbo_html2pdf_core::{
|
|
36
36
|
append_pdfs, build_cascade, compile as core_compile, emit_pdf_with_images, layout_boxes,
|
|
37
|
-
render_pages, style::parse_stylesheet, CidBox, CompileOptions,
|
|
38
|
-
|
|
39
|
-
Rgba, TextWatermark, Watermark,
|
|
37
|
+
render_pages, stamp as core_stamp, style::parse_stylesheet, CidBox, CompileOptions,
|
|
38
|
+
Diagnostics, EmitOptions, Encryption, FontFace, FontRegistry, ImageWatermark, MissingPolicy,
|
|
39
|
+
NoImages, Permissions, RenderInputs, Rgba, StampWatermark, TextWatermark, Watermark,
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
use convert::{build_registry, diagnostics_to_js, JsDiagnostic, JsFont, JsImage, MapResolver};
|
|
@@ -376,6 +376,96 @@ pub fn append_pdf(base: Buffer, extras: Vec<Buffer>) -> napi::Result<Buffer> {
|
|
|
376
376
|
Ok(merged.into())
|
|
377
377
|
}
|
|
378
378
|
|
|
379
|
+
/// A text-only watermark for [`stamp`]. Unlike [`JsWatermark`] (the render-time
|
|
380
|
+
/// mark, which may be text or image), the post-emit overlay only ever draws
|
|
381
|
+
/// shaped, embedded text — there is no `image`/`tiled` shape here. The shown
|
|
382
|
+
/// text is embedded via a real, subsetted font: `font` when supplied, else the
|
|
383
|
+
/// bundled sans-serif (the same resolution the render-time watermark uses).
|
|
384
|
+
#[napi(object)]
|
|
385
|
+
pub struct JsStampWatermark {
|
|
386
|
+
/// The word to stamp.
|
|
387
|
+
pub text: String,
|
|
388
|
+
/// Fill color `#rrggbb`. Defaults to gray.
|
|
389
|
+
pub color: Option<String>,
|
|
390
|
+
/// Fill opacity `0.0..=1.0`. Defaults to `0.15`.
|
|
391
|
+
pub opacity: Option<f64>,
|
|
392
|
+
/// Rotation in degrees. Defaults to `45`.
|
|
393
|
+
pub angle: Option<f64>,
|
|
394
|
+
/// Font size in CSS px. Defaults to `64`.
|
|
395
|
+
pub font_size: Option<f64>,
|
|
396
|
+
/// Optional font (TrueType/OTF bytes) to shape + embed the watermark text
|
|
397
|
+
/// with; omit for the bundled sans-serif.
|
|
398
|
+
pub font: Option<Buffer>,
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/// Options for [`stamp`]: the watermark plus the optional decrypt/re-encrypt
|
|
402
|
+
/// round trip.
|
|
403
|
+
#[napi(object)]
|
|
404
|
+
pub struct JsStampOptions {
|
|
405
|
+
/// The watermark overlaid on every page.
|
|
406
|
+
pub watermark: JsStampWatermark,
|
|
407
|
+
/// Opens an encrypted `pdf` input. Omit for a plaintext input.
|
|
408
|
+
pub password: Option<String>,
|
|
409
|
+
/// Re-encrypts the stamped output. Omit for plaintext output.
|
|
410
|
+
pub encryption: Option<JsEncryption>,
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/// Overlay a watermark on EVERY page of an existing PDF, optionally decrypting
|
|
414
|
+
/// the input and re-encrypting the output. Throws `TurboPdfError` on failure.
|
|
415
|
+
#[napi]
|
|
416
|
+
pub fn stamp(pdf: Buffer, opts: JsStampOptions) -> napi::Result<Buffer> {
|
|
417
|
+
let watermark = build_stamp_watermark(opts.watermark)?;
|
|
418
|
+
let password = opts.password;
|
|
419
|
+
let encryption = opts.encryption.map(JsEncryption::into_core);
|
|
420
|
+
let stamped = core_stamp(
|
|
421
|
+
pdf.as_ref(),
|
|
422
|
+
&watermark,
|
|
423
|
+
password.as_deref(),
|
|
424
|
+
encryption.as_ref(),
|
|
425
|
+
)
|
|
426
|
+
.map_err(errors::from_stamp)?;
|
|
427
|
+
Ok(stamped.into())
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/// Lower a [`JsStampWatermark`] into the core [`StampWatermark`], applying the
|
|
431
|
+
/// documented defaults (gray / 0.15 / 45° / 64px) for every omitted field.
|
|
432
|
+
/// Throws a typed `TurboPdfError` if `font` is supplied but is not a valid
|
|
433
|
+
/// TrueType/OTF font.
|
|
434
|
+
fn build_stamp_watermark(w: JsStampWatermark) -> napi::Result<StampWatermark> {
|
|
435
|
+
let face = resolve_stamp_face(w.font)?;
|
|
436
|
+
Ok(StampWatermark {
|
|
437
|
+
text: w.text,
|
|
438
|
+
font_size: w.font_size.map(|v| v as f32).unwrap_or(64.0),
|
|
439
|
+
color: w
|
|
440
|
+
.color
|
|
441
|
+
.as_deref()
|
|
442
|
+
.and_then(parse_hex_color)
|
|
443
|
+
.unwrap_or(Rgba::new(128, 128, 128, 255)),
|
|
444
|
+
opacity: w.opacity.map(|v| v as f32).unwrap_or(0.15),
|
|
445
|
+
angle_deg: w.angle.map(|v| v as f32).unwrap_or(45.0),
|
|
446
|
+
face,
|
|
447
|
+
})
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/// Resolve the [`FontFace`] the stamp overlay shapes/embeds its text with:
|
|
451
|
+
/// the caller's `font` bytes when supplied (parsed the SAME way the render
|
|
452
|
+
/// path parses caller fonts, see [`convert::build_registry`]), else the
|
|
453
|
+
/// bundled sans-serif — the same resolution [`build_watermark`] uses for the
|
|
454
|
+
/// render-time mark (`FontRegistry::new().select(&[], 400, false)`).
|
|
455
|
+
fn resolve_stamp_face(font: Option<Buffer>) -> napi::Result<FontFace> {
|
|
456
|
+
match font {
|
|
457
|
+
Some(bytes) => {
|
|
458
|
+
FontFace::from_bytes(bytes.to_vec(), "watermark", 400, false).ok_or_else(|| {
|
|
459
|
+
errors::from_invalid_font("stamp watermark font: not a valid TrueType/OTF font")
|
|
460
|
+
})
|
|
461
|
+
}
|
|
462
|
+
None => Ok(FontRegistry::new()
|
|
463
|
+
.select(&[], 400, false)
|
|
464
|
+
.expect("bundled sans face")
|
|
465
|
+
.clone()),
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
379
469
|
/// The shared render pipeline: cascade + geometry + fonts -> `render_pages` ->
|
|
380
470
|
/// `emit_pdf`. Diagnostics flow into the result; only fatal faults throw.
|
|
381
471
|
fn run_pipeline(
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|