1

Hi everyone,

I've searched around and can't find an answer to this but I'm sure it's a common problem.

I want to loop round some data and create multiple pdf files. My code (below) runs but I only get one PDF file created.

I have verified that my loop works (by commenting out the do_pdf() function and echoing $filename and $studentname)

Any help and suggestions gratefully received.


Here's a simplified/basic version of my code:


require_once(dirname(__FILE__).'/PDF/html2pdf.class.php');
$invoicedata = $_SESSION['invoicedata'];

foreach ($invoicedata as $id => $invoice)
{
$studentname = $invoice['studentname'];
$template = file_get_contents('invoices/invoice-template.html');
$pdfcontent = str_replace('___NAME___', $studentname, $template);
$filename= 'MyInvoice' . $studentname . '.pdf';
do_pdf($pdfcontent, $filename);
}


function do_pdf($content, $filename)
{
ob_clean();
$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->WriteHTML($content);
$html2pdf->Output($filename, 'D');
}

2

So I carried on working on this but still couldn't get it to work. But incase anyone else comes here looking for a solution I'm using a workaround.

Changing $html2pdf->Output($filename, 'D') to $html2pdf->Output($filename, 'F') works as expected and creates multiple pdf files on the server. So then I just zip them up and download as a single file. In retrospect this is probably a better solution for my users.