Pick individual pages or non-contiguous sets and write them to a new PDF. Works with page numbers, page labels, or a custom predicate.
use pdfluent::PdfDocument;
fn main() -> pdfluent::Result<()> {
let doc = PdfDocument::open("report.pdf")?;
let subset = doc.extract_pages(0..3)?;
subset.save("first-three.pdf")?;
Ok(())
}Open the document you want to extract pages from. Page count is available immediately after opening.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("source.pdf")?;
println!("{} pages total", doc.page_count());Pass a slice of 1-based page numbers. Pages appear in the output in the order given, so you can reorder them freely.
// Extract a contiguous page range (0-based, end-exclusive): pages 2-4
let extracted = doc.extract_pages(1..4)?;
extracted.save("pages_2_to_4.pdf")?;If the PDF uses custom page labels such as "i", "ii", "A-1", pass label strings instead of integers.
// Split into single-page documents
for (i, page) in doc.split_pages()?.into_iter().enumerate() {
page.save(format!("page_{}.pdf", i + 1))?;
}Use extract_pages_where() to filter programmatically. The closure receives a PageInfo struct with page number, label, width, height, and rotation.
// Extract a range and read the bytes (e.g. to send over HTTP)
let bytes = doc.extract_pages(0..3)?.to_bytes()?;
println!("Extracted PDF is {} bytes", bytes.len());Call save() to write to disk, or to_bytes() to get the PDF data as a Vec<u8> for streaming or further processing.
// Extract the final two pages by range and save
let n = doc.page_count();
doc.extract_pages(n.saturating_sub(2)..n)?.save("last_pages.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.