Rust library for generating PDF documents. Inspired by the JavaScript pdfmake library.
- Pure Rust, no C dependencies
- Builder API for easy document construction
- Tables with cell spanning (colSpan, rowSpan)
- Lists (ordered, unordered)
- Images (PNG, JPEG)
- SVG rendering
- QR codes
- Text wrapping and alignment
- Page headers and footers (static and dynamic)
- CJK font support (register TTF files)
- Font subsetting (only used glyphs are embedded)
- Named styles for consistent formatting
- Feature flags for optional components
Add to your Cargo.toml:
[dependencies]
pdfmake_rust = "0.1.0"use pdfmake_rust::{Document, DocumentNode, Margins, PageSize, PdfMake, TextNode};
use std::fs;
fn main() {
let doc = Document::builder()
.page_size(PageSize::a4())
.page_margins(Margins::all(40.0))
.content(DocumentNode::Text(TextNode::new("Hello, World!")))
.build();
let pdf = PdfMake::new();
let bytes = pdf.render(&doc).expect("Failed to render PDF");
fs::write("output.pdf", &bytes).expect("Failed to write PDF");
}See the examples/ directory for complete examples:
hello_world- Basic documenttable- Table with spanninginvoice- Real-world invoice layoutmulti_column- Multi-column layout
Run examples with:
cargo run --example hello_world
cargo run --example table
cargo run --example invoiceOptional components can be enabled/disabled via feature flags:
[dependencies]
pdfmake_rust = { version = "0.1.0", default-features = false, features = ["svg", "image", "qr"] }| Feature | Default | Description |
|---|---|---|
svg |
Yes | SVG rendering support |
image |
Yes | PNG/JPEG image support |
qr |
Yes | QR code generation |
encryption |
No | PDF encryption (RC4, 128-bit) |
tagged-pdf |
No | Tagged PDF for accessibility (planned) |
The rendering pipeline follows these stages:
Document → normalize → measure → layout → render → PDF bytes
| Stage | Module | Purpose |
|---|---|---|
| Model | model.rs |
Document structure and builder API |
| Normalize | normalize.rs |
Flatten document tree into blocks |
| Measure | measure.rs |
Calculate text metrics, line wrapping |
| Layout | layout.rs |
Position blocks into pages |
| Render | render.rs |
Emit PDF 1.4 byte stream |
Register CJK fonts to support Chinese, Japanese, Korean text:
let doc = Document::builder()
.font("SimHei", "path/to/SimHei.ttf")
.content(DocumentNode::Text(TextNode::new("你好世界")))
.build();This project is inspired by and references the JavaScript pdfmake library created by bpampuch and maintained by liborm85. The Rust implementation is written from scratch while following the same design principles.
Licensed under the Apache License, Version 2.0. See LICENSE for details.
The original JavaScript pdfmake library is licensed under the MIT License.