Read the linearization dictionary from the start of a PDF file to determine if it is structured for fast web delivery.
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(())
}Linearization is checked by inspecting the first object in the file. No full parse is required.
let doc = Document::open("file.pdf")?;is_linearized() reads the first cross-reference table and checks for the /Linearized dictionary key.
if doc.is_linearized() {
println!("Linearized.");
} else {
println!("Not linearized.");
}The linearization dictionary contains hints used by HTTP range-request-based PDF viewers. linearization_info() exposes the key fields.
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());
}If the file has been modified after linearization, the hint offsets may be stale. validate_linearization() checks offsets against the actual file structure.
let valid = doc.validate_linearization()?;
if !valid {
println!("Warning: linearization hints are out of date.");
println!("Re-linearize for optimal web performance.");
}Call doc.linearize() to produce a linearized copy. This is typically done as the final step before publishing.
if !doc.is_linearized() {
let mut doc = Document::open("file.pdf")?;
doc.linearize()?;
doc.save("web_optimized.pdf")?;
}No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.