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.

Laravel Merge PDFs
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);
$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");

 

Kindly Consider SUBSCRIBING to OUR CHANNEL and help US grow with you. For Business Purpose kindly email us at geeksourcecodes@gmail.com Or Whatapp us at +254714643906

LOGIN/REGISTER TO DOWNLOAD THE FREE SOURCECODES BELOWyes

For more information about the system. You can contact me @

Email –geeksourcecodes@gmail.com
Mobile No. – +254714643906
Or feel free to comment below.

SUBSCRIBE TO OUR YOUTUBE CHANNEL BELOW TO WATCH MORE VIDEOS

Note: Source Code is only available for educational purpose, plz don’t use it for commercial purpose without the permission of the original author.