mr-pdf πβ¨
mr-pdf is a high-performance, lightweight PDF generation library for Rust. Designed with a focus on low memory footprint and premium aesthetics, it allows you to create stunning PDF documents with a fluent, developer-friendly API.
π Key Features
- Low Memory Footprint: Uses a streaming architecture that writes document sections directly to the output. Memory usage scales with the complexity of a single page, not the entire document.
- Premium Aesthetics: Built-in support for advanced layouts, charts, and modern typography.
- Flexible Layout Engine: Flutter-inspired rows and columns (
Pdf::row,Pdf::column) for complex designs. - Interactive Elements: Support for clickable hyperlinks in text, tables, and images.
- Rich Text & Inline Style: Support for bold, colors, and mixed styles within table cells and paragraphs.
- QR Code Generation: Native vector-based QR code generation (Optional).
- Watermarks: Global text watermarks with customizable opacity, angle, and size.
- Dynamic Charts: Generate beautiful Bar, Line, and Pie charts directly in your PDF (Optional).
π¦ Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
To enable extra capabilities, use the corresponding features:
[]
# Basic installation
= "0.1.4"
# Full installation (Charts + QR Code)
= { = "0.1.4", = ["chart", "qrcode"] }
π οΈ Quick Start
use ;
π Visual Components
Beautiful Charts (Opt-in)
Easily visualize data with built-in chart types by enabling the chart feature:
- Bar Charts: For comparison.
- Line Charts: For trends.
- Pie Charts: For proportions.
Flexible Image & SVG Sizing
use SizeExt;
// SVG with custom positioning and size (supports Points or Percent)
pdf.svg
.width
.render?;
// Images (PNG/JPG) with specific position and size
pdf.image
.position
.size
.render?;
Tables with Spanning & Advanced Headers
use ;
pdf.table?;
Page Watermarks
Set a global watermark that appears on every page of your document:
pdf.set_watermark;
Low-Memory Streaming Tables
If you need to process thousands or even millions of rows from a database or stream, keeping them all in memory is inefficient. The StreamingTable API consumes only enough memory for a single row buffer and writes immediately to the PDF!
let mut builder = new;
builder.widths
.header
.repeat_header;
let mut stream_table = builder.start?; // Renders the header!
while let Some = my_db_rx.recv.await
// Drops map to table end automatically
Automatic Font Subsetting (O(1) Memory Usage)
mr-pdf is extremely memory efficient. Behind the scenes, we automatically collect the unique characters you use. When you call .finish(), we invoke a subsetting engine (fontcull) to slice the raw embedded TTF files perfectly.
This means you can embed large 2MB ttf Asian/Arabic filesβand if you only type "Hello World", the embedded font size inside the PDF will shrink to just ~15 KB, reducing your total file size by up to 90%!
PDF Security & Password Protection
Keep your documents private with 128-bit encryption. You can set an Owner password to restrict permissions (printing, copying) and a User password to lock the file entirely.
pdf.set_encryption;
Writing Documents in Markdown
You can use standard markdown syntax to rapidly compose large text blocks. MR-PDF automatically maps elements like Headings, Paragraphs, Lists, and Code blocks into beautifully laid-out PDF text blocks!
let markdown_text = "
# MR-PDF Markdown
This lets you quickly write **bold** and *italic* text concepts!
- Support for Headings (Auto scalable sizes)
- Support for Paragraph wrapping
- Bullet lists
fn demo() {}
";
pdf.markdown(markdown_text)
.size(12.0)
.font("MyRegisteredFont") // Use a specific font for the block
.render()?;
ποΈ Performance Objectives
mr-pdf is tailored for environments where resource efficiency is paramount:
- Direct Writing: Offsets and object metadata are managed centrally, but content streams are flushed directly to the disk/socket once a page is complete.
- Minimal Buffering: Does not store the entire document DOM in memory.
- OptimizedTTF: Only necessary font metrics are loaded into memory for layout calculation.
π License
MIT / Apache-2.0