Library for merging multiple PDFs

4.0.5 2020-06-15 20:22 UTC

This package is auto-updated.

Last update: 2021-05-15 22:23:39 UTC


Packagist Version Build Status Quality Score

PHP library for merging multiple PDFs.


composer require kadudutra/libmergepdf


Append the first ten pages of bar.pdf to foo.pdf:

use kadudutra\libmergepdf\Merger;
use kadudutra\libmergepdf\Pages;

$merger = new Merger;
$merger->addFile('bar.pdf', new Pages('1-10'));
$createdPdf = $merger->merge();

Bulk add files from an iterator:

use kadudutra\libmergepdf\Merger;

$merger = new Merger;
$merger->addIterator(['A.pdf', 'B.pdf']);
$createdPdf = $merger->merge();

Merging pdfs of version 1.5 and later

The default FPDI driver is not able handle compressed pdfs of version 1.5 or later. Circumvent this limitation by using the slightly more experimental TCPDI driver.

use kadudutra\libmergepdf\Merger;
use kadudutra\libmergepdf\Driver\TcpdiDriver;

$merger = new Merger(new TcpdiDriver);

Using an immutable merger

Immutability may be achieved by using a driver directly.

use kadudutra\libmergepdf\Driver\Fpdi2Driver;
use kadudutra\libmergepdf\Source\FileSource;
use kadudutra\libmergepdf\Pages;

$merger = new Fpdi2Driver;

$createdPdf = $merger->merge(
    new FileSource('foo.pdf'),
    new FileSource('bar.pdf', new Pages('1-10'))

Known issues

  • Links and other content outside a page content stream is removed at merge. This is due to limitations in FPDI and not possible to resolve with the current strategy. For more information see FPDI.