turbo-html2pdf 0.8.7 → 0.8.8
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
|
@@ -1,213 +1,38 @@
|
|
|
1
1
|
# turbo-html2pdf
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
layout → automatic pagination → PDF, shipped as a tiny native addon for Node and
|
|
6
|
-
WebAssembly. No Chromium download, no 200 ms browser spin-up, deterministic output.
|
|
3
|
+
Native HTML/CSS and Jinja-style templates to PDF for Node. Layout, pagination
|
|
4
|
+
and PDF generation run in Rust without Chromium or a separate service.
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
> on PyPI (Python). The name says exactly what it does — HTML → PDF — and avoids
|
|
11
|
-
> clashing with the unrelated "TurboPDF".
|
|
12
|
-
|
|
13
|
-
## 🌐 It generates PDFs entirely in the browser
|
|
14
|
-
|
|
15
|
-
Because the engine is pure Rust → WebAssembly (~3 MB), it runs **100% client-side**
|
|
16
|
-
— no server, no backend, no Chromium. A user pastes HTML/CSS, supplies a font as a
|
|
17
|
-
`Uint8Array`, and gets PDF bytes, all in the browser tab.
|
|
18
|
-
|
|
19
|
-
As far as we know, **no other library does HTML/CSS → PDF in the browser**: the
|
|
20
|
-
HTML→PDF tools that match its fidelity (Puppeteer, Playwright, Gotenberg, WeasyPrint,
|
|
21
|
-
wkhtmltopdf) are all **server-side** — they drive a headless browser or a native
|
|
22
|
-
binary you have to host. The libraries that *do* run in the browser (jsPDF, PDFKit,
|
|
23
|
-
react-pdf) are **draw APIs**, not HTML/CSS layout engines — you place every box by
|
|
24
|
-
hand. turbo-html2pdf is the only one that is *both* a real HTML/CSS engine *and*
|
|
25
|
-
runs with zero server.
|
|
26
|
-
|
|
27
|
-
```js
|
|
28
|
-
import init, { compile } from 'turbo-html2pdf-wasm'
|
|
29
|
-
await init() // load the ~3 MB wasm once
|
|
30
|
-
const program = compile('<h1>{{ t }}</h1>')
|
|
31
|
-
const { pdf } = program.render({ data: { t: 'Hello' }, css: 'h1{font-size:24pt}',
|
|
32
|
-
fonts: [{ data: fontBytes, family: 'Inter' }] })
|
|
33
|
-
// `pdf` is a Uint8Array — download it, no round-trip to a server
|
|
6
|
+
```sh
|
|
7
|
+
npm install turbo-html2pdf
|
|
34
8
|
```
|
|
35
9
|
|
|
36
|
-
---
|
|
37
|
-
|
|
38
|
-
## Why it's fast
|
|
39
|
-
|
|
40
|
-
It does **HTML/CSS → PDF without a browser**. The incumbent way to turn HTML into a
|
|
41
|
-
PDF is to drive headless Chrome (Puppeteer/Playwright/Gotenberg) — which means
|
|
42
|
-
shipping ~200 MB of Chromium, paying a per-process spin-up, and a big memory
|
|
43
|
-
footprint. turbo-html2pdf is a native layout + PDF engine instead, so the common
|
|
44
|
-
"render one document" path is **tens to hundreds of times faster**.
|
|
45
|
-
|
|
46
|
-
### Benchmarks (this machine: Apple M3, 8 cores; `benches/competitive`)
|
|
47
|
-
|
|
48
|
-
Two scenarios, both measured:
|
|
49
|
-
|
|
50
|
-
- **Warm (server):** the realistic server path — the template's compiled `Program`
|
|
51
|
-
is reused and **fonts are parsed once and cached** (the `Fonts` handle). This is
|
|
52
|
-
the number to lead with.
|
|
53
|
-
- **Cold (one-shot):** a single render from a cold start (compile + parse fonts +
|
|
54
|
-
render), e.g. a CLI invocation.
|
|
55
|
-
|
|
56
|
-
All engines render the **same** content (same fonts, A4 geometry, same text); a
|
|
57
|
-
PNG-equivalence diff (`sim` ≈ 0.98 vs the reference) confirms the outputs are
|
|
58
|
-
comparable, so this isn't "winning by doing less".
|
|
59
|
-
|
|
60
|
-
**`invoice` — a one-page document (the bread-and-butter case), median ms:**
|
|
61
|
-
|
|
62
|
-
| Engine | Warm (cached) | Cold | vs turbo (warm) | Memory | Ships a browser? |
|
|
63
|
-
|---|--:|--:|--:|--:|:--:|
|
|
64
|
-
| **turbo-html2pdf** | **1.27** | **1.48** | **1×** | **232 MB** | **no** |
|
|
65
|
-
| Playwright (Chromium) | 46.3 | 49.4 | **36× slower** | 393 MB | yes (~200 MB) |
|
|
66
|
-
| Gotenberg (Chromium) | 66.9 | 70.6 | **53× slower** | 424 MB | yes (~1 GB) |
|
|
67
|
-
| Puppeteer (Chromium) | 86.1 | 132.2 | **68× slower** | 550 MB | yes (~200 MB) |
|
|
68
|
-
| Typst (native, own DSL) | 63.8 | 62.7 | **50× slower** | 1.5 GB | no |
|
|
69
|
-
| WeasyPrint (Python) | 375.8 | 379.3 | **296× slower** | 379 MB | no |
|
|
70
|
-
|
|
71
|
-
**It's also the fastest on every other workload — not just the small one:**
|
|
72
|
-
|
|
73
|
-
| Workload | turbo-html2pdf (warm/cold) | Playwright | Puppeteer | Gotenberg | WeasyPrint | Typst |
|
|
74
|
-
|---|--:|--:|--:|--:|--:|--:|
|
|
75
|
-
| `invoice` (1 page) | **1.27 / 1.48** | 46 / 49 | 86 / 132 | 67 / 71 | 376 / 379 | 64 / 63 |
|
|
76
|
-
| `report-1k` (1 000-row table) | **119 / 130** | 187 / 170 | 384 / 429 | 204 / 204 | 2 550 / 2 547 | 220 / 221 |
|
|
77
|
-
| `legal` (prose + footnotes) | **25 / 26** | 71 / 98 | 124 / 147 | 80 / 80 | 444 / 453 | 72 / 72 |
|
|
78
|
-
| `mixed` (flex + table) | **8.6 / 9.2** | 57 / 58 | 94 / 229 | 73 / 73 | 432 / 437 | 69 / 71 |
|
|
79
|
-
|
|
80
|
-
Against every **HTML/CSS-to-PDF** engine, turbo-html2pdf wins on **all** workloads —
|
|
81
|
-
by 1.6× (big tables) up to ~300× (small docs) — at **lower memory** and with **no
|
|
82
|
-
browser** to install.
|
|
83
|
-
|
|
84
|
-
> **Honest footnote.** `pdfkit` (~6 ms) and `jspdf` (~2 ms) are faster on raw
|
|
85
|
-
> output, but they are **imperative draw APIs** — *you* compute and place every box;
|
|
86
|
-
> there is no HTML/CSS, no layout engine, no pagination. `@react-pdf` is a React
|
|
87
|
-
> component DSL (no HTML/CSS) and its flexbox layout melts on big tables (`report-1k`
|
|
88
|
-
> ≈ **1 300 ms**, ~10× slower than turbo). Different category from "give me HTML, get
|
|
89
|
-
> a PDF". `wkhtmltopdf` (legacy/unmaintained) was not installed.
|
|
90
|
-
|
|
91
|
-
Numbers are "on this machine", never absolutes — rerun with `cd benches/competitive
|
|
92
|
-
&& pnpm bench`. Full table + methodology in
|
|
93
|
-
[`benches/competitive/RESULTS.md`](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/benches/competitive/RESULTS.md).
|
|
94
|
-
|
|
95
|
-
### Why it's *that* fast
|
|
96
|
-
|
|
97
|
-
- **No browser, no process spin-up.** It's a library call, not an IPC round-trip to
|
|
98
|
-
Chrome.
|
|
99
|
-
- **The font program is parsed once.** `ttf-parser` + `rustybuzz` faces are cached
|
|
100
|
-
in the `FontFace` (via `self_cell`) instead of re-parsed per text run; with the
|
|
101
|
-
`Fonts` warm-start handle a server parses fonts once at startup and reuses them.
|
|
102
|
-
- **Box styles resolve once per box.** Context-independent boxes cache their
|
|
103
|
-
resolved `BoxStyle` instead of re-parsing ~25 CSS properties on every layout pass.
|
|
104
|
-
- **Shaping is memoized** by run text (the measure + place passes, and repeated
|
|
105
|
-
cells, share one shape).
|
|
106
|
-
- **Deterministic.** No wall clock, no randomness, no system-font lookup — identical
|
|
107
|
-
inputs produce byte-identical PDFs.
|
|
108
|
-
|
|
109
|
-
---
|
|
110
|
-
|
|
111
|
-
## Architecture
|
|
112
|
-
|
|
113
|
-
A template is **compiled once** into a reusable `Program`, then rendered against
|
|
114
|
-
data many times. The pipeline (`crates/turbo-html2pdf-core`):
|
|
115
|
-
|
|
116
|
-
```
|
|
117
|
-
template (HTML + Jinja + t: directives)
|
|
118
|
-
│ compile() ── parse + cache (MiniJinja)
|
|
119
|
-
▼
|
|
120
|
-
render_pages(data, css, fonts):
|
|
121
|
-
│ 1. render markup ── Jinja → HTML string (data substituted)
|
|
122
|
-
│ 2. parse ── turbo-dom document, t: directives as markers
|
|
123
|
-
│ 3. cascade + inherit ── turbo-dom-layout → ComputedStyle per node
|
|
124
|
-
│ 4. layout ── turbo-dom-layout: block / inline / flex (taffy) / table
|
|
125
|
-
│ → a "galley" of positioned, shaped fragments
|
|
126
|
-
│ 5. paginate ── break the galley into pages (orphans/widows,
|
|
127
|
-
│ break rules, repeated <thead>); resolve
|
|
128
|
-
│ running headers/footers + footnotes per page
|
|
129
|
-
▼ emit_pdf() ── fragments → PDF 1.7: subset + embed fonts
|
|
130
|
-
(TrueType / Type0-CFF), text, vector boxes/
|
|
131
|
-
PDF bytes borders, raster images, watermark
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
Layout (the shared `turbo-dom-layout` crate), font shaping (`rustybuzz`), and PDF writing (`pdf-writer` + `subsetter`) are
|
|
135
|
-
all native; `taffy` powers flexbox. The engine embeds no fonts and does no network
|
|
136
|
-
or filesystem I/O — fonts and images are supplied by the caller.
|
|
137
|
-
|
|
138
|
-
**Frontends & bindings** (`packages/`, `crates/`):
|
|
139
|
-
- `turbo-html2pdf` — Node native addon (`compile`/`render` → `Buffer`).
|
|
140
|
-
- `turbo-html2pdf-wasm` — the same engine in the browser (WebAssembly).
|
|
141
|
-
- `turbo-html2pdf-react` — author templates as React components (compiled to the
|
|
142
|
-
template string at build time, never on the render path).
|
|
143
|
-
- `turbo-html2pdf-template` — author templates with plain functions (no React).
|
|
144
|
-
|
|
145
|
-
The engine is `Send + Sync`: one compiled `Program` renders concurrently across
|
|
146
|
-
threads.
|
|
147
|
-
|
|
148
|
-
---
|
|
149
|
-
|
|
150
|
-
## The DSL — HTML for documents
|
|
151
|
-
|
|
152
|
-
You write **ordinary HTML + a CSS subset**, plus a Jinja templating layer for data,
|
|
153
|
-
plus a handful of `t:` directives for paged-media features browsers can't do.
|
|
154
|
-
Full reference in [`docs/`](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/): [`dsl.md`](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/dsl.md),
|
|
155
|
-
[`paged-media.md`](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/paged-media.md), [`css-support.md`](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/css-support.md),
|
|
156
|
-
[`api.md`](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/api.md).
|
|
157
|
-
|
|
158
|
-
- **Templating (Jinja / MiniJinja):** `{{ value }}`, `{% for %}`, `{% if %}`,
|
|
159
|
-
includes/macros, plus document filters (`currency`, `number`, `percent`, `ordinal`,
|
|
160
|
-
`date`, `datetime`, …) and a `{% switch %}` / `{% case %}` extension.
|
|
161
|
-
- **`t:` directives** (the paged-media layer):
|
|
162
|
-
- `<t:running-header>` / `<t:running-footer>` — headers/footers repeated on every
|
|
163
|
-
page, re-evaluated per page so `{{ page.number }}` / `{{ page.total }}` (and
|
|
164
|
-
`<t:page/>` / `<t:pages/>`) are correct.
|
|
165
|
-
- `<t:footnote>` — auto-numbered footnotes; the body lands on the page where the
|
|
166
|
-
reference falls (content-driven, with a body/footnote fix-point).
|
|
167
|
-
- Pagination is automatic: **page count is an output, never an input.** `@page`
|
|
168
|
-
sets size/margins; break rules + orphans/widows are honored; `<thead>` repeats
|
|
169
|
-
when a table spans pages.
|
|
170
|
-
|
|
171
10
|
```js
|
|
172
|
-
const { compile
|
|
173
|
-
const
|
|
11
|
+
const { compile } = require('turbo-html2pdf')
|
|
12
|
+
const { writeFileSync } = require('node:fs')
|
|
174
13
|
|
|
175
|
-
// Warm at startup, reuse per request — the fast path.
|
|
176
14
|
const program = compile('<h1>{{ title }}</h1><p>{{ body }}</p>')
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
const { pdf, pageCount } = program.render({
|
|
15
|
+
const { pdf, pageCount, diagnostics } = program.render({
|
|
180
16
|
data: { title: 'Hello', body: 'World' },
|
|
181
|
-
css: '@page { size: A4; margin: 20mm }
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
fs.writeFileSync('out.pdf', pdf) // %PDF-1.7
|
|
17
|
+
css: '@page { size: A4; margin: 20mm }',
|
|
18
|
+
})
|
|
19
|
+
writeFileSync('out.pdf', pdf)
|
|
186
20
|
```
|
|
187
21
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
## Features
|
|
191
|
-
|
|
192
|
-
- HTML + CSS subset (block / inline / flexbox / tables), automatic pagination.
|
|
193
|
-
- Running headers & footers with per-page values; auto-numbered footnotes.
|
|
194
|
-
- Font subsetting + embedding (TrueType & CFF/OpenType); per-glyph fallback.
|
|
195
|
-
- Raster images (PNG/JPEG, alpha → SMask) with a sane max-size clamp.
|
|
196
|
-
- Watermarks: out-of-the-box faded diagonal **DRAFT** text (word/colour/angle
|
|
197
|
-
configurable) and background-image watermarks.
|
|
198
|
-
- The Node package enables endnotes, internal cross-references, CMYK print colour,
|
|
199
|
-
PDF/A, PDF/UA, PDF append, AES-256 encryption and post-render stamping.
|
|
200
|
-
The separate `turbo-html2pdf-svg` package adds SVG image support.
|
|
201
|
-
- Deterministic output; `Send + Sync`; no network / no system fonts.
|
|
202
|
-
|
|
203
|
-
## Status
|
|
22
|
+
Compile once and render many times. Default sans-serif, serif and monospace fonts
|
|
23
|
+
are bundled. Pass named font objects to `Fonts.load` or `render` to use your own.
|
|
204
24
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
25
|
+
The addon supports named PNG/JPEG images, headers, footers, footnotes, endnotes,
|
|
26
|
+
links, watermarks, PDF append, PDF/A-2b, PDF/UA-1, CMYK, AES-256 encryption and
|
|
27
|
+
text stamping of existing PDFs. Install `turbo-html2pdf-svg` for SVG images.
|
|
28
|
+
Output is deterministic for the same inputs except when encryption is enabled.
|
|
29
|
+
HTML/CSS support is a subset; the engine does not fetch external assets.
|
|
210
30
|
|
|
211
|
-
|
|
31
|
+
- [API and examples](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/api.md)
|
|
32
|
+
- [Features and runtime limits](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/features.md)
|
|
33
|
+
- [Template and pagination guide](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/guide.md)
|
|
34
|
+
- [Benchmarks and deployment](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/README.md)
|
|
35
|
+
- [TypeScript API](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/crates/turbo-pdf-napi/index.d.ts)
|
|
212
36
|
|
|
213
|
-
|
|
37
|
+
See the [0.8.8 release notes](https://github.com/miaskiewicz/turbo-html2pdf/blob/main/docs/RELEASE-0.8.8.md).
|
|
38
|
+
MIT; bundled fonts have separate notices in `THIRD_PARTY_NOTICES.md`.
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|