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::{Document, render::{RenderOptions, ImageFormat}};

fn main() -> pdfluent::Result<()> {
    let doc = Document::open("input.pdf")?;

    let opts = RenderOptions::new()
        .dpi(150)
        .format(ImageFormat::Jpeg { quality: 85 });

    for (i, page) in doc.pages().enumerate() {
        let image = page.render(&opts)?;
        image.save(format!("page_{:03}.jpg", i + 1))?;
    }

    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the PDF

A read-only Document is sufficient for rendering.

rust
let doc = Document::open("input.pdf")?;
2

Configure render options

Set the DPI for rasterization and the output format. Higher DPI produces sharper images but larger files.

rust
use pdfluent::render::{RenderOptions, ImageFormat};

let opts = RenderOptions::new()
    .dpi(150)                                   // 150 DPI = medium resolution
    .format(ImageFormat::Jpeg { quality: 85 })  // 85% JPEG quality
    .background_color([255, 255, 255]);          // white background
3

Render a single page

page.render() returns a RenderedImage containing the pixel data and dimensions.

rust
let page = doc.page(0)?;
let image = page.render(&opts)?;
println!("Width:  {}px", image.width());
println!("Height: {}px", image.height());
4

Save the rendered image to disk

image.save() writes the encoded JPEG bytes to the specified path.

rust
image.save("page_1.jpg")?;
5

Render all pages to a directory

Iterate over all pages and write one JPEG per page.

rust
std::fs::create_dir_all("output_images")?;
for (i, page) in doc.pages().enumerate() {
    let image = page.render(&opts)?;
    image.save(format!("output_images/page_{:03}.jpg", i + 1))?;
}

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 pixel-perfect 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