The PDF Stitcher library allows you to easily stitch together multiple PDFs into a single file.
To install the PDF Stitcher package, run the following Composer command.
composer require langleyfoxall/pdf-stitcher
Please note that this package requires Ghostscript (gs
) to be installed
on your server. If you are running Ubuntu, this can be installed with the
following command.
sudo apt install ghostscript
See the following basic usage example.
(new PdfStitcher)
->addPdf('firstDocument.pdf')
->addPdfs(['secondDocument.pdf', 'yetAnotherDocument.pdf'])
->save('destinationDocument.pdf');
This will take in three input PDFs, stitch them together, and save out
the result to destinationDocument.pdf
. The documents will be stitched
together in the order they are added.
A path to a Ghostscript executable can be passed into the PdfStitcher
constructor:
new PdfStitcher('a/path/to/a/gs/executable')
Additional Ghostscript arguments can be passed into the PdfStitcher
constructor:
new PdfStitcher(null, '-dNEWPDF=false')
These will NOT be escaped in any way.
You may optionally give an array of page indices when adding a PDF:
(new PdfStitcher)
->addPdf('firstDocument.pdf', [0, 2, 3, 5])
->save('destinationDocument.pdf');
Only the page numbers you list will be included.
The following will be rejected:
- Non-integer page numbers.
- Page numbers less than zero.
- Duplicate page numbers (e.g. 1, 3, 3, 5, 7).
- Misordered page numbers (e.g. 1, 5, 3, 7).