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::prelude::*;

fn main() -> Result<()> {
    let doc = PdfDocument::open("slides.pdf")?;

    // Render all pages as PNG thumbnails at 72 DPI.
    let report = doc.to_images(
        "thumb_{page}.png",
        ToImagesOptions::new()
            .with_dpi(72)
            .with_format(ImageFormat::Png),
    )?;

    println!("wrote {} thumbnails", report.paths.len());
    Ok(())
}
Install:cargo add pdfluent@1.0.0-beta.5Download SDK →

Step by step

1

Open the source PDF

Load the document.

rust
use pdfluent::prelude::*;

let doc = PdfDocument::open("slides.pdf")?;
2

Build ToImagesOptions

72 DPI is typical for thumbnails; for crisper previews, go up to 150 DPI.

rust
let opts = ToImagesOptions::new()
    .with_dpi(72)
    .with_format(ImageFormat::Png);
3

Render all pages

The {page} marker in the filename pattern is substituted with the 1-based page number.

rust
let report = doc.to_images("thumb_{page}.png", opts)?;
for path in &report.paths {
    println!("wrote {}", path.display());
}
4

Render only the first page

Limit the range with with_pages(from, to) using 1-based inclusive bounds.

rust
let opts = ToImagesOptions::new()
    .with_dpi(72)
    .with_pages(1, 1);
let _ = doc.to_images("cover.png", opts)?;

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 (not available in 1.0) 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