Migrate from pdf-rs to PDFluent

Both libraries are written in Rust. pdf-rs is read-only. If you need to write, fill forms, sign, or validate PDFs, switch to PDFluent.

Migrating from pdf-rs to PDFluent. Install with cargo add pdfluent@1.0.0-beta.5

Migration steps

1

Replace the dependency in Cargo.toml

Remove pdf from Cargo.toml and add pdfluent. Both are pure Rust, so there are no native library changes.

pdf-rs (before)
# Cargo.toml
[dependencies]
pdf = "0.9"
PDFluent (after)
# Cargo.toml
[dependencies]
pdfluent = "0.9"
2

Open a PDF for reading

pdf-rs uses FileOptions to open a file. PDFluent uses Document::open. The error handling model is the same — both return Results.

pdf-rs (before)
use pdf::file::FileOptions;

let file = FileOptions::cached()
    .open("document.pdf")
    .unwrap();
PDFluent (after)
use pdfluent::PdfDocument;

let doc = PdfDocument::open("document.pdf")?;
3

Extract text from pages

pdf-rs requires traversing content streams manually. PDFluent provides a high-level text() method per page.

pdf-rs (before)
use pdf::file::FileOptions;
use pdf::object::*;

let file = FileOptions::cached().open("document.pdf").unwrap();

for page in file.pages() {
    let page = page.unwrap();
    // Content stream must be parsed manually
    // No high-level text extraction API
    if let Some(content) = page.contents.as_ref() {
        for op in content.operations.iter() {
            // match on operator codes to extract text
            println!("{:?}", op);
        }
    }
}
PDFluent (after)
use pdfluent::PdfDocument;

let doc = PdfDocument::open("document.pdf")?;

for i in 0..doc.page_count() {
    let text = doc.page(i)?.text()?;
    println!("{}", text);
}
4

Write and modify PDFs

pdf-rs has no write API. PDFluent supports modifying existing documents, filling forms, adding annotations, and saving. This step has no pdf-rs equivalent.

pdf-rs (before)
// pdf-rs: no write API available
// Cannot modify documents, fill forms, or save changes
PDFluent (after)
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("application.pdf")?;

// Fill AcroForm
let mut form = doc.acroform()?;
form.set_field("applicant_name", "Jane Smith")?;
form.set_field("date", "2024-04-14")?;

// Validate PDF/A before saving
let report = doc.validate_pdfa(pdfluent::PdfaLevel::A1b)?;
println!("Valid: {}", report.is_valid());

doc.save("application_filled.pdf")?;

Things to watch out for

  • !pdf-rs uses cached file access by default. PDFluent loads the document into memory. For very large files, use Document::open_streaming().
  • !pdf-rs has incomplete support for some encrypted PDFs and complex content streams. PDFluent handles these cases.

Frequently asked questions