Manipulation · PHP

Merge PDF files in PHP

Combine, split, reorder and rotate PDF pages from PHP. rust-pdf parses each document into an editable model, renumbers and remaps every object correctly on merge, and rebuilds a clean page tree on output.

Why PHP needs this

PHP's classic libraries (TCPDF, FPDF) predate modern PDF and have no real PDF/A, no PAdES signatures and no AES-256, while the commercial alternatives are costly.

Merging and splitting are list operations on a flat page order: append another document, extract a subset, reverse, rotate or delete pages, then save. Because the parser handles classic and cross-reference streams, object streams and encrypted input, it works on real-world files, not just ones it wrote itself.

  • Merge whole documents, extract page subsets, reorder with a permutation, rotate or delete pages.
  • Objects are renumbered and references deep-remapped, so merged files stay valid.
  • Reads classic and xref streams, object streams and RC4 / AES encrypted input.

Merge PDFs in PHP with rust-pdf

Install the package, then call the same idiomatic API every rust-pdf binding shares. The snippet below is real PHP code from the reference docs.

PHP
$a = EditableDoc::loadFile('a.pdf');
$b = EditableDoc::loadFile('b.pdf');
$a->merge($b);                         // a now has a's pages followed by b's
$a->rotatePage(0, 90);
$a->reorderPages(array_reverse(range(0, $a->pageCount() - 1)));
$a->save('merged.pdf');

$subset = $a->extractPages([0, 2]);    // pages 1 and 3
$subset->save('subset.pdf');
Validated by: qpdfmutool

This is part of the free tier in PHP. No license required.

Full PHP reference in the documentation.

Merging in PHP: FAQ

Can I merge more than two PDFs in PHP?

Yes. Load each document and call merge for each one; pages are appended in order. You can then reorder, rotate or extract any subset before saving.

Does merging keep the files valid?

Yes. On merge every object from the other document is renumbered and its references are deep-remapped, and the real page tree is rebuilt on output, so the result passes qpdf and mutool.

Is merging free?

Yes. Loading, merging, splitting, reordering and text extraction are all part of the free tier in PHP. You only need a license for the corporate features such as PDF/A, signatures and encryption.

Merge PDF Files in PHP

One Rust core, the same output across every language. Prototype for free, license the corporate features when you ship.