Restructure a PDF so the first page is available before the full file downloads. Known as "Fast Web View" in Acrobat.
use pdfluent::{PdfDocument, LinearizeOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("brochure.pdf")?;
doc.linearize(LinearizeOptions::default())?;
doc.save("brochure_linear.pdf")?;
println!("PDF is now linearized for fast web view");
Ok(())
}Add the pdfluent crate to Cargo.toml.
[dependencies]
pdfluent = "0.9"Load the document. Linearization rearranges the internal file structure.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("catalogue.pdf")?;LinearizeOptions lets you control hint tables and resource ordering. The defaults work well for most documents.
use pdfluent::LinearizeOptions;
let opts = LinearizeOptions::default()
.primary_page_hint_stream(true) // helps browsers fetch only page 1 first
.reorder_resources_by_page(true);Call linearize() on the document. The internal structure is rearranged so the first page and its resources appear at the beginning of the file.
doc.linearize(opts)?;
// Verify the result
println!("Linearized: {}", doc.is_linearized());Write to a new file. Serve this file from your web server with byte-range request support enabled.
doc.save("catalogue_linear.pdf")?;
println!("Ready to serve via HTTP with Range support");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.