Free Rust lopdf Templates (Smallest 2.2 KB PDF Output)
Low-level Rust PDF object-model and stream-operator builder that both generates compact PDFs in 33 ms across 22 pages (0.3 ms / 1.7 KB single-page; 132.6 KB stress PDF, 9.9 MB RAM) and loads/merges/stamps existing PDFs.
Install: cargo add lopdf@0.44 | Runtime: Rust (Rust) | Version: 0.44 | Popularity: 450k/mo installs
Works with: Rust, Axum, Actix Web, CLI Tools
How lopdf (Rust) Templates Work in CodePDF
Generates a Rust program constructing lopdf::Document, Dictionary, Stream, and Operation primitives (`BT`, `Tf`, `Td`, `Tj`, `re`, `f`) directly.
- Eliminates hand-writing raw PostScript/PDF content stream operators (`BT`, `ET`, `rg`, `re`)
- Smallest generated PDF payload (2.2 KB) across all languages
- Easy to combine generated pages with existing PDF documents loaded via lopdf::Document::load()
Quick Start: Generate PDFs with lopdf
Install lopdf (Rust) (Rust), then run the code CodePDF exports for any template:
cargo add lopdf@0.44
- Run cargo add lopdf@0.44 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.
- lopdf is a low-level builder — the generated code drives raw PDF content streams and dictionaries directly.
- The generated code targets lopdf 0.44.x — pin the same major version in Cargo.toml.
Example lopdf Output (Professional Invoice)
Compiled directly from the Professional Invoice template (Rust):
#![allow(unused_variables)]
#![allow(unused_mut)]
#![allow(unused_imports)]
#![allow(dead_code)]
// Generated by CodePDF — https://codepdf.dev
// lopdf: add lopdf = "0.44" to your Cargo.toml
use lopdf::{Document, Object, ObjectId, Stream, dictionary};
use std::collections::HashMap;
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;
}
const HELV: [u16; 95] = [278,278,355,556,556,889,667,191,333,333,389,584,278,333,278,278,556,556,556,556,556,556,556,556,556,556,278,278,584,584,584,556,1015,667,667,722,722,667,611,778,722,278,500,667,556,833,722,778,667,778,722,667,611,722,667,944,667,667,611,278,278,278,469,556,333,556,556,500,556,556,278,556,556,222,222,500,222,833,556,556,556,556,333,500,278,556,500,722,500,500,500,334,260,334,584];
const HELV_B: [u16; 95] = [278,333,474,556,556,889,722,238,333,333,389,584,278,333,278,278,556,556,556,556,556,556,556,556,556,556,333,333,584,584,584,611,975,722,722,722,722,667,611,778,722,278,556,722,611,833,722,778,667,778,722,667,611,722,667,944,667,667,611,333,278,333,584,556,333,556,611,556,611,556,333,611,611,278,278,556,278,889,611,611,611,611,389,556,333,611,556,778,556,556,500,389,280,389,584];
const TIMES: [u16; 95] = [250,333,408,500,500,833,778,180,333,333,500,564,250,333,250,278,500,500,500,500,500,500,500,500,500,500,278,278,564,564,564,444,921,722,667,667,722,611,556,722,722,333,389,722,611,889,722,722,556,722,667,556,611,722,722,944,722,722,611,333,278,333,469,500,333,444,500,444,500,444,333,500,500,278,278,500,278,778,500,500,500,500,333,389,278,500,500,722,500,500,444,480,200,480,541];
const TIMES_B: [u16; 95] = [250,333,555,500,500,1000,833,278,333,333,500,570,250,333,250,278,500,500,500,500,500,500,500,500,500,500,333,333,570,570,570,500,930,722,667,722,722,667,611,778,778,389,500,778,667,944,722,778,611,778,722,556,667,722,722,1000,722,722,667,333,278,333,581,500,333,500,556,444,556,444,333,500,556,278,333,556,278,833,556,500,556,556,444,389,333,556,500,722,500,500,444,394,220,394,520];
const TIMES_I: [u16; 95] = [250,333,420,500,500,833,778,214,333,333,500,675,250,333,250,278,500,500,500,500,500,500,500,500,500,500,333,333,675,675,675,500,920,611,611,667,722,611,611,722,722,333,444,667,556,833,667,722,611,722,611,500,556,722,611,833,611,556,556,389,278,389,422,500,333,500,500,444,500,444,278,500,500,278,278,444,278,722,500,500,500,500,389,389,278,500,444,667,444,444,389,400,275,400,541];
const TIMES_BI: [u16; 95] = [250,389,555,500,500,833,778,278,333,333,500,570,250,333,250,278,500,500,500,500,500,500,500,500,500,500,333,333,570,570,570,500,832,667,667,667,722,667,667,722,778,389,500,667,611,889,722,722,611,722,667,556,611,722,667,889,667,611,611,333,278,333,570,500,333,500,500,444,500,444,333,500,556,278,278,500,278,778,556,500,500,500,389,389,278,556,444,667,500,444,389,348,220,348,570];
let tbl: &[u16; 95] = match font {
"F6" | "F8" => &HELV_B,
"F9" => &TIMES,
"F10" => &TIMES_B,
"F11" => &TIMES_I,
"F12" => &TIMES_BI,
_ => &HELV,
};
// … (1055 more lines in full generated file)
Custom Fonts in lopdf
Generated code defaults to Type1 Standard 14 fonts (/F1–/F12); manually embed a Type0/CIDFontTrueType FontDescriptor + FontFile2 stream and register its ref in page /Resources/Font for custom typefaces.
- Write your TTF binary bytes into a Flate-compressed FontFile2 stream object.
- Link it to a FontDescriptor and CIDFont dictionary inside your Type0 font object.
- Register the font key in /Resources/Font and reference it in content stream Tf operators.
Official documentation: lopdf docs
lopdf 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 | lopdf | Rank (of 22) |
|---|---|---|
| Wall-clock time | 33 ms | 3rd fastest |
| CPU time | 0.03 s | 2nd lowest |
| Peak RAM | 9.9 MB | 2nd lowest |
| PDF file size | 132.6 KB | 3rd smallest |
- Engine type
- Low-Level / Object Editor
- Under the hood
- Low-level Rust PDF document object model (DOM) & raw content-stream operator builder
- Runs in
- Rust (Axum, Actix Web, CLI)
- Verdict
- #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.
Under the Hood & Benchmark Highlights
- Constructs raw PDF dictionaries and PostScript-style stream operators (BT, Tf, Td, Tj, re) directly in Rust — producing the smallest single-page PDF of all 22 libraries (2.2 KB).
- Hit #2 lowest RAM (9.3 MB) and #2 lowest CPU time (0.11s) on the 11-page 26-component stress test (110.9 KB PDF; 4.99s wall-clock includes cargo run --release).
- Unlike pdf-writer, lopdf can also load, inspect, merge, and stamp existing PDF files in the same crate.
Good to Know (lopdf Caveats)
- Raw PDF object & stream API: Operates directly on PDF dictionaries and PostScript-style operators (BT, Tf, Td, Tj, re) with no layout or text-shaping engine.
- Manual Unicode fonts: Standard-14 fonts are WinAnsi-only; embedding UTF-8 TrueType fonts requires constructing Type0/CIDFont streams in /Resources/Font.
- Best when you need to manipulate existing PDFs (merge, inspect, stamp) alongside generating pages in Rust.
Popular lopdf PDF Templates
- Professional Invoice lopdf template — Line-item invoice with billing details, totals and payment terms.
- Payment Receipt lopdf template — Clean transaction receipt with payment method details, item breakdown and verified badge.
- Certificate of Completion lopdf template — Landscape certificate with decorative border, recipient name and signatures.
- Mutual Non-Disclosure Agreement (MNDA) lopdf template — Bilateral confidentiality agreement built with non-tabular numbered covenant cards, trade-secret carve-outs, and dual signatures.
- Standard Monthly Payslip lopdf template — Itemized monthly payroll slip reconciling gross earnings, statutory tax withholdings, and direct-deposit net pay.
- Quality Assurance Inspection & Lot Certificate (CoA) lopdf template — AS9100D / EN 10204 Type 3.1 Certificate of Conformance with lot barcode, ASTM mechanical/NDT test ledger, and DFARS checkboxes.
All 100 CodePDF templates export to lopdf (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.
lopdf Template FAQs
Why use lopdf for PDF templates in Rust?
lopdf gives you full read/write access to the PDF object tree so you can generate tiny 2.2 KB template pages and merge, stamp, or encrypt existing PDFs in the same crate.
Do I need to know PDF stream operators like BT and Tj to use this?
Not at all — design on CodePDF's visual canvas and CodePDF emits all lopdf Operation vectors automatically.
Which lopdf version is supported?
CodePDF generates code for current lopdf 0.44.
lopdf 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.
- pdf-writer (Rust) — 16 ms, 5.9 MB RAM, 157.2 KB PDF. #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.
See the full 22-library benchmark rankings · Compare all supported PDF libraries