PDF Generation in Rust: Overview of Libraries and Tools
Rust is a modern systems programming language that is increasingly used for creating high-performance server applications, including document generation. In this article, we will review the main libraries for creating PDF files in Rust, their capabilities, usage examples, and recommendations for selection.
Main Libraries for Working with PDF in Rust
The Rust ecosystem offers several mature solutions for PDF generation. Let's look at the most popular ones:
- printpdf — a low-level library with support for vector graphics, images, and fonts.
- genpdf — a high-level library for creating documents with a simple API.
- pdf_writer — a fast and safe PDF generator with minimal dependencies.
- wkhtmltopdf — a wrapper around the wkhtmltopdf utility for converting HTML to PDF.
The printpdf Library
printpdf is one of the most popular libraries for PDF generation in Rust. It provides full control over the document content: text, lines, rectangles, images, and embedded fonts.
Installation
[dependencies]printpdf = "0.7"Example: Creating a Simple PDF with Text
use printpdf::*;use std::fs::File;use std::io::BufWriter;
fn main() { // Create an A4 document let (doc, page1, layer1) = PdfDocument::new( "Example Document", Mm(210.0), Mm(297.0), "Layer 1" );
// Get the current font (built-in) let font = doc.add_builtin_font(BuiltinFont::Helvetica).unwrap();
// Draw text let current_layer = doc.get_page(page1).get_layer(layer1); current_layer.use_text( "Hello, PDF world from Rust!", 24.0, Mm(20.0), Mm(270.0), &font );
// Save to file let file = File::create("example.pdf").unwrap(); let writer = BufWriter::new(file); doc.save(&mut std::io::BufWriter::new(writer)).unwrap();}The genpdf Library
genpdf provides a higher-level API reminiscent of working with word processors. It supports paragraphs, tables, headings, and automatic line wrapping.
Installation
[dependencies]genpdf = "0.2"Example: Creating a Document with a Heading and Table
use genpdf::{Document, SimplePageSettings};use genpdf::elements::{Paragraph, Table, TableLayout};use genpdf::fonts::FontFamily;
fn main() -> Result<(), Box> { // Load a font (e.g., DejaVu Sans) let font_family = FontFamily::from_files( "/usr/share/fonts/truetype/dejavu/", "DejaVuSans", None )?;
// Create a document let mut doc = Document::new(font_family, SimplePageSettings::a4());
// Add a heading doc.push(Paragraph::new("Sales Report").aligned(genpdf::Alignment::Center));
// Create a table let mut table = Table::new(TableLayout::new(vec![2, 3, 2])); 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("50,000 RUB")); table.row() .element(Paragraph::new("Mouse")) .element(Paragraph::new("50")) .element(Paragraph::new("1,500 RUB"));
doc.push(table);
// Save doc.render_to_file("report.pdf")?; Ok(())} The pdf_writer Library
pdf_writer is a minimalist and fast library focused on low-level work with PDF structures. It