Read the total number of pages from a PDF file. PDFluent reads the page count from the document catalog without parsing every page's content stream.
use pdfluent::prelude::*;
fn main() -> Result<()> {
let doc = PdfDocument::open("report.pdf")?;
println!("Pages: {}", doc.page_count());
Ok(())
}Add the pdfluent crate to Cargo.toml.
[dependencies]
pdfluent = "1"page_count() reads the /Count entry in the PDF page tree. It doesn't parse page content streams; for a well-formed PDF the call is effectively O(1).
use pdfluent::prelude::*;
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());Some malformed PDFs have an incorrect /Count entry. If you need a physically-verified count, iterate doc.pages() and count as you go — each iteration step resolves the next page from the tree.
let doc = PdfDocument::open("maybe_corrupt.pdf")?;
let physical_count = doc.pages().count();
let declared_count = doc.page_count();
assert_eq!(physical_count, declared_count, "declared /Count != physical pages");Loop over files and collect counts. PdfDocument opens in-memory, so drop each doc between iterations to keep allocations bounded.
use pdfluent::prelude::*;
use std::fs;
for entry in fs::read_dir("./invoices")? {
let path = entry?.path();
if path.extension().map(|e| e == "pdf").unwrap_or(false) {
match PdfDocument::open(&path) {
Ok(doc) => println!("{}: {} pages", path.display(), doc.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.