Rescale or replace the MediaBox on each page to change the physical dimensions of a PDF.
use pdfluent::{Document, PageSize, Rect};
fn main() -> pdfluent::Result<()> {
let mut doc = Document::open("input.pdf")?;
let page_count = doc.page_count();
for i in 0..page_count {
doc.page_mut(i)?.resize(PageSize::A4, true)?;
}
doc.save("resized.pdf")?;
Ok(())
}Load the PDF into a mutable Document handle.
let mut doc = Document::open("input.pdf")?;PDFluent provides a PageSize enum with standard ISO and North American sizes. You can also supply a custom Rect in points.
use pdfluent::PageSize;
// Standard sizes
let a4 = PageSize::A4; // 595 x 842 pt
let letter = PageSize::Letter; // 612 x 792 pt
let a3 = PageSize::A3; // 842 x 1191 pt
// Custom size: 4 x 6 inches
let custom = PageSize::Custom(Rect::new(0.0, 0.0, 288.0, 432.0));The second parameter controls whether the content stream is scaled proportionally to fill the new MediaBox. Pass false to keep content at original scale (it may clip or leave whitespace).
doc.page_mut(0)?.resize(PageSize::A4, true)?;Iterate over every page index and apply the same resize operation.
let count = doc.page_count();
for i in 0..count {
doc.page_mut(i)?.resize(PageSize::Letter, true)?;
}Write the resized document to disk.
doc.save("resized.pdf")?;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.