How-to guides/Merge & Split

Merge PDFs in Rust

Combine multiple PDF files into one document. PDFluent preserves bookmarks, page labels, and named destinations across all input files.

rust
use pdfluent::PdfMerger;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let output = PdfMerger::new()
        .add_file("part1.pdf")?
        .add_file("part2.pdf")?
        .add_file("part3.pdf")?
        .merge("combined.pdf")?;

    println!("Merged {} pages into combined.pdf", output.page_count);
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to your project

Add the pdfluent crate to your Cargo.toml. No system libraries are required.

rust
[dependencies]
pdfluent = "0.9"
2

Build a PdfMerger and add input files

Create a PdfMerger instance and chain add_file calls for each PDF you want to include. Files are appended in the order they are added.

rust
use pdfluent::PdfMerger;

let merger = PdfMerger::new()
    .add_file("invoice_jan.pdf")?
    .add_file("invoice_feb.pdf")?
    .add_file("invoice_mar.pdf")?;
3

Configure bookmark and outline behaviour

By default PDFluent wraps each input file in a top-level bookmark named after the filename. You can rename these or disable the wrapping entirely.

rust
use pdfluent::{PdfMerger, MergeOptions};

let options = MergeOptions::default()
    .wrap_bookmarks(true)
    .bookmark_label_from_filename(true);

let merger = PdfMerger::with_options(options)
    .add_file("report_q1.pdf")?
    .add_file("report_q2.pdf")?;
4

Write the merged output

Call merge() with the output path. The method returns a MergeResult containing the total page count and a list of source page ranges.

rust
let result = merger.merge("annual_report.pdf")?;

println!("Total pages: {}", result.page_count);
for source in &result.sources {
    println!("{}: pages {}-{}", source.path, source.start_page, source.end_page);
}
5

Merge from in-memory buffers

If your PDFs come from a network request or database, pass byte slices instead of file paths.

rust
use pdfluent::PdfMerger;

let bytes_a: Vec<u8> = fetch_pdf_a(); // your source
let bytes_b: Vec<u8> = fetch_pdf_b();

let result = PdfMerger::new()
    .add_bytes(&bytes_a)?
    .add_bytes(&bytes_b)?
    .merge_to_bytes()?;

std::fs::write("merged.pdf", &result.data)?;

Notes and tips

  • Named destinations from each input file are remapped so they remain valid in the merged document.
  • Page labels (Roman numerals, custom prefixes) are preserved per source file.
  • Encrypted PDFs must be decrypted before merging. Use PdfDocument::decrypt() first.
  • The merger processes files in order. Page numbering in the output starts at 1 and increases sequentially.

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