PHP - How to use mPDF to merge PDFs

前端 未结 4 1466
小蘑菇
小蘑菇 2020-12-30 11:50

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.

4条回答
  •  太阳男子
    2020-12-30 12:29

    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');
        }
    }
    

提交回复
热议问题