How-to guides/Document Structure

Change the page size of a PDF in Rust

Rescale or replace the MediaBox on each page to change the physical dimensions of a PDF.

rust
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(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the document

Load the PDF into a mutable Document handle.

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

Choose a target page size

PDFluent provides a PageSize enum with standard ISO and North American sizes. You can also supply a custom Rect in points.

rust
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));
3

Resize a page and scale the content

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).

rust
doc.page_mut(0)?.resize(PageSize::A4, true)?;
4

Resize all pages in a loop

Iterate over every page index and apply the same resize operation.

rust
let count = doc.page_count();
for i in 0..count {
    doc.page_mut(i)?.resize(PageSize::Letter, true)?;
}
5

Save the output

Write the resized document to disk.

rust
doc.save("resized.pdf")?;

Notes and tips

  • Scaling content proportionally may leave margins if the aspect ratio of the original and target sizes differ.
  • Resize updates the MediaBox. If a CropBox was set, it is cleared to match the new MediaBox.
  • Rotation is preserved. A landscape page stays landscape after resize unless you call page.set_rotation(0) first.
  • For high-quality output, scale at a 1:1 ratio when the source and target sizes are very close.

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