PHP - How to use mPDF to merge PDFs

前端 未结 4 1464
小蘑菇
小蘑菇 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:30

    I Merge pdfs like this - {all pages in all files}:

    $this = Class that extends mPDF class...

    function mergePDFFiles(Array $filenames, $outFile)
    {
        if ($filenames) {
    
            $filesTotal = sizeof($filenames);
            $fileNumber = 1;
    
            $this->SetImportUse(); // this line comment out if method doesnt exist
    
            if (!file_exists($outFile)) {
                $handle = fopen($outFile, 'w');
                fclose($handle);
            }
    
            foreach ($filenames as $fileName) {
                if (file_exists($fileName)) {
                    $pagesInFile = $this->SetSourceFile($fileName);
                    for ($i = 1; $i <= $pagesInFile; $i++) {
                        $tplId = $this->ImportPage($i); // in mPdf v8 should be 'importPage($i)'
                        $this->UseTemplate($tplId);
                        if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) {
                            $this->WriteHTML('');
                        }
                    }
                }
                $fileNumber++;
            }
    
            $this->Output($outFile);
    
        }
    
    }
    

提交回复
热议问题