Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sam defri's avatar

pdf merge

Hello everyone, i'm using dompdf to generate pdf files and download them into my computer.

Now i'm generating multiple pdf files and i need to combine into one pdf file and download.

how can i do this in laravel?

0 likes
11 replies
RayC's avatar

@ravp The information you're looking for is right there in the Usage section of the package @oussamamater posted.

https://github.com/Webklex/laravel-pdfmerger#usage

Here's a little code to point you in the right direction:

$pdfmerge = PDFMerger::init();
$pdfmerge->addPDF(public_path('invoices/transport/INV'.$towinvoice->id.'-'.$towunit->claim_number.'.pdf'));

// Get the uploaded images to merge with the PDF
if(isset($uploadedImages)){
	foreach($uploadedImages as $image){
		$pdfmerge->addPDF($image->getPath()); // Add each file for merging / can be pdf files
	}
}

// Merge the files
$pdfmerge->merge();
// Save the file
$pdfmerge->save(public_path('invoices/transport/merged/INV'.$towinvoice->id.'-'.$towunit->claim_number.'.pdf'), 'file');
1 like
RayC's avatar

@ravp I wrote the code. Use the dompdf to create the other PDF files and then use merger to merge them. $pdfMerge->save() method will generate the pdf file.

Read the docs on the two packages you're using, if you get stuck, post what you've come up with and someone will be able to help you.

You have to give it a try on your own and let us see some progress here.

2 likes
OussamaMater's avatar

@RayC +1 to "give it a try on your own".

I for real still can't see where the issue is, @ravp please read the full comments, they pointing towards what you need, there's no exact code to write for you, you need to do it on your own.

  1. Generate PDFs.
  2. Save them to an empty tmp directory (so you don't have to worry about the names later on).
  3. Loop this directory and everytime append the path you're getting using the finder - symfony component I mentioned above.
  4. Simply execute the merge() and save() as the docs stated
  5. Clear the tmp directory so it's ready for the next operation

And give it a try*

Resource on how to manipulate files easily:

5 likes

Please or to participate in this conversation.