Combine multiple PDF files into one document. PDFluent preserves bookmarks, page labels, and named destinations across all input files.
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(())
}Add the pdfluent crate to your Cargo.toml. No system libraries are required.
[dependencies]
pdfluent = "0.9"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.
use pdfluent::PdfMerger;
let merger = PdfMerger::new()
.add_file("invoice_jan.pdf")?
.add_file("invoice_feb.pdf")?
.add_file("invoice_mar.pdf")?;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.
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")?;Call merge() with the output path. The method returns a MergeResult containing the total page count and a list of source page ranges.
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);
}If your PDFs come from a network request or database, pass byte slices instead of file paths.
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)?;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.