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.
Last updated: 2026-06-29
Why PHP needs this
Combining a cover letter with a contract, splitting a scanned batch into per-customer files, dropping a blank page or rotating a sideways scan. These page operations are bread-and-butter back-office work, yet awkward to do well in PHP without reaching for a command-line tool.
rust-pdf parses each document into a flat, editable page list, so merge, extractPages, reorderPages, rotatePage and delete are simple list operations. On merge it renumbers every object from the other file and deep-remaps its references, then rebuilds a clean page tree at save, so the output passes qpdf and mutool.
Loaded through Composer autoload and bridged by ext-ffi, the whole thing runs inside the PHP-FPM request or a worker. No pdftk or qpdf executable to provision, and no temp-file shuffling. Because the parser reads classic and cross-reference streams, object streams and encrypted input, it copes with real uploads, not just files 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
Add the package with Composer, then call the same idiomatic API every binding shares. The page-manipulation snippet below is real PHP from the reference docs.
composer require rust-pdf/rustpdf
$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');
Page manipulation is part of the free PHP tier — no license token required.
The complete manipulation API is in the PHP 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.
Can I merge uploaded PDFs inside a Laravel request without pdftk?
Yes. EditableDoc::loadFile reads an uploaded file straight from storage, merge, extractPages and reorderPages run in-process, and save returns the bytes. There is no pdftk or qpdf binary to install on the server, and no temp-file shell-out from your controller. It all stays inside the PHP process.
Wire PDF assembly into your PHP back office
One Rust core, the same page operations in every language. Merging, splitting and reordering stay free — you only license the corporate features such as signing and encryption.