I am using MPDF library to generate pdf files .I have created two PDF files in my root directory as follows :
$invoice_nos = ['0'=>'ISE-00000014Y18','1'=>'ISE-00000005Y18'];
foreach ($invoice_nos as $key => $invoice_no) {
$html = 'Invoice No - '.$invoice_no;
$pdf_file_name = $invoice_no.'.pdf';
$pdf_file_path = ROOT . '/app/webroot/Service_Invoices/'. DS .$pdf_file_name ;
ob_start();
$mpdf = new \mPDF('utf-8', 'A4' ,'','',5,5,36,10,5,4);
$mpdf->WriteHTML($html,2);
ob_clean();
$mpdf->Output($pdf_file_name,'f');
}
Now I want to merge these two files into a single file with different pages. How can I do this? I have searched many examples of it but nothing is working.
Hi There so i actually used this code to Flatten a PDF that had editable forms but i believe we can change it to merge the pdf's together.
This solution uses php's Imagick() which should be part of your hosting environment.
So here is the code, i have tried to comment it as best possible. you will call the mergePdf() and put the destination folder (where your files are and where you will save the new file) and an array of the files (Just there names) to be merged, and then a new file name. once done it will save the new file in the destination folder.
mPDF is not the best tool to merge PDF files. You'll be better off with GhostScript:
Alternatively, generate both invoices directly to one file: