How-to guides/Optimization

Detect if a PDF is linearized (web-optimized) in Rust

Read the linearization dictionary from the start of a PDF file to determine if it is structured for fast web delivery.

rust
use pdfluent::Document;

fn main() -> pdfluent::Result<()> {
    let doc = Document::open("file.pdf")?;

    if doc.is_linearized() {
        println!("PDF is linearized (web-optimized).");
        if let Some(info) = doc.linearization_info() {
            println!("File length hint: {}", info.file_length());
            println!("First page end:   {}", info.first_page_end_offset());
        }
    } else {
        println!("PDF is not linearized.");
    }

    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the PDF

Linearization is checked by inspecting the first object in the file. No full parse is required.

rust
let doc = Document::open("file.pdf")?;
2

Check the linearization flag

is_linearized() reads the first cross-reference table and checks for the /Linearized dictionary key.

rust
if doc.is_linearized() {
    println!("Linearized.");
} else {
    println!("Not linearized.");
}
3

Read linearization parameters

The linearization dictionary contains hints used by HTTP range-request-based PDF viewers. linearization_info() exposes the key fields.

rust
if let Some(info) = doc.linearization_info() {
    println!("File length:       {}", info.file_length());
    println!("First page number: {}", info.first_page_number());
    println!("First page end:    {}", info.first_page_end_offset());
    println!("Hint stream start: {:?}", info.hint_stream_offset());
}
4

Validate the linearization hints

If the file has been modified after linearization, the hint offsets may be stale. validate_linearization() checks offsets against the actual file structure.

rust
let valid = doc.validate_linearization()?;
if !valid {
    println!("Warning: linearization hints are out of date.");
    println!("Re-linearize for optimal web performance.");
}
5

Linearize a non-linearized PDF

Call doc.linearize() to produce a linearized copy. This is typically done as the final step before publishing.

rust
if !doc.is_linearized() {
    let mut doc = Document::open("file.pdf")?;
    doc.linearize()?;
    doc.save("web_optimized.pdf")?;
}

Notes and tips

  • A PDF modified after linearization becomes de-linearized. The /Linearized key is still present but the hints are stale and should not be trusted.
  • Linearization only matters for HTTP range-request delivery. For local file access or downloads, it has no performance benefit.
  • Linearized PDFs typically have the first page objects near the start of the file, allowing browsers to display the first page before the full download completes.
  • validate_linearization() is a read operation and is safe to call on any PDF, linearized or not.

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