
Open Source Rust Library for PDF Processing
Read, Edit, Write & Render PDF Documents with a Safe, Efficient Rust API.
What is pdf?
pdf-rs is an open source Rust library designed for reading, parsing, and manipulating PDF documents with high safety and performance. It provides a low-level, idiomatic Rust API that enables developers to inspect PDF structures, extract content (such as text, images, and fonts), and modify or write new PDF files. The library avoids unsafe code wherever possible and is built around Rust’s ownership and type system to prevent common memory and concurrency errors. It supports core PDF features like parsing cross-reference tables, handling streams, and decoding filters such as Flate, ASCIIHex, and ASCII85.
The project emphasizes correctness and extensibility, with modular crates like `pdf::read`, `pdf::write`, and `pdf::content` for granular control. It integrates well with rendering engines (e.g., Pathfinder) and text extraction tools, making it suitable for building PDF viewers, analyzers, and converters. While PDF writing remains experimental, the library is actively used in production for reading and inspection tasks. Contributions are welcome—especially test files and bug reports—to improve compatibility with real-world PDFs. Join the community on Zulip to discuss features, issues, or improvements.
Getting Started with pdf
To begin using the pdf library in your Rust project, add it to your Cargo.toml dependencies. The core crate is pdf, and optional sub-crates like pdf::read and pdf::write provide granular functionality. Install via cargo add pdf or manually add [dependencies] entries. For examples, run cargo run --example read -- tests/files/sample.pdf. The project uses a Cargo workspace, so building the full suite requires --package flags for subcomponents. Documentation and examples are included in the repository to help you get up and running quickly.
Install pdf via Cargo
[dependencies]
pdf = "0.6"Parse & Extract PDF Content
The pdf-rs library enables deep parsing of PDF documents, including extraction of text, images, fonts, and metadata. It decodes streams using filters like Flate, ASCIIHex, and ASCII85, and handles cross-reference tables and object streams. Developers can traverse the PDF object tree to inspect pages, annotations, outlines, and embedded resources. The API is designed to be safe and ergonomic, leveraging Rust’s type system to prevent invalid access. For instance, you can load a PDF, iterate over pages, and extract embedded images as raw data or decoded formats (JPEG, TIFF, PNG). This makes it ideal for building PDF analyzers, archival tools, or content extraction pipelines without relying on external binaries.
How to Extract Images from a PDF File via Rust?
use pdf::file::File;
use pdf::object::*;
let mut file: File<_> = File::open("example.pdf")?;
for page in file.pages() {
let page_obj = page?;
if let Some(resources) = page_obj.resources()? {
for (name, stream) in resources.xobject_iter()? {
if let Some(image) = stream.to_image()? {
// Save or process image data
println!("Found image: {}", name);
}
}
}
}Modify & Write PDF Documents
While PDF writing remains experimental, pdf-rs provides a structured API for creating and editing PDF files. Developers can add new pages, insert content streams, embed fonts, and update metadata. The library supports low-level operations like appending objects, updating cross-reference tables, and writing linearized PDFs. You can programmatically generate PDFs from scratch or modify existing ones—such as adding watermarks, redacting text, or updating form fields. The modular design separates reading, writing, and content handling, allowing fine-grained control. This capability is especially useful for building PDF generation tools, report engines, or document automation systems where full control over output is required.
How to Create a Simple PDF with Text via Rust?
use pdf::file::File;
use pdf::object::*;
use pdf::content::*;
let mut file = File::new();
let page = file.add_page(612.0, 792.0, None);
let mut stream = ContentStream::new();
stream.text_start().text_move(100.0, 700.0);
stream.text_show("Hello, PDF!")?;
page.contents = Some(file.add_object(stream));
file.save("output.pdf")?;Render PDFs with Pathfinder
pdf-rs integrates seamlessly with the Pathfinder rendering engine for high-performance PDF rendering. The companion crate pdf_render provides a renderer that converts PDF content into GPU-accelerated vector primitives. This enables smooth zooming, panning, and printing in desktop or web-based viewers. The renderer supports text, images, paths, and gradients, and can output to OpenGL, Vulkan, or software backends. Developers can embed the viewer in their applications using the provided minimal viewer example. This integration makes pdf-rs suitable for building full-featured PDF viewers, annotation tools, or collaborative document platforms requiring real-time rendering.
How to Render a PDF Page via Pathfinder?
use pdf_render::{Renderer, RenderParams};
use std::fs::File;
let mut file = File::open("example.pdf")?;
let mut renderer = Renderer::new();
let page = renderer.load_page(&mut file, 0)?;
renderer.render_page(&page, &RenderParams::default())?;Inspect & Analyze PDF Structure
The inspect-prim tool included in the pdf-rs ecosystem provides an interactive visualization of PDF internals as a hierarchy of primitives. Developers can explore object trees, stream contents, and rendering operations in real time. This is invaluable for debugging compatibility issues, understanding PDF structure, or validating file integrity. The tool supports zooming, filtering, and exporting structural data. Combined with the modular crate design, it allows deep inspection without writing custom code. This capability is essential for PDF tooling, forensics, and compliance checking—especially when handling legacy or malformed documents that require careful analysis.
How to Run the PDF Inspector Tool?
# Clone and build inspect-prim
git clone https://github.com/pdf-rs/inspect-prim
cd inspect-prim
cargo run -- example.pdf