How to merge base64 pdf files into one using laravel?

Member

by aubrey , in category: PHP Frameworks , 3 days ago

How to merge base64 pdf files into one using laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 3 days ago

@aubrey 

To merge base64 PDF files into one using Laravel, you can follow these steps:

  1. Decode the base64 strings of the PDF files:
1
2
$pdf1 = base64_decode($base64string1);
$pdf2 = base64_decode($base64string2);


  1. Use the PDFMerger package to merge the PDF files:
1
composer require clegginabox/pdf-merger


  1. Use the PDFMerger package to merge the PDF files:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
use PDFMergerPdfMerger;

$pdfMerger = new PdfMerger();
$pdfMerger->addPDFData($pdf1, 'P');
$pdfMerger->addPDFData($pdf2, 'P');

// Optional: You can add more PDF files if needed
// $pdfMerger->addPDFData($pdf3, 'P');

$mergedPdf = $pdfMerger->merge('browser', 'merged_pdf'); // 'browser' will display the merged PDF in the browser

// Optional: You can save the merged PDF file to the server
// $pdfMerger->save('path/to/save/merged_pdf.pdf', 'file');

$pdfMerger->close();


This code will merge the base64 PDF files provided as input and display the merged PDF file in the browser. You can also save the merged PDF file to the server by uncommenting the save method and providing the path to save the file.


Remember to replace the $base64string1 and $base64string2 variables with your actual base64 encoded strings of the PDF files.