I will start out by saying that I can generate PDFs just fine with mPDF, but for the life of me, I can\'t get it to merge an existing PDF with the PDF it just generated.
function mergePDFFiles(Array $filenames, $outFile) {
$mpdf = new mPDF();
if ($filenames) {
// print_r($filenames); die;
$filesTotal = sizeof($filenames);
$fileNumber = 1;
$mpdf->SetImportUse();
if (!file_exists($outFile)) {
$handle = fopen($outFile, 'w');
fclose($handle);
}
foreach ($filenames as $fileName) {
if (file_exists($fileName)) {
$pagesInFile = $mpdf->SetSourceFile($fileName);
//print_r($fileName); die;
for ($i = 1; $i <= $pagesInFile; $i++) {
$tplId = $mpdf->ImportPage($i);
$mpdf->UseTemplate($tplId);
if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) {
$mpdf->WriteHTML(' ');
}
}
}
$fileNumber++;
}
$mpdf->Output($outFile, 'D');
}
}