Rust PDF Guide: Complete Tutorial with Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PDF in Rust: Complete Guide to Creating Documents with the printpdf Library

1. What It Is and Why You Need It

printpdf is a powerful and flexible library for the Rust programming language, designed for creating PDF documents from scratch. Unlike many other tools that simply convert HTML or Markdown to PDF, printpdf provides a low-level API that gives you full control over every aspect of the document: from text placement and vector graphics to embedding fonts and images. This library is written in pure Rust with no external dependencies (aside from the standard library), making it an ideal choice for systems where memory safety, performance, and portability are critical.

Why use printpdf? In today's world, PDF is the universal format for document exchange. Server-side PDF generation is required in a wide range of scenarios: creating invoices and receipts in ERP systems, generating reports and analytical dashboards, producing certificates and diplomas, and building printable forms and templates. Rust, with its focus on performance and reliability, is perfectly suited for these tasks. printpdf lets you do this efficiently, without the overhead of launching external processes (like wkhtmltopdf) or using heavyweight browser engines.

Key features of the library include: support for all standard PDF objects (pages, fonts, images, graphics), Unicode support and TrueType/OpenType font embedding, multi-page document creation, RGB, CMYK, and Grayscale color spaces, and precise element positioning using the PDF coordinate system. This isn't a "quick conversion" tool—it's a "constructor" for professional document creation.

2. Installation

To install the printpdf library in your Rust project, use the standard Cargo package manager. You need to add the dependency to your project's Cargo.toml file.

Open a terminal in your project's root folder and run the command:

cargo add printpdf

Or manually add the following line to the [dependencies] section of your Cargo.toml:

[dependencies]
printpdf = "0.7"

After this, Cargo will automatically download the library and all its internal dependencies the next time you build your project. Make sure you have Rust (rustc) and Cargo installed. If not, visit rustup.rs to install them.

3. Quick Start — Minimal Working Example

Let's create a simple PDF document with one page and some text. This example demonstrates the basic workflow with the library: creating a document, adding a page, selecting a font, drawing text, and saving the file.

use printpdf::*;
use std::fs::File;
use std::io::BufWriter;

fn main() {
    // Create a new PDF document
    let (doc, page1, layer1) = PdfDocument::new(
        "My First PDF in Rust",
        Mm(210.0), // Page width (A4)
        Mm(297.0), // Page height (A4)
        "Layer 1", // Layer name
    );

    // Get the default font (built-in Helvetica)
    let font = doc.add_builtin_font(BuiltinFont::Helvetica).unwrap();

    // Start working with the layer
    let current_layer = doc.get_page(page1).get_layer(layer1);

    // Use a 48pt font in black color
    current_layer.use_text("Hello, Rust PDF!", 48.0, Mm(50.0), Mm(200.0), &font);

    // Save the document to a file
    let file =                    

Recommendations