【问题标题】:mPDF merge multiple files pdfmPDF 合并多个文件 pdf
【发布时间】:2019-02-08 06:24:55
【问题描述】:

我使用的是最新版本的 MPDF。 此代码有效。我合并了几个文件。 我没有得到回报。 如果您注释掉 foreach,则返回。 也许我没有正确团结?

 list($directorySite, $shell) = explode('app', __DIR__);
    require($directorySite.'/vendor/autoload.php');
    try {
        $mpdf = new Mpdf(['mode' => 'utf-8']);
        $mpdf->SetImportUse();
        $page1 = $mpdf->SetSourceFile('public/scanInvoice/'.$resultJPGtoPDF);
        for ($i=1;$i<=$page1;$i++) {
            $mpdf->AddPage();
            $tplId = $mpdf->ImportPage($i);
            $mpdf->UseTemplate($tplId);
            $mpdf->WriteHTML('');
        }
        foreach ($pathsPDF as $item){
            $page2 = $mpdf->SetSourceFile('public/scanInvoice/'.$item);
            for ($i=1;$i<=$page2;$i++) {
                $mpdf->AddPage();
                $tplId = $mpdf->ImportPage($i);
                $mpdf->UseTemplate($tplId);
                $mpdf->WriteHTML('');
            }
        }
        $preName = $this->translit('JPEGandPDF');
        $mpdf->Output($direct.DIRECTORY_SEPARATOR.$preName.'.pdf', 'F');
        return $preName.'.pdf';
    } catch (MpdfException $e) {
        return $e->getMessage();
    }

【问题讨论】:

  • 我又试了很多次,还是没看懂。如果您发送一份 PDF,则循环有效,我得到回报。 //foreach ($pathsPDF as $item){ $allPage = $mpdf-&gt;SetSourceFile('public/scanInvoice/' . $pathsPDF[0]); for ($i = 1; $i &lt;= $allPage; $i++) { $mpdf-&gt;AddPage(); $tplId = $mpdf-&gt;ImportPage($i); $mpdf-&gt;UseTemplate($tplId); $mpdf-&gt;WriteHTML(''); } //}
  • 如果我打开循环,我不会得到回报。 foreach ($pathsPDF as $item){ $allPage = $mpdf-&gt;SetSourceFile('public/scanInvoice/' . $item); for ($i = 1; $i &lt;= $allPage; $i++) { $mpdf-&gt;AddPage(); $tplId = $mpdf-&gt;ImportPage($i); $mpdf-&gt;UseTemplate($tplId); $mpdf-&gt;WriteHTML(''); } }

标签: php mpdf


【解决方案1】:

我知道这篇文章很旧,但这可能会对某人有所帮助。 我正在使用 PHP 7.3 和 mpdf 8 对 PDF 进行分组,将分组的文件保存到磁盘,这对我来说很好:

$files = array();//the files to merge
$files[] = DIR_UPLOAD. 'gls/batch_11/labels_45812.pdf';
$files[] = DIR_UPLOAD. 'gls/batch_11/labels_45818.pdf';
$files[] = DIR_UPLOAD. 'gls/batch_11/labels_45820.pdf';

$output= DIR_UPLOAD. 'gls/batch_11/labels.pdf';//where to save the combined file


require_once(DIR_SYSTEM.'library/mpdf/vendor/autoload.php');
$mpdf=new \Mpdf\Mpdf(['tempDir' => DIR_SYSTEM.'library/mpdf/tmp', 'format'=> 'a6', 'orientation'=> 'L']);//A6 format landscape

    $file_counter=1;//to be used in the loop
    foreach($files AS $file)
    {
          $pagecount = $mpdf->SetSourceFile($file);
          for($i=0; $i< $pagecount; $i++)
          {
              $tplId = $mpdf->importPage($i+1);
              $mpdf->useTemplate($tplId);

                  //add a page except for the last loop of the last document (otherwise we have a blank page)
                  if( $file_counter != count($files) ||   ($i+1) != $pagecount)
                  {
                    $mpdf ->addPage();
                  }

          }//end for
      $file_counter++;
    }//end foreach

    $mpdf->Output($output);//save the file

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 2013-06-10
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    相关资源
    最近更新 更多