Combine PDF files in PHP and Laravel with optional page numbering.
composer require mamura/pdf-combineuse Mamura\PdfCombine\DTO\CombinePdfData;
use Mamura\PdfCombine\Services\CombineDocuments;
use Mamura\PdfCombine\Services\Drivers\FpdiPdfCombiner;
$service = new CombineDocuments(new FpdiPdfCombiner());
$output = $service->handle(
new CombinePdfData(
files: [
'/path/one.pdf',
'/path/two.pdf',
],
outputPath: '/path/merged.pdf',
addPageNumbers: true
)
);
echo $output;This package supports Laravel auto-discovery.
use Mamura\PdfCombine\DTO\CombinePdfData;
use Mamura\PdfCombine\Services\CombineDocuments;
$path = app(CombineDocuments::class)->handle(
new CombinePdfData(
files: [
storage_path('app/a.pdf'),
storage_path('app/b.pdf'),
],
addPageNumbers: true
)
);
return response()->json([
'file' => $path,
]);| Field | Type | Description |
|---|---|---|
| files | array | List of PDF file paths |
| outputPath | ?string | Output file path (optional) |
| addPageNumbers | bool | Add page numbers to the final PDF |
filesmust contain valid paths to readable PDF files.- If
outputPathis not provided, a temporary file will be generated automatically. - Page numbers are rendered at the bottom center of each page when enabled.
The package may throw:
Mamura\PdfCombine\Exceptions\FileNotFoundExceptionMamura\PdfCombine\Exceptions\InvalidPdfExceptionMamura\PdfCombine\Exceptions\PdfCombineException
Example:
try {
$service->handle($data);
} catch (\Mamura\PdfCombine\Exceptions\PdfCombineException $e) {
// handle error
}Run tests with:
vendor/bin/phpunit- Supports only local PDF files
- Uses FPDI as the current driver
- Does not support remote files or cloud storage (S3, etc.)
- Does not perform OCR or PDF validation beyond basic checks
- Configurable page number position
- Custom font support
- Multiple drivers (external APIs, fallback strategies)
- Stream output support (without saving to disk)
MIT