Convert first pages or any pages to JPEG/PNG thumbnails. Batch process document libraries without a display server.
use pdfluent::{PdfDocument, ImageFormat};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = PdfDocument::open("document.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(())
}Run cargo add [email protected] to get started.
Set the target width in pixels. PDFluent calculates the height to preserve the page aspect ratio. Or set both width and height explicitly and choose a fit or crop mode.
Output as JPEG, PNG, or WebP. JPEG with quality 75-85 is the typical choice for document previews. PNG is available for transparent backgrounds. WebP gives smaller files at comparable quality.
Pass page index 0 for a cover thumbnail. Pass any valid page index to generate previews for interior pages. Page indices are zero-based.
No display server, no X11, no GPU driver required. The renderer is purely software-based. It runs on Alpine Linux containers and AWS Lambda without any extra system packages.
Use Rayon to process a folder of PDFs across all CPU cores. Each document opens, renders its thumbnail, and saves independently. Throughput scales linearly with core count up to I/O limits.
The thumbnail API is part of the WASM build. Generate document previews entirely in the browser. No file upload needed. Typical thumbnail generation for a single page takes under 200 ms in Chrome on a modern laptop.