How-to guides/Rendering

Render PDF pages to JPEG images in Rust

Rasterize individual pages or an entire PDF document to JPEG files at a configurable DPI and quality level.

rust
use pdfluent::prelude::*;

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

    let report = doc.to_images(
        "page_{page}.jpg",
        ToImagesOptions::new()
            .with_dpi(150)
            .with_format(ImageFormat::Jpeg),
    )?;

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

Step by step

1

Open the PDF

Load the document.

rust
use pdfluent::prelude::*;

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

Configure JPEG output

ToImagesOptions defaults to PNG; switch to Jpeg with with_format. JPEG quality is fixed at 90 in 1.0; user-configurable quality is tracked for a later release.

rust
let opts = ToImagesOptions::new()
    .with_dpi(150)
    .with_format(ImageFormat::Jpeg);
3

Render to files

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

rust
let report = doc.to_images("page_{page}.jpg", opts)?;

Notes and tips

  • 150 DPI is sufficient for screen display and thumbnails. Use 300 DPI for print-quality rendering. 72 DPI is native PDF resolution (1 pt = 1 px at 72 DPI).
  • JPEG is lossy. For lossless archiving use ImageFormat::Png instead.
  • Pages with transparency need a background color. Without a white background, transparent areas render as black in JPEG.
  • Rendering speed scales approximately linearly with DPI squared. A 300 DPI render takes roughly 4x longer than 150 DPI for the same page.

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