How-to guides/Document Info

Read the PDF spec version from a document in Rust

Useful for pre-flight checks, compatibility filtering, and document auditing pipelines.

rust
use pdfluent::PdfDocument;

fn main() -> pdfluent::Result<()> {
    let doc = PdfDocument::open("file.pdf")?;
    let v = doc.version();
    println!("PDF {}.{}", v.major, v.minor);
    Ok(())
}

Step by step

1

Add PDFluent to Cargo.toml

The base crate includes all document metadata and version inspection functions.

rust
# Cargo.toml
[dependencies]
pdfluent = "1.0.0-beta.8"
2

Open the document and call pdf_version()

pdf_version() reads the %PDF-x.y header from the first 8 bytes of the file. It does not require full document parsing.

rust
use pdfluent::PdfDocument;

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

println!("{}.{}", version.major, version.minor);
3

Compare versions with the PdfVersion enum

Use predefined constants to write readable version checks. PdfVersion implements PartialOrd.

rust
let v = doc.version();

match (v.major, v.minor) {
    (1, 0) => println!("Very old document"),
    (1, 4) => println!("PDF 1.4 - supports transparency"),
    (1, 5) => println!("PDF 1.5 - supports object streams"),
    (1, 6) => println!("PDF 1.6 - supports AES-128"),
    (1, 7) => println!("PDF 1.7 - supports AES-256"),
    (2, 0) => println!("PDF 2.0 - latest standard"),
    _ => println!("Other version: {}.{}", v.major, v.minor),
}
4

Read version without fully opening the document

Use PdfDocument::peek_version() to read only the header bytes. This is faster when you need to filter files before loading them.

rust
use pdfluent::PdfDocument;

let doc = PdfDocument::open("document.pdf")?;
let version = doc.version();
println!("PDF {}.{}", version.major, version.minor);

// Only proceed for documents that are at least PDF 1.6
if (version.major, version.minor) >= (1, 6) {
    // ...
}

Notes and tips

  • The version in the %PDF header is the declared version. Some tools update the DocumentCatalog Version entry without changing the header. PDFluent returns the higher of the two values.
  • PDF 2.0 (ISO 32000-2) is the current standard. PDF 1.7 (ISO 32000-1) is still the most common version in real-world documents.
  • Versions below 1.4 do not support transparency groups. Versions below 1.5 do not support cross-reference streams.

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