Install Laravel PDFMerger via Composer
masterInstall the package using Composer to add it to your Laravel project.
$ composer require webklex/laravel-pdfmergerrepository·master·Indexed 40 days ago
https://github.com/webklex/laravel-pdfmergerA tool for merging multiple PDF files into a single document within Laravel applications. It supports adding PDFs via file paths or raw strings, selecting specific pages, and duplex printing support. Output methods include saving to a file, streaming to the browser, downloading, or returning raw content via the PDFMerger facade.
Install the package using Composer to add it to your Laravel project.
$ composer require webklex/laravel-pdfmergerTo use the package, register the service provider and the PDFMerger alias in your config/app.php file.
'providers' => [
...
Webklex\PDFMerger\Providers\PDFMergerServiceProvider::class
],
'aliases' => [
...
'PDFMerger' => Webklex\PDFMerger\Facades\PDFMergerFacade::class
]Initialize the merger using the PDFMerger facade, add PDF files from paths, merge them, and save the result.
use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;
$oMerger = PDFMerger::init();
$oMerger->addPDF('/path/to/file_one.pdf', [2]);
$oMerger->addPDF('/path/to/file_two.pdf', 'all');
$oMerger->merge();
$oMerger->save('merged_result.pdf');You can add PDFs to the merger using file paths or raw string data.
Use addPDF($file, $pages) where $pages can be 'all' or an array of specific page numbers.
Use addString($string, $pages) to add PDF content directly from a string (e.g., via file_get_contents).
// Add specific pages from a file
$oMerger->addPDF($file, [1, 3]);
// Add all pages from a file
$oMerger->addPDF($file, 'all');
// Add raw content string
$oMerger->addString(file_get_contents('/path/to/file.pdf'), [1]);Use setFileName($filename) to define the filename for the merged output before saving or downloading.
$oMerger->setFileName('example.pdf');After adding files, you can process the merger using several methods:
merge(): Merges the added files.duplexMerge(): Merges files while adding blank pages to support duplex printing.save($filename): Saves the merged PDF to a specific path.stream(): Streams the merged content to the browser.download(): Triggers a download of the merged content.output(): Returns the raw content data of the merged PDF.$oMerger->merge();
$oMerger->stream();
// or
$oMerger->download();
// or
$data = $oMerger->output();Once merged, you can retrieve the resulting PDF in several ways:
output(): Returns the merged PDF content as a raw string.save($filePath): Saves the merged PDF to the specified file path. If no path is provided, it uses the name set via setFileName().stream(): Streams the PDF content to the browser (inline).download(): Sends a Laravel Response that triggers a file download in the browser.// Save to a specific path
$merger->save('/path/to/output.pdf');
// Get raw string content
$content = $merger->output();
// Download via browser
$merger->download();
// Stream to browser
$merger->stream();Execute the merge process on all files added to the queue. You can perform a standard merge or a duplex-safe merge.
Standard Merge (merge)
Combines all queued PDFs into a single document.
$orientation (string|null): The default orientation for the merged pages.Duplex Merge (duplexMerge)
Combines all queued PDFs and automatically inserts blank pages between documents where necessary to ensure that each new document starts on a new side of a sheet (ideal for duplex printing).
$orientation (string): The orientation to use (defaults to 'P').// Standard merge
$merger->merge();
// Duplex-safe merge (adds blank pages for printing)
$merger->duplexMerge('P');The package registers PDFMerger as a singleton in the Laravel service container. You can resolve the PDFMerger instance using the app() helper or by injecting it into your classes. The instance is initialized with the application's filesystem component.
// Using the app() helper
$pdfMerger = app('PDFMerger');
// Or via dependency injection in a controller/service
public function mergeFiles(Webklex\PDFMerger\PDFMerger $pdfMerger)
{
// ...
}The Webklex\PDFMerger\Facades\PDFMergerFacade provides a static interface to the PDFMerger service within Laravel. By using this facade, you can access the underlying PDFMerger instance via the PDFMerger service container binding without manually resolving the class.
use Webklex\PDFMerger\Facades\PDFMerger;
// Access the PDFMerger service via the facade
PDFMerger::add('file1.pdf');Use addPDF() to queue an existing PDF file for merging. You can specify specific pages to include and the page orientation.
Parameters:
$filePath (string): The absolute path to the PDF file.$pages (string|array): Use 'all' to include all pages, or provide an array of specific page numbers (e.g., [1, 3, 5]).$orientation (string|null): The orientation for the pages (e.g., 'P' for Portrait, 'L' for Landscape). If null, it defaults to the file's orientation.Throws:
\Exception if the file does not exist.\Exception if the $pages parameter is not 'all' and not an array.$merger->addPDF('/path/to/file.pdf', 'all', 'P');
// Or specific pages
$merger->addPDF('/path/to/file.pdf', [1, 2, 5]);Use addString() to include PDF content that is already stored in a string variable (e.g., from a database or a previous process) without saving it to a file first. The method internally creates a temporary file to process the content.
Parameters:
$string (string): The raw PDF binary content.$pages (string|array): 'all' or an array of page numbers.$orientation (string|null): The page orientation.Returns: self (the PDFMerger instance for chaining).
$merger->addString($pdfBinaryString, 'all');