Free Rust printpdf Templates & Op-Vector Code Generator

Pure-Rust typed Op-vector PDF builder (0.12 API) with zero C++ dependencies. Generates 1-page PDFs in 0.3 ms and 22 stress-test pages in 44 ms (0.08s CPU, 37.7 MB RAM, 154.2 KB PDF); requires explicit Cargo feature flags per image format.

Install: cargo add printpdf@0.12.5 --features "images png jpeg" | Runtime: Rust (Rust) | Version: 0.12.5 | Popularity: 190k/mo installs

Works with: Axum, Actix Web, Tokio, Cloudflare Workers Rust, CLI

How printpdf (Rust) Templates Work in CodePDF

Emits a standalone Rust main.rs assembling typed printpdf::Op vectors (SetTextCursor, WriteTextBuiltinFont, DrawPolygon, DrawLine) in Pt/Mm coordinates.

  • Targets modern printpdf 0.12 Op-vector API (and supports legacy 0.8 layer API)
  • Automates bottom-left coordinate conversion, polygon fills, and dynamic Vec<T> table loops
  • Pure memory-safe Rust ready for Axum, Actix Web, or CLI binaries

Quick Start: Generate PDFs with printpdf

Install printpdf (Rust) (Rust), then run the code CodePDF exports for any template:

cargo add printpdf@0.12.5 --features "images png jpeg"

  1. Run cargo add printpdf@0.12.5 --features "images png jpeg" in your Rust project. Enable the format features (gif, bmp, svg, ...) your document actually uses — printpdf decodes each image format behind its own feature flag.
  2. Save the generated file as src/main.rs (or merge it into an existing binary).
  3. Run cargo run to produce document.pdf in the project root.
  4. Coordinates are emitted in points with a bottom-left origin; the canvas layout is flipped automatically.
  5. The generated code targets printpdf 0.12.x — pin the same major version in Cargo.toml.

Example printpdf Output (Standard B2B Commercial Invoice)

Compiled directly from the Standard B2B Commercial Invoice template (Rust):

#![allow(dead_code, unused_variables, unused_mut)]
use printpdf::*;
use std::collections::HashMap;

// This code is generated using codepdf.dev
// Target: printpdf v0.12.5 (cargo add printpdf)

fn estimate_text_width(text: &str, font_size: f32, is_bold: bool, is_mono: bool) -> f32 {
    if is_mono {
        return text.chars().count() as f32 * font_size * 0.60_f32;
    }
    let mut em = 0.0_f32;
    for c in text.chars() {
        em += match c {
            'i' | 'l' | 'I' | '!' | '|' | '.' | ',' | ':' | ';' | '\'' | ' ' => 0.28,
            'j' | 'f' | 't' | 'r' | '(' | ')' | '[' | ']' | '-' | '/' => 0.34,
            'm' | 'w' | 'M' | 'W' | '@' | '%' | '&' => 0.84,
            'A'..='Z' => 0.68,
            '0'..='9' => 0.56,
            _ => 0.52,
        };
    }
    em * font_size * if is_bold { 1.06 } else { 1.0 }
}

fn draw_rounded_rect(
    ops: &mut Vec<Op>,
    x: f32,
    y: f32,
    w: f32,
// … (1029 more lines in full generated file)

Custom Fonts in printpdf

Generated code defaults to BuiltinFont Standard 14 fonts; parse .ttf/.otf font bytes via ParsedFont::from_bytes() and register the resulting FontId on PdfDocument.

  1. Store custom .ttf/.otf binaries under ./fonts/ or embed via include_bytes!().
  2. Parse with ParsedFont::from_bytes(&bytes, 0, &mut warnings) and call doc.add_font(&parsed).
  3. Pass the returned FontId to Op::SetFontSize and Op::WriteText operations.

Official documentation: printpdf docs

printpdf Benchmark Results

Measured on CodePDF’s 26-component, 22-page All-Components Dynamic Multi-Page Flow Stress Test (same document across all 22 libraries).

printpdf (Rust) stress-test results and rank out of 22 PDF libraries
MetricprintpdfRank (of 22)
Wall-clock time44 ms5th fastest
CPU time0.08 s4th lowest
Peak RAM37.7 MB6th lowest
PDF file size154.2 KB10th smallest
Engine type
Coordinate Canvas
Under the hood
Pure-Rust typed Op-vector PDF builder (0.12 API) with feature-gated image codecs
Runs in
Rust (Axum, Actix Web, Tokio, CLI)
Verdict
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.

Under the Hood & Benchmark Highlights

  • Pure memory-safe Rust emitting typed printpdf::Op vectors — zero C/C++ bindings or browser binaries.
  • Once compiled, runtime execution is tiny (0.17s CPU, 34.8 MB RAM, 124.7 KB across 11 pages), though cargo run --release compile time dominated our one-shot wall-clock measurement (8.93s).
  • Requires explicit Cargo feature flags per image codec (--features "images png jpeg") and broke API compatibility between 0.8 and 0.12.

Good to Know (printpdf Caveats)

  • Pin exact version (0.8 vs. 0.12 breaking change): printpdf 0.12 switched from the legacy layer API to typed Op vectors — match the version selected in CodePDF with your Cargo.toml.
  • Explicit Cargo image feature flags: Each image format is gated behind its own feature (--features "images png jpeg"); omitting a format causes runtime decode failures.
  • Ahead-of-time Cargo build vs. runtime speed: Initial cargo build --release takes several seconds, while pure PDF generation runs in 0.3 ms single-page and 44 ms across 22 pages (consider pdf-writer at 16 ms / 5.9 MB RAM for lowest footprint).

Popular printpdf PDF Templates

All 100 CodePDF templates export to printpdf (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.

printpdf Template FAQs

Which version of Rust printpdf does CodePDF target?

CodePDF targets current stable printpdf 0.12.5 (Op-vector API) by default and also supports printpdf 0.8 in the version selector.

Does exported Rust printpdf code compile cleanly with cargo build?

Yes — CodePDF's Rust generators are regression-tested against cargo to ensure valid borrows, escaping, and %%EOF PDF output.

How do printpdf, lopdf, and pdf-writer compare in Rust?

pdf-writer offers the fastest compile/run speed and ultra-low 1.6 MB RAM; lopdf produces the smallest 2.2 KB PDFs; printpdf offers a high-level Op-vector graphics model. CodePDF exports to all three!

printpdf vs Other Rust PDF Libraries

  • 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.
  • 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