### Install Laravel PDFMerger via Composer Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This snippet shows the Composer command to install the webklex/laravel-pdfmerger package. It requires Composer to be installed and accessible in your terminal. ```bash $ composer require webklex/laravel-pdfmerger ``` -------------------------------- ### Install Laravel PDFMerger via Composer Source: https://context7.com/webklex/laravel-pdfmerger/llms.txt Installs the Laravel PDFMerger package using Composer. This is the initial step to integrate the library into your Laravel project. ```bash composer require webklex/laravel-pdfmerger ``` -------------------------------- ### Downloading Merged PDF Content in Laravel Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP example demonstrates how to trigger a download of the merged PDF content directly to the user's browser using the `download` method. This is a common way to provide merged files to users. ```php $oMerger->download(); ``` -------------------------------- ### Basic PDF Merging with Laravel PDFMerger Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP example illustrates the fundamental usage of the PDFMerger facade to merge multiple PDF files. It shows how to initialize the merger, add PDF files with specific page selections, perform the merge, and save the output. ```php use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger; $oMerger = PDFMerger::init(); $oMerger->addPDF('/path/to/project/vendors/webklex/laravel-pdfmerger/src/PDFMerger/examples/pdf_one.pdf', [2]); $oMerger->addPDF('/path/to/project/vendors/webklex/laravel-pdfmerger/src/PDFMerger/examples/pdf_two.pdf', 'all'); $oMerger->merge(); $oMerger->save('merged_result.pdf'); ``` -------------------------------- ### Selecting Pages for PDF Merging in Laravel Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP example showcases various methods for selecting specific pages from a PDF file to be merged. It covers adding all pages, a single page by its number, or a custom array of page numbers. ```php $oMerger->addPDF($file, 'all'); //Add all pages $oMerger->addPDF($file, [1]); //Add page one only $oMerger->addPDF($file, [2]); //Add page two only $oMerger->addPDF($file, [1, 3]); //Add page one and three only ``` -------------------------------- ### Configure PDFMerger Service Provider and Alias in Laravel Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP code demonstrates how to register the PDFMergerServiceProvider and set up the PDFMerger facade in your Laravel application's config/app.php file. This is a necessary step after installation to enable the package's functionality. ```php 'providers' => [ ... Webklex\PDFMerger\Providers\PDFMergerServiceProvider::class ], 'aliases' => [ ... 'PDFMerger' => Webklex\PDFMerger\Facades\PDFMergerFacade::class ] ``` -------------------------------- ### Setting Filename for Merged PDF in Laravel Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP example demonstrates how to set a specific filename for the merged PDF document using the `setFileName` method. This filename will be used when saving, streaming, or downloading the file. ```php $oMerger->setFileName('example.pdf'); ``` -------------------------------- ### Getting Raw Merged PDF Output in Laravel Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP code snippet shows how to retrieve the raw binary content of the merged PDF document using the `output` method. This raw data can then be processed further as needed. ```php echo $oMerger->output(); ``` -------------------------------- ### Initialize PDFMerger Instance Source: https://context7.com/webklex/laravel-pdfmerger/llms.txt Creates a new instance of the PDFMerger class, preparing it for merge operations. The init() method ensures a clean state by creating a fresh FPDI instance and clearing any previous files, preventing resource conflicts. ```php use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger; $merger = PDFMerger::init(); // The init() method creates a fresh FPDI instance and clears any previous files // This prevents resource conflicts when performing multiple merge operations ``` -------------------------------- ### Run Tests for Laravel PDFMerger Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This command executes the test suite for the Laravel PDFMerger package using Composer. It's essential for verifying the package's integrity and ensuring new changes haven't introduced regressions. ```bash $ composer test ``` -------------------------------- ### Add PDF Files with Page Selection and Merge Source: https://context7.com/webklex/laravel-pdfmerger/llms.txt Demonstrates adding PDF files to the merge queue, specifying which pages to include (all, specific arrays, or ranges) and setting custom orientation. It also shows how to perform the merge operation and save the result to a file. ```php use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger; $merger = PDFMerger::init(); // Add all pages from a PDF $merger->addPDF('/path/to/document.pdf', 'all'); // Add only page 1 $merger->addPDF('/path/to/cover.pdf', [1]); // Add specific pages (page 1 and page 3) $merger->addPDF('/path/to/report.pdf', [1, 3]); // Add multiple pages with custom orientation $merger->addPDF('/path/to/landscape.pdf', [2, 4, 5], 'L'); // Merge and save to file system $merger->merge(); $merger->save('/path/to/output/merged_document.pdf'); // Expected: Single PDF file containing all specified pages in order ``` -------------------------------- ### Add PDF from Raw String Data and Merge Source: https://context7.com/webklex/laravel-pdfmerger/llms.txt Shows how to add PDF content directly from raw binary string data, useful when PDFs are fetched from external sources like URLs or databases. It merges this content with other PDFs and outputs the result as a binary string. ```php use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger; $merger = PDFMerger::init(); // Load PDF content from external source $pdfContent = file_get_contents('https://example.com/remote.pdf'); // Add the raw content with specific pages $merger->addString($pdfContent, [1, 2]); // Add another PDF from file system $merger->addPDF('/path/to/local.pdf', 'all'); // Merge and output $merger->merge(); $output = $merger->output(); // Expected: Binary string containing merged PDF data // Note: Temporary files created by addString() are automatically cleaned up on destruct ``` -------------------------------- ### Adding Raw PDF Content with Laravel PDFMerger Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP snippet demonstrates how to add raw PDF content, obtained from a file using `file_get_contents`, directly to the merger. It also shows how to specify which pages from this raw content should be included. ```php $oMerger->addString(file_get_contents('/path/to/project/vendors/webklex/laravel-pdfmerger/src/PDFMerger/examples/pdf_two.pdf'), [1]); ``` -------------------------------- ### Complex PDF Merge Workflow with Error Handling (PHP) Source: https://context7.com/webklex/laravel-pdfmerger/llms.txt Demonstrates a comprehensive workflow for merging multiple PDF documents using Webklex PDFMerger in a Laravel service class. It includes adding a cover page, dynamically adding client-specific documents from a database, appending a terms and conditions page, setting a descriptive filename, performing the merge, saving the output locally, and backing it up to S3. Robust error handling with logging is also implemented. ```php use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; class DocumentService { public function mergeClientDocuments($clientId) { try { $merger = PDFMerger::init(); // Add cover page $merger->addPDF(storage_path('templates/cover.pdf'), [1]); // Add client-specific documents $documents = DB::table('client_documents') ->where('client_id', $clientId) ->orderBy('sort_order') ->get(); foreach ($documents as $doc) { $filePath = storage_path('app/' . $doc->file_path); // Validate file exists before adding if (file_exists($filePath)) { $merger->addPDF($filePath, 'all'); } else { \Log::warning("Missing document: {$filePath}"); } } // Add terms and conditions as last page $merger->addPDF(storage_path('templates/terms.pdf'), [1]); // Set descriptive filename $filename = sprintf('client_%d_documents_%s.pdf', $clientId, date('Y-m-d') ); $merger->setFileName($filename); // Perform merge $merger->merge(); // Save to permanent storage $outputPath = storage_path("app/merged/{$clientId}/{$filename}"); $merger->save($outputPath); // Also upload to cloud backup $binary = $merger->output(); Storage::disk('s3')->put("client-documents/{$filename}", $binary); return [ 'success' => true, 'path' => $outputPath, 'filename' => $filename, ]; } catch (\Exception $e) { \Log::error('PDF merge failed: ' . $e->getMessage()); return [ 'success' => false, 'error' => $e->getMessage(), ]; } } } // Expected: Merged PDF with cover, all client documents, and terms page // Saved locally and backed up to S3 with proper error handling ``` -------------------------------- ### Download Merged PDF with Headers (PHP) Source: https://context7.com/webklex/laravel-pdfmerger/llms.txt Triggers a browser download of a merged PDF file. It requires the WebklexPDFMergerFacadesPDFMergerFacade and sets appropriate HTTP headers for file download. The output filename can be customized. ```php use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger; // In a Laravel route or controller Route::get('/download-report/{year}', function($year) { $merger = PDFMerger::init(); // Collect annual reports $reports = [ storage_path("reports/{$year}/q1.pdf"), storage_path("reports/{$year}/q2.pdf"), storage_path("reports/{$year}/q3.pdf"), storage_path("reports/{$year}/q4.pdf"), ]; foreach ($reports as $report) { if (file_exists($report)) { $merger->addPDF($report, 'all'); } } $merger->setFileName("annual_report_{$year}.pdf"); $merger->merge(); // Trigger download with proper Content-Disposition header return $merger->download(); }); // Expected: Browser download dialog with filename "annual_report_2024.pdf" ``` -------------------------------- ### Streaming Merged PDF Content in Laravel Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP snippet shows how to stream the merged PDF content directly to the browser using the `stream` method. This is often used for immediate display or download without saving to a file first. ```php $oMerger->stream(); ``` -------------------------------- ### Merge PDFs and Save to File System Source: https://context7.com/webklex/laravel-pdfmerger/llms.txt Merges multiple specified PDF files and saves the combined document to a designated location on the file system. It supports saving with an explicit path or using a filename previously set with setFileName(). ```php use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger; $merger = PDFMerger::init(); $merger->addPDF('/var/www/documents/invoice_2024_01.pdf', 'all'); $merger->addPDF('/var/www/documents/invoice_2024_02.pdf', 'all'); $merger->addPDF('/var/www/documents/invoice_2024_03.pdf', 'all'); // Perform merge operation $merger->merge(); // Save with explicit path $success = $merger->save('/var/www/output/invoices_q1_2024.pdf'); // Alternatively, set filename first then save $merger->setFileName('quarterly_report.pdf'); $merger->save(); // Uses the preset filename // Expected: Boolean true on success, merged PDF written to specified location ``` -------------------------------- ### Duplex Merging with Laravel PDFMerger Source: https://github.com/webklex/laravel-pdfmerger/blob/master/README.md This PHP code utilizes the `duplexMerge` method to merge PDF files while adding blank pages to support duplex printing. This is useful for ensuring correct page order when printing double-sided. ```php $oMerger->duplexMerge(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.