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, ThumbnailOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = PdfDocument::open("presentation.pdf")?;
let opts = ThumbnailOptions::default().max_width(200);
for (i, page) in doc.pages().enumerate() {
let thumb = page.render_thumbnail(&opts)?;
thumb.save_png(&format!("thumb_{:03}.png", i + 1))?;
}
Ok(())
}Open the PDF. The thumbnail renderer reads only the resources needed for the visible page content at the target resolution.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("presentation.pdf")?;
println!("{} pages", doc.page_count());Use ThumbnailOptions to set max_width or max_height. The aspect ratio is preserved. You can also set both to fit inside a bounding box.
use pdfluent::ThumbnailOptions;
// Fit each page inside 200x200 px
let opts = ThumbnailOptions::default()
.max_width(200)
.max_height(200);Iterate over doc.pages() and call render_thumbnail() on each page. This is faster than render() at full DPI because the rasteriser works at a reduced resolution.
for (i, page) in doc.pages().enumerate() {
let thumb = page.render_thumbnail(&opts)?;
let path = format!("thumb_{:03}.png", i + 1);
thumb.save_png(&path)?;
println!("{}x{} -> {}", thumb.width, thumb.height, path);
}For faster batch processing, collect pages and use rayon. Each page renders independently with no shared mutable state.
use rayon::prelude::*;
let pages: Vec<_> = doc.pages().collect();
pages.par_iter().enumerate().try_for_each(|(i, page)| {
let thumb = page.render_thumbnail(&opts)?;
thumb.save_png(&format!("thumb_{:03}.png", i + 1))
})?;Use to_png_bytes() to get the encoded PNG as a Vec<u8> for serving over HTTP or storing in a database.
let thumb = doc.page(0)?.render_thumbnail(&opts)?;
let bytes: Vec<u8> = thumb.to_png_bytes()?;
println!("Thumbnail: {} bytes", bytes.len());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.