PHP - How to use mPDF to merge PDFs

蹲街弑〆低调 提交于 2019-11-30 13:57:55

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();

        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);
                    $this->UseTemplate($tplId);
                    if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) {
                        $this->WriteHTML('<pagebreak />');
                    }
                }
            }
            $fileNumber++;
        }

        $this->Output($outFile);

    }

}
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('<pagebreak />');
                    }
                }
            }
            $fileNumber++;
        }
        $mpdf->Output($outFile, 'D');
    }
}

This is an old question. I landed here after got stuck merging my PDFs as one and showing it in browser. Two answers above were tested by me and didn't work as expected. First page of the first file was shown correctly, first page of the second file jumped a blank page, and got truncted, i.e. only shown N-1 of N pages. The last page was missing. Here is a workaround I made after crawling into mpdf.php source code:

    function mergePDFFiles(Array $filenames, $outFile, $title='', $author = '', $subject = '') {
        $mpdf=new mPDF('c');
        $mpdf->SetTitle($title);
        $mpdf->SetAuthor($author);
        $mpdf->SetSubject($subject);
        if ($filenames) {
            $filesTotal = sizeof($filenames);
            $mpdf->SetImportUse();        
            for ($i = 0; $i<count($filenames);$i++) {
                $curFile = $filenames[$i];
                if (file_exists($curFile)){
                    $pageCount = $mpdf->SetSourceFile($curFile);
                    for ($p = 1; $p <= $pageCount; $p++) {
                        $tplId = $mpdf->ImportPage($p);
                        $wh = $mpdf->getTemplateSize($tplId);                
                        if (($p==1)){
                            $mpdf->state = 0;
                            $mpdf->UseTemplate ($tplId);
                        }
                        else {
                            $mpdf->state = 1;
                            $mpdf->AddPage($wh['w']>$wh['h']?'L':'P');
                            $mpdf->UseTemplate($tplId);    
                        }
                    }
                }                    
            }                
        }
        $mpdf->Output();
        unset($mpdf);
    }

$outFile is not used, because the resulting PDF is intended to be force to the browser.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!