How-to guides/Metadata

Write PDF metadata in Rust

Set title, author, subject and keywords on any PDF via the MetadataMut builder. Changes are buffered until commit(), then flushed to the document.

rust
use pdfluent::prelude::*;

fn main() -> Result<()> {
    let mut doc = PdfDocument::open("report.pdf")?;

    doc.metadata_mut()
        .set_title("Q3 Financial Report")
        .set_author("Finance Team")
        .set_subject("Quarterly earnings, consolidated")
        .set_keywords(&["finance", "q3", "2026"])
        .commit()?;

    doc.save("report_tagged.pdf")?;
    Ok(())
}
Install:cargo add pdfluent@1.0.0-beta.5Download SDK →

Step by step

1

Open the PDF for mutation

Open with a mutable binding. MetadataMut borrows &mut on the document for the duration of the builder chain.

rust
use pdfluent::prelude::*;

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

Chain setters via metadata_mut()

metadata_mut() returns a MetadataMut builder. Each setter returns &mut Self so you can chain them. Changes are buffered locally — nothing is written until commit().

rust
let mut meta = doc.metadata_mut();
meta.set_title("Q3 Financial Report")
    .set_author("Finance Team")
    .set_subject("Quarterly earnings")
    .set_keywords(&["finance", "q3", "2026"]);
3

Commit to flush changes into the document

commit() writes the buffered changes to the Info dictionary. It returns Result<()>; call it explicitly so you can handle write errors. MetadataMut also flushes on drop, but in that path errors are silenced.

rust
doc.metadata_mut()
    .set_title("Q3 Financial Report")
    .set_author("Finance Team")
    .commit()?;
4

Save the tagged document

save() writes the PDF to disk. The metadata changes are part of that write; no separate flush step required.

rust
doc.save("report_tagged.pdf")?;

Notes and tips

  • metadata_mut() is infallible — a PDF always has an Info dictionary slot, created lazily by commit() if absent.
  • The 1.0 setter surface is set_title, set_author, set_subject, set_keywords. Producer, creator, creation_date and modification_date are read-only in 1.0 (they're written by PDFluent at save time).
  • keywords takes a &[&str] and is serialised joined with commas in the PDF's /Keywords entry, which is the common convention.
  • Non-ASCII values (titles with accented characters, CJK text) round-trip correctly — PDFluent picks UTF-16BE with BOM when needed.
  • Writing metadata does not require any specific capability; it's part of the core SDK surface and available at every tier.

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions