How-to guides/Rendering

Generate page thumbnails from a PDF in Rust

Render small preview images of every page in a PDF. Set a fixed width or height and PDFluent calculates the other dimension automatically.

rust
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(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the document

Open the PDF. The thumbnail renderer reads only the resources needed for the visible page content at the target resolution.

rust
use pdfluent::PdfDocument;

let doc = PdfDocument::open("presentation.pdf")?;
println!("{} pages", doc.page_count());
2

Configure thumbnail size

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.

rust
use pdfluent::ThumbnailOptions;

// Fit each page inside 200x200 px
let opts = ThumbnailOptions::default()
    .max_width(200)
    .max_height(200);
3

Render thumbnails for all pages

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.

rust
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);
}
4

Render thumbnails in parallel

For faster batch processing, collect pages and use rayon. Each page renders independently with no shared mutable state.

rust
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))
})?;
5

Get thumbnail bytes without saving to disk

Use to_png_bytes() to get the encoded PNG as a Vec<u8> for serving over HTTP or storing in a database.

rust
let thumb = doc.page(0)?.render_thumbnail(&opts)?;
let bytes: Vec<u8> = thumb.to_png_bytes()?;
println!("Thumbnail: {} bytes", bytes.len());

Notes and tips

  • render_thumbnail() uses a faster code path than render() for small output sizes. Do not use render() at low DPI as a substitute.
  • The thumbnail background defaults to white. Set background_color in ThumbnailOptions to change it.
  • Pages with very large embedded images may take longer to thumbnail because the image must be decoded before downsampling.

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions