Read the total number of pages from a PDF file. PDFluent reads the page count from the document catalog without loading every page into memory.
use pdfluent::PdfDocument;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = PdfDocument::open("report.pdf")?;
println!("Pages: {}", doc.page_count());
Ok(())
}Add the pdfluent crate to Cargo.toml.
[dependencies]
pdfluent = "0.9"page_count() reads the /Count entry in the PDF page tree. This is an O(1) operation on well-formed PDFs.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("invoice.pdf")?;
let count = doc.page_count();
println!("This PDF has {} page(s)", count);If the PDF is already in memory (from a network response or database blob), use from_bytes() instead of open().
let pdf_bytes: Vec<u8> = fetch_pdf_from_database()?;
let doc = PdfDocument::from_bytes(&pdf_bytes)?;
println!("Pages: {}", doc.page_count());For a fast page count check without retaining the document, use the PdfInfo helper. It parses only the document catalog.
use pdfluent::PdfInfo;
let info = PdfInfo::from_file("large_archive.pdf")?;
println!("Pages: {}", info.page_count);
// info is dropped here - no PdfDocument held in memoryLoop over files and collect counts. PdfInfo keeps allocations low when processing many files.
use pdfluent::PdfInfo;
use std::fs;
let dir = fs::read_dir("./invoices")?;
for entry in dir {
let path = entry?.path();
if path.extension().map(|e| e == "pdf").unwrap_or(false) {
match PdfInfo::from_file(&path) {
Ok(info) => println!("{}: {} pages", path.display(), info.page_count),
Err(e) => eprintln!("{}: error - {}", path.display(), e),
}
}
}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.