Rasterise any PDF page to a PNG image at a chosen DPI. Works headless with no display server required.
use pdfluent::{PdfDocument, RenderOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = PdfDocument::open("document.pdf")?;
let page = doc.page(0)?;
let opts = RenderOptions::default().dpi(150);
let image = page.render(&opts)?;
image.save_png("page_1.png")?;
println!("{}x{} pixels", image.width, image.height);
Ok(())
}Open the document and get the page you want to render. Pages are 0-indexed.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("slides.pdf")?;
let page = doc.page(0)?;RenderOptions::default() uses 72 DPI. For sharp screen previews, use 150 DPI. For print-quality output, use 300 DPI.
use pdfluent::RenderOptions;
let opts = RenderOptions::default()
.dpi(150)
.background_color([255, 255, 255, 255]); // white, opaqueCall page.render() with the options. The returned RenderedPage holds the raw RGBA pixel data and the pixel dimensions.
let image = page.render(&opts)?;
println!("Rendered: {}x{} px", image.width, image.height);Call save_png() with an output path. PNG encoding is built in; no external image library is required.
image.save_png("output.png")?;For further processing with the image crate or another renderer, access the raw RGBA bytes directly.
let rgba: &[u8] = image.as_rgba_bytes();
// Width * height * 4 bytes
assert_eq!(rgba.len(), image.width as usize * image.height as usize * 4);No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.