Generating PDF Reports in Rust: Library Overview and Practical Examples
Rust is a modern systems programming language gaining popularity in web development, including server-side applications and document generation tools. If you need to generate PDF reports in Rust, you've come to the right place. In this article, we'll review the main libraries for working with PDF, their capabilities, and provide working code examples.
Why Generate PDF in Rust?
PDF is a universal format for exchanging documents, reports, and invoices. Rust provides:
- High performance and low memory consumption.
- Memory safety without a garbage collector.
- Easy integration with web frameworks (Actix, Rocket, Axum).
- Cross-platform support (Windows, Linux, macOS).
Main Libraries for PDF in Rust
1. printpdf — Low-Level PDF Manipulation
printpdf is the most popular library for creating PDF documents from scratch. It allows you to draw lines, text, images, and tables, controlling every pixel. Suitable for complex reports with custom layouts.
Installation:
[dependencies]printpdf = "0.7"Example: Simple PDF with Text
use printpdf::*;use std::fs::File;
fn main() { let (doc, page1, layer1) = PdfDocument::new( "My First Report", Mm(210.0), Mm(297.0), "Layer 1", );
let font = doc.add_builtin_font(BuiltinFont::Helvetica).unwrap();
let current_layer = doc.get_page(page1).get_layer(layer1); current_layer.use_text("Hello, world!", 24.0, Mm(20.0), Mm(280.0), &font);
doc.save(&mut File::create("output.pdf").unwrap()).unwrap();}2. genpdf — High-Level Report Generation
genpdf is built on top of printpdf and provides a convenient API for creating documents with tables, headings, paragraphs, and styles. Ideal for reports where structure matters rather than manual positioning.
Installation:
[dependencies]genpdf = "0.3"Example: Report with a Table
use genpdf::{Document, SimplePageSettings};use genpdf::elements::{Paragraph, Table, TableLayout};use genpdf::fonts::FontFamily;
fn main() { let font_family = FontFamily::from_files( "/usr/share/fonts/truetype/dejavu/", "DejaVuSans", None, ).unwrap();
let mut doc = Document::new(font_family); doc.set_title("Sales Report");
let mut table = Table::new(TableLayout::new(vec![2, 3, 3])); table.row() .element(Paragraph::new("Product")) .element(Paragraph::new("Quantity")) .element(Paragraph::new("Price"));
table.row() .element(Paragraph::new("Laptop")) .element(Paragraph::new("10")) .element(Paragraph::new("$1200"));
doc.push(table); doc.render_to_file("report.pdf").unwrap();}3. wkhtmltopdf — HTML to PDF via WebKit
If you prefer to design reports in HTML/CSS, use a wrapper around the wkhtmltopdf utility. It converts HTML templates to PDF, supporting styles, images, and JavaScript.
Installation:
[dependencies]wkhtmltopdf = "0.4"Example: HTML Conversion
use wkhtmltopdf::*;
fn main() { let html = r#" <h1>Monthly Report</h1> <p>Sales: <strong>150 000 ₽</strong></p> <table border="1"> <tr><td>January</td><td>50 000</td></tr> <tr><td>February</td><td>100 000</td></tr&g