PHP - How to use mPDF to merge PDFs

前端 未结 4 1465
小蘑菇
小蘑菇 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: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('');
                            }
                          }
                        }
                        $fileNumber++;
                      }
                      $pdf->Output($Mpath);
                    }
    

提交回复
热议问题