Laravel Framework | PHP & MySQL | Tutorials

David Carr

Laravel Merge PDFs

Ever needed to merge multiple PDF's together? It's a common need. In this tutorial, I'll cover how to do merge multiple pdf's together in Laravel.

First, we need a package called lara-pdf-merger Install it with composer:

composer require daltcore/lara-pdf-merger

Next, create import the namespace:

use LynX39\LaraPdfMerger\Facades\PdfMerger;

Create a new instance:

$pdfMerger = PDFMerger::init();

Add PDF to be merged:

$pdfMerger->addPDF(base_path('Modules/Quotes/pages/1.pdf'), 'all');

Do this for each PDF to be merged.

$pdfMerger->addPDF(base_path('Modules/Quotes/pages/1.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/2.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/3.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/4.pdf'), 'all');

Next, call a merge method to perform the merge.

$pdfMerger->merge();

Next, save the PDF to disk

$pdfMerger->save(public_path('quotes/001.pdf'), "file");

Putting this all together:

$pdfMerger = PDFMerger::init(); //Initialize the merger
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/1.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/2.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/3.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/4.pdf'), 'all');
$pdfMerger->merge();
$pdfMerger->save(public_path('quotes/001.pdf'), "file");

Generate a PDF and merge with existing

You may want to generate new PDF and merge that in with existing PDFs.

Let's install Laravel DOMPDF to make a new PDF. Install with composer:

composer require barryvdh/laravel-dompdf

Import PDF:

use PDF;

Create a new PDF, load a view file (quotes/pdf.blade.php) for the contents of the PDF and save to a quotes folder and use the filename quote-001.pdf

$filename = "quote-001.pdf";
$pdf = PDF::loadView('quotes.pdf', ['quote' => $data]);
$pdf->save('quotes/'.$filename);

Now, this can be merged using the same technique above.

$filename = "quote-001.pdf";
$pdf = PDF::loadView('quotes/pdf', ['quote' => $record]);
$pdf->save('quotes/'.$filename);

$pdfMerger = PDFMerger::init(); //Initialize the merger
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/1.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/2.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/3.pdf'), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/4.pdf'), 'all');
$pdfMerger->addPDF(public_path('quotes/'.$filename), 'all');
$pdfMerger->addPDF(base_path('Modules/Quotes/pages/5.pdf'), 'all');
$pdfMerger->merge();
$pdfMerger->save(public_path('quotes/quote-001.pdf'), "file");

 

Domains are often purchased from multiple providers, keeping track of where a domain is and its DNS settings can be tricky. Domain Mapper solves this by listing all your domains in one place. View your DNS settings and receive reminders to renew your domains. Try it today.

Support my work by donating with PayPal.

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2009 - 2021 DC Blog. All code MIT license. All rights reserved.