How-to guides/Document Structure

Arrange multiple PDF pages on a single sheet (N-up) in Rust

Tile 2, 4, 6, or 9 source pages onto a single output sheet, useful for printing booklets or handouts.

rust
use pdfluent::{Document, nup::{NUpLayout, NUpOptions}};

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

    // 2-up: two A4 source pages side by side on one A3 sheet
    let opts = NUpOptions::new(NUpLayout::TwoUp);
    let output = doc.nup(opts)?;

    output.save("2up.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the source document

The source pages are scaled and positioned into the output sheet. The original file is not modified.

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

Choose an N-up layout

NUpLayout provides common configurations. Each layout specifies the grid dimensions and the output sheet size.

rust
use pdfluent::nup::NUpLayout;

let layout = NUpLayout::TwoUp;    // 1x2 grid, landscape A4
// NUpLayout::FourUp   // 2x2 grid, A4
// NUpLayout::SixUp    // 2x3 grid, A4
// NUpLayout::NineUp   // 3x3 grid, A4
// NUpLayout::Custom { cols: 3, rows: 2, sheet: PageSize::A3 }
3

Configure margins and gaps

NUpOptions lets you set the gap between cells and the outer margin in points.

rust
use pdfluent::nup::NUpOptions;

let opts = NUpOptions::new(NUpLayout::FourUp)
    .gap(8.0)       // 8 pt gap between cells
    .margin(20.0);  // 20 pt outer margin
4

Build the N-up document

Call doc.nup(opts) to produce a new Document where each output page contains the specified number of source pages.

rust
let output = doc.nup(opts)?;
5

Save and verify

Write the output file. The page count of the output is ceil(source_pages / n).

rust
println!(
    "Input pages: {}, Output pages: {}",
    doc.page_count(),
    output.page_count(),
);
output.save("nup_output.pdf")?;

Notes and tips

  • Source pages are scaled proportionally to fit each cell. Aspect ratio is preserved; cells may have blank margins.
  • For booklet printing (saddle-stitch), use NUpLayout::Booklet which handles page imposition order automatically.
  • Annotations on source pages are scaled along with the content and remain functional in the output.
  • N-up output page size defaults to the same size as the source. Override with NUpOptions::sheet_size(PageSize::A3).

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