Free Rust pdf-writer Templates (The Crate Behind Typst)
The low-level typed Rust PDF serializer crate built for Typst — zero layout engine, #1 lowest RAM (5.9 MB), and #2 fastest pure PDF generation across all 22 libraries (0.2 ms single-page; 16 ms & 0.04s CPU for 22 stress-test pages).
Install: cargo add pdf-writer@0.12 | Runtime: Rust (Rust) | Version: 0.12 | Popularity: 280k/mo installs
Works with: Axum, Actix Web, Tokio, WASM / Embedded Rust
How pdf-writer (Rust) Templates Work in CodePDF
Emits strongly-typed Rust code using pdf_writer::Pdf, Ref, Content, and Standard-14 font dictionaries to serialize spec-compliant PDFs directly to bytes.
- Record-low 1.6 MB peak RSS memory footprint — the leanest backend engine in our 22-library benchmark
- Handles raw PDF object IDs, Catalog/Pages trees, font dictionaries, and content streams automatically
- Zero runtime bloat and compile-time structural safety
Quick Start: Generate PDFs with pdf-writer
Install pdf-writer (Rust) (Rust), then run the code CodePDF exports for any template:
cargo add pdf-writer@0.12
- Run cargo add pdf-writer@0.12 in your Rust project.
- Save the generated file as src/main.rs (or merge it into an existing binary).
- Run cargo run to produce output.pdf in the project root.
- pdf-writer is a low-level builder (it powers Typst) — objects and content streams are written with typed writers.
- The generated code targets pdf-writer 0.12.x — pin the same major version in Cargo.toml.
Example pdf-writer Output (Standard B2B Commercial Invoice)
Compiled directly from the Standard B2B Commercial Invoice template (Rust):
#![allow(unused_variables)]
#![allow(unused_mut)]
#![allow(unused_imports)]
#![allow(dead_code)]
// This code is generated using codepdf.dev
// pdf-writer: add pdf-writer = "0.12" to your Cargo.toml
// flate2: add flate2 = "1" to your Cargo.toml (stream compression)
use pdf_writer::{Content, Filter, Finish, Name, Null, Pdf, Rect, Ref, Str, TextStr};
use flate2::{write::ZlibEncoder, Compression};
use std::io::Write;
use std::collections::HashMap;
fn write_content_stream(pdf: &mut Pdf, id: Ref, raw: &[u8]) {
let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
if enc.write_all(raw).is_ok() {
if let Ok(compressed) = enc.finish() {
pdf.stream(id, &compressed).filter(Filter::FlateDecode);
return;
}
}
pdf.stream(id, raw);
}
fn measure_text(text: &str, font_size: f32, font: &str) -> f32 {
if matches!(font, "F1" | "F2" | "F3" | "F4") {
return text.chars().count() as f32 * font_size * 0.60_f32;
}
// … (967 more lines in full generated file)
Custom Fonts in pdf-writer
Generated code defaults to Type1 WinAnsi Standard 14 fonts; manually emit Type0Font, CidFont, FontDescriptor, and FontFile2 stream objects using pdf-writer's typed writers for custom typefaces.
- Allocate indirect Ref IDs for Type0Font, CidFont, FontDescriptor, and FontFile2 stream.
- Bind the font ref under resources.fonts().pair(Name(b"FCustom"), font_ref).
- Invoke content.set_font(Name(b"FCustom"), font_size) inside page content streams.
Official documentation: pdf-writer docs
pdf-writer Benchmark Results
Measured on CodePDF’s 26-component, 22-page All-Components Dynamic Multi-Page Flow Stress Test (same document across all 22 libraries).
| Metric | pdf-writer | Rank (of 22) |
|---|---|---|
| Wall-clock time | 16 ms | 2nd fastest |
| CPU time | 0.04 s | 3rd lowest |
| Peak RAM | 5.9 MB | 1st lowest |
| PDF file size | 157.2 KB | 11th smallest |
- Engine type
- Low-Level / Object Editor
- Under the hood
- Low-level typed PDF serializer crate created for the Typst typesetting engine
- Runs in
- Rust (Axum, Actix Web, Tokio, WASM / Embedded)
- Verdict
- #1 lowest RAM (5.9 MB) & #2 fastest overall (16 ms for 22p; 0.2 ms single-page, 0.04s CPU) — Typst’s low-level Rust serializer.
Under the Hood & Benchmark Highlights
- Built by the Typst team as Typst’s own PDF backend — writes spec-compliant PDF object graphs and content streams directly to bytes with compile-time type safety.
- Dominated resource efficiency on the 11-page 26-component stress test: #1 lowest peak RAM (5.4 MB — 47× less than Puppeteer!) and #1 lowest CPU time (0.06s), while the 7.18s wall-clock reflects cargo run --release compilation.
- Tradeoff: zero built-in layout, text shaping, or font subsetting — CodePDF does all coordinate and wrapping math up front.
Good to Know (pdf-writer Caveats)
- Very low-level serializer: pdf-writer writes raw PDF objects, indirect Ref IDs, and content streams — there is no built-in layout engine, text shaping, or automatic font subsetting.
- Manual Unicode font embedding: Built-in Standard-14 fonts work with zero assets, but custom Unicode fonts require constructing Type0Font, CidFont, FontDescriptor, and FontFile2 streams (or pairing with subsetter/ttf-parser).
- Best suited for fixed or structured high-throughput Rust services where ~1.6 MB RSS and zero runtime bloat matter most.
Popular pdf-writer PDF Templates
- Standard B2B Commercial Invoice pdf-writer template — Enterprise commercial tax invoice with QR payment link, Currency balance KPIs, line-item ledger, and ACH wire instructions.
- Infrastructure Service Level & Uptime Report pdf-writer template — Monthly cloud SRE availability report tracking 99.996% composite uptime, P99 latency, 5-tier SLA ledger, and BGP failover digest.
- Enterprise Software License Key Certificate pdf-writer template — Ceremonial landscape software entitlement certificate with workspace/HA KPIs, module pills, and Ed25519 signed license monospace code block.
- Standard Personal Bank Account Statement pdf-writer template — Retail checking statement with opening/closing balance summary cards, chronological transaction ledger, and APY disclosure.
- Payment Receipt pdf-writer template — Clean transaction receipt with payment method details, item breakdown and verified badge.
- Standard Straight Bill of Lading (BOL) pdf-writer template — Uniform Domestic Straight Bill of Lading with SCAC/PRO Code-128 barcode, DOT HazMat controls, NMFC class table, and CDL sign-off.
All 100 CodePDF templates export to pdf-writer (Rust). Browse them by category: Career (5) · Business (6) · Education (7) · Marketing (6) · Human Resources (8) · Finance (7) · Legal (8) · Healthcare (10) · Logistics (6) · Real Estate (6) · Hospitality (4) · Operations (5) · Technology (5) · Banking (9) · Non-Profit (4) · Personal (3) · Compliance & Enterprise (1), or see the full template gallery.
pdf-writer Template FAQs
What is pdf-writer in Rust?
pdf-writer is a strongly-typed step-by-step PDF serializer maintained by the Typst team. CodePDF compiles visual layouts directly into pdf-writer Rust source code.
How much memory does generated pdf-writer code use?
Only ~1.6 MB peak RSS in benchmarks, making it ideal for high-concurrency Rust microservices and edge workers.
Does pdf-writer require external font files?
No — Standard-14 PDF fonts work immediately with zero external assets.
pdf-writer vs Other Rust PDF Libraries
- printpdf (Rust) — 44 ms, 37.7 MB RAM, 154.2 KB PDF. Pure-Rust typed Op-vector PDF builder: 44 ms across 22 pages (0.3 ms single-page, 37.7 MB RAM, 0.08s CPU) once compiled.
- lopdf (Rust) — 33 ms, 9.9 MB RAM, 132.6 KB PDF. #2 lowest RAM (9.9 MB), #3 fastest overall (33 ms for 22p; 0.3 ms single-page), & #3 smallest PDF (132.6 KB), plus edits existing PDFs.
See the full 22-library benchmark rankings · Compare all supported PDF libraries