turbo-html2pdf 0.2.14 → 0.3.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/README.md CHANGED
@@ -201,7 +201,7 @@ fs.writeFileSync('out.pdf', pdf) // %PDF-1.7
201
201
 
202
202
  ## Status
203
203
 
204
- `v0.2.14`. The core engine is complete and heavily tested (the `turbo-html2pdf-core`
204
+ `v0.3.0`. 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
@@ -203,3 +203,14 @@ export function render(templateHtml: string, opts?: RenderOptions, fonts?: Fonts
203
203
  * the merged PDF. Equivalent to `RenderOptions.appendPdfs` but usable on
204
204
  * already-emitted bytes. Throws `TurboPdfError` if any input fails to parse. */
205
205
  export function appendPdf(base: Buffer, extras: Buffer[]): Buffer;
206
+
207
+ /** Debug/conformance seam (NOT part of the render path): lay `html` out (its
208
+ * own `<style>` blocks plus `css`) at `width`×`height` px with the bundled
209
+ * default fonts, and return a JSON string — an array of
210
+ * `{ cid, x, y, width, height }`, one per box whose source element carried a
211
+ * `data-cid="..."` attribute, in document order. Coordinates are absolute px
212
+ * (page origin top-left) and sizes are the border box, matching a browser's
213
+ * `getBoundingClientRect()`. Only box-generating elements (block, inline-block,
214
+ * flex/grid/table items, replaced `<img>`) resolve a `data-cid`; a
215
+ * `display:inline` element is flattened during box generation and drops it. */
216
+ export function layoutBoxes(html: string, css: string, width: number, height: number): string;
package/index.js CHANGED
@@ -133,5 +133,11 @@ const render = guard((templateHtml, opts, fonts) => native.render(templateHtml,
133
133
  // Glue foreign PDFs after `base`; parse faults surface as a typed TurboPdfError.
134
134
  const appendPdf = guard((base, extras) => native.appendPdf(base, extras));
135
135
 
136
- module.exports = { compile, render, appendPdf, Fonts: native.Fonts, TurboPdfError };
136
+ // Debug/conformance seam: lay HTML out and return the placed geometry of every
137
+ // `data-cid`-tagged box as a JSON string. Not part of the render path.
138
+ const layoutBoxes = guard((html, css, width, height) =>
139
+ native.layoutBoxes(html, css, width, height),
140
+ );
141
+
142
+ module.exports = { compile, render, appendPdf, layoutBoxes, Fonts: native.Fonts, TurboPdfError };
137
143
  module.exports.default = module.exports;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turbo-html2pdf",
3
- "version": "0.2.14",
3
+ "version": "0.3.0",
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",
package/src/lib.rs CHANGED
@@ -33,10 +33,10 @@ use std::collections::HashMap;
33
33
 
34
34
  use turbo_html2pdf_core::style::TokenSet;
35
35
  use turbo_html2pdf_core::{
36
- append_pdfs, build_cascade, compile as core_compile, emit_pdf_with_images, render_pages,
37
- style::parse_stylesheet, CompileOptions, Diagnostics, EmitOptions, Encryption, FontRegistry,
38
- ImageWatermark, MissingPolicy, NoImages, Permissions, RenderInputs, Rgba, TextWatermark,
39
- Watermark,
36
+ append_pdfs, build_cascade, compile as core_compile, emit_pdf_with_images, layout_boxes,
37
+ render_pages, style::parse_stylesheet, CidBox, CompileOptions, Diagnostics, EmitOptions,
38
+ Encryption, FontRegistry, ImageWatermark, MissingPolicy, NoImages, Permissions, RenderInputs,
39
+ Rgba, TextWatermark, Watermark,
40
40
  };
41
41
 
42
42
  use convert::{build_registry, diagnostics_to_js, JsDiagnostic, JsFont, JsImage, MapResolver};
@@ -299,6 +299,72 @@ pub fn render_oneshot(
299
299
  run_pipeline(&program, opts.unwrap_or_default(), fonts)
300
300
  }
301
301
 
302
+ /// Lay `html` out (author `<style>` blocks + `css`) at `width`×`height` px with
303
+ /// the bundled default fonts, and return — as a JSON string — the placed
304
+ /// geometry of every box whose source element carries a `data-cid="..."`
305
+ /// attribute: `[{ "cid", "x", "y", "width", "height" }, …]`, in document order.
306
+ ///
307
+ /// A debug/conformance seam (drives `benches/conformance` against Chromium's
308
+ /// `getBoundingClientRect()`); it does NOT touch the compile→render→emit path.
309
+ /// Coordinates are absolute px (page origin at top-left) and sizes are the border
310
+ /// box, matching a browser's client rect. See the core `layout_boxes` for the
311
+ /// inline-flattening limitation (tag box-generating elements).
312
+ #[napi(js_name = "layoutBoxes")]
313
+ pub fn layout_boxes_json(
314
+ html: String,
315
+ css: String,
316
+ width: f64,
317
+ height: f64,
318
+ ) -> napi::Result<String> {
319
+ let fonts = FontRegistry::new();
320
+ let mut diags = Diagnostics::default();
321
+ let boxes = layout_boxes(&html, &css, width as f32, height as f32, &fonts, &mut diags)
322
+ .map_err(errors::from_render)?;
323
+ Ok(cid_boxes_to_json(&boxes))
324
+ }
325
+
326
+ /// Serialize the placed boxes to a JSON array by hand — a handful of numeric
327
+ /// fields, so a manual encoder keeps the dependency-free thin-bridge contract
328
+ /// without pulling `serde` onto the core `CidBox` type.
329
+ fn cid_boxes_to_json(boxes: &[CidBox]) -> String {
330
+ let mut s = String::from("[");
331
+ for (i, b) in boxes.iter().enumerate() {
332
+ if i > 0 {
333
+ s.push(',');
334
+ }
335
+ s.push_str(&format!(
336
+ "{{\"cid\":{},\"x\":{},\"y\":{},\"width\":{},\"height\":{}}}",
337
+ json_string(&b.cid),
338
+ b.x,
339
+ b.y,
340
+ b.width,
341
+ b.height
342
+ ));
343
+ }
344
+ s.push(']');
345
+ s
346
+ }
347
+
348
+ /// Encode `s` as a JSON string literal (quotes + the mandatory escapes), so a
349
+ /// `data-cid` containing a quote/backslash/control char can't break the array.
350
+ fn json_string(s: &str) -> String {
351
+ let mut out = String::with_capacity(s.len() + 2);
352
+ out.push('"');
353
+ for c in s.chars() {
354
+ match c {
355
+ '"' => out.push_str("\\\""),
356
+ '\\' => out.push_str("\\\\"),
357
+ '\n' => out.push_str("\\n"),
358
+ '\r' => out.push_str("\\r"),
359
+ '\t' => out.push_str("\\t"),
360
+ c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
361
+ c => out.push(c),
362
+ }
363
+ }
364
+ out.push('"');
365
+ out
366
+ }
367
+
302
368
  /// Glue one or more foreign PDF documents after `base`, page by page, returning
303
369
  /// the merged PDF. Equivalent to `RenderOptions.appendPdfs` but usable on
304
370
  /// already-emitted bytes. Throws `TurboPdfError` if any input fails to parse or
Binary file
Binary file
Binary file
Binary file
Binary file