PHP - How to use mPDF to merge PDFs

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

    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.

    0 讨论(0)
  • 2020-12-30 12:27

    This code is work with mpdf 8.0.7 with php 7.4.

                  Mfiles = array();
    
                 //Multiple pdf store in Mfiles array with full path.               
                 array_push($Mfiles,'/assets/'.$pdfname.'.pdf');
    
                                
                 $Mpath = 'Give file path with have you need to merge all pdf';
    
                 // after loop we start this code
                  if($Mfiles) 
                    {
                      $filesTotal = sizeof($Mfiles);
                      $fileNumber = 1;
                      
                      if (!file_exists($Mpath)) 
                      {
                          $handle = fopen($Mpath, 'w');
                          fclose($handle);
                      }
                      foreach ($Mfiles as $fileName) 
                      {
                        if (file_exists($fileName)) 
                        {
                          $pagesInFile = $pdf->SetSourceFile($fileName);
                          for ($i = 1; $i <= $pagesInFile; $i++) 
                          {
                            $tplId = $pdf->importPage($i);
                            $pdf->UseTemplate($tplId);
                            if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) 
                            {
                                $pdf->WriteHTML('<pagebreak />');
                            }
                          }
                        }
                        $fileNumber++;
                      }
                      $pdf->Output($Mpath);
                    }
    
    0 讨论(0)
  • 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('<pagebreak />');
                        }
                    }
                }
                $fileNumber++;
            }
            $mpdf->Output($outFile, 'D');
        }
    }
    
    0 讨论(0)
  • 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('<pagebreak />');
                        }
                    }
                }
                $fileNumber++;
            }
    
            $this->Output($outFile);
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题