PHP: mPDF / pdf library / is it possible to create multiple pdf files in a loop?

懵懂的女人 提交于 2019-12-11 04:12:17

问题


I'm using mPDF library and I'm trying to write a code which will create multiple PDF files at one call (with one opening of: test.php). Problem: always only the first file is downloaded. I know that the question may seems stupid, I've tried to dive deep into google but I can't figure out how to solve this. Maybe someone could lead me on the right path?

My logic is:
- I have one array with all of orders; for each order I would like to create PDF.
- I loop the array and each time call the MPDF library

test.php

$myorders = [ 
                0 => array (
                'FirstName' => 'Asia',
                'LastName' => 'Basia',
                'OrderNr' => '123'

                ), 

                1 => array (
                'FirstName' => 'Madzia',
                'LastName' => 'Siasia',
                'OrderNr' => '456'
                )
           ];

for ($i=0; $i < count($myorders); $i++) { 
    $FirstName = $myorders[$i]['FirstName'];
    $LastName = $myorders[$i]['LastName'];
    $OrderNr = $myorders[$i]['OrderNr'];
    require 'invoice.php';
}

invoice.php

require 'MPDF57/mpdf.php';
$mpdf=new mPDF();
$mpdf->Bookmark('Start of the document');
$mpdf->WriteHTML('test test' . $FirstName);
$mpdf->Output("filename{$OrderNr}.pdf",'D');

Only filename123.pdf is downloaded. Is it not possible to download more than one file? What am I doing wrong?


回答1:


The answer for your updated question is:

Ofcourse you can create multiple pdf files and save them onto disc. You just have to place the pdf generation code inside your for loop.

Please refer the below mentioned code:

require 'MPDF57/mpdf.php';

for ($i=0; $i < count($myorders); $i++) { 
    $FirstName = $myorders[$i]['FirstName'];
    $OrderNr = $myorders[$i]['OrderNr'];
    $mpdf=new mPDF();
    $mpdf->Bookmark('Start of the document');
    $mpdf->WriteHTML('test test' . $FirstName);
    $mpdf->Output("filename{$OrderNr}.pdf", 'F');
}



回答2:


How can you expect such feature.

You should do this using a different approach. First use the mpdf to save the files to your disc and then create a zip file and make that available for download.




回答3:


App::import('Vendor', 'mpdf', array('file' => 'mpdf' . DS . 'mpdf.php'));

$zip = new ZipArchive();

$zip->open('filename.zip', ZipArchive::CREATE);

if($zip->open('filename.zip'))

{

   for ($i=0; $i < count($myorders); $i++)

      {

         $FirstName = $myorders[$i]['FirstName'];

         $OrderNr = $myorders[$i]['OrderNr'];

         $mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);

         $mpdf->Bookmark('Start of the document');

         $mpdf->SetDisplayMode('fullpage');

         $mpdf->setHeader('hello');

         $mpdf->WriteHTML('test test' . $FirstName);

         $content = $mpdf->Output('filename'.$OrderNr.'.pdf','F');

         $zip->addFile($content,'hellofilename'.$OrderNr.'.pdf');

   }
}

else
    {
    echo "cant open!!";exit;
    }
$zip->close();

$archfilename = 'filename.zip';

header("Content-type: application/zip"); 

header("Content-Disposition: attachment; filename = $archfilename"); 

header("Pragma: no-cache"); 

header("Expires: 0"); 

readfile("filename.zip");

exit;

?>

hope this helps :) :)




回答4:


Below is the code to generate multiple PDF Files and save them in some directory.I used the sleep and time functions to generate unique filename so that it will overwrite existing file with same name.

<?php
$record = array('BMW','Mercedes','Toyota');
include_once("../lib/MPDF57/mpdf.php");
foreach($record as $value){
$filename = time().'Performance_review.pdf';
sleep(2);
$mpdf=new mPDF(); 
$mpdf->WriteHTML($value);
$get_pdf   = $mpdf->Output('../pdf-output/'.$filename,'F');
}?>


来源:https://stackoverflow.com/questions/24385292/php-mpdf-pdf-library-is-it-possible-to-create-multiple-pdf-files-in-a-loop

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