0

I'm using the latest version MPDF. This code works. I combine several files. I do not get return. If you comment out foreach, then the return comes. Maybe I'm not uniting correctly?

 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();
    }
Andrew
  • 1
  • 1
  • 1
  • See https://stackoverflow.com/a/50152965/819007 and https://stackoverflow.com/a/35043282/819007 – Finwe Sep 04 '18 at 06:41
  • I tried many more times, but I did not understand it. If you send one PDF, then the loop works and I get return. `//foreach ($pathsPDF as $item){ $allPage = $mpdf->SetSourceFile('public/scanInvoice/' . $pathsPDF[0]); for ($i = 1; $i <= $allPage; $i++) { $mpdf->AddPage(); $tplId = $mpdf->ImportPage($i); $mpdf->UseTemplate($tplId); $mpdf->WriteHTML(''); } //}` – Andrew Sep 06 '18 at 08:39
  • If I turn on the loop, I do not get return. `foreach ($pathsPDF as $item){ $allPage = $mpdf->SetSourceFile('public/scanInvoice/' . $item); for ($i = 1; $i <= $allPage; $i++) { $mpdf->AddPage(); $tplId = $mpdf->ImportPage($i); $mpdf->UseTemplate($tplId); $mpdf->WriteHTML(''); } }` – Andrew Sep 06 '18 at 08:43

1 Answers1

2

I know the post is old but this may help someone. I am grouping PDF's using PHP 7.3 and mpdf 8, saving the grouped file to disk, this is working fine for me:

$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
user1620090
  • 499
  • 6
  • 19