Render small preview images of every page in a PDF. Set a fixed width or height and PDFluent calculates the other dimension automatically.
use pdfluent::{PdfDocument, ImageFormat};
fn main() -> pdfluent::Result<()> {
let doc = PdfDocument::open("file.pdf")?;
for i in 0..doc.page_count() {
let png = doc.render_page(i, 72, ImageFormat::Png)?;
std::fs::write(format!("thumb-{}.png", i + 1), png)?;
}
Ok(())
}Load the document.
use pdfluent::prelude::*;
let doc = PdfDocument::open("slides.pdf")?;72 DPI is typical for thumbnails; for crisper previews, go up to 150 DPI.
let opts = ToImagesOptions::new()
.with_dpi(72)
.with_format(ImageFormat::Png);The {page} marker in the filename pattern is substituted with the 1-based page number.
let report = doc.to_images("thumb_{page}.png", opts)?;
for path in &report.paths {
println!("wrote {}", path.display());
}Limit the range with with_pages(from, to) using 1-based inclusive bounds.
let opts = ToImagesOptions::new()
.with_dpi(72)
.with_pages(1, 1);
let _ = doc.to_images("cover.png", opts)?;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.