PHP foreach loop in TCPDF and forced download

foreachheaderphptcpdf

require_once('tcpdf/tcpdf.php');

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetFont('dejavusans', '', 10);

$pdf->AddPage();

$html = '<table><tr><td></td><td></td><td></td><td></td></tr></table>';

$pdf->writeHTML($html, true, false, true, false, '');

$pdf->lastPage();

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
$pdf->Output();

Guys I'm using tcpdf to output a PDF invoice. Problem is how do I foreach loop into the $html above that tcpdf provided?

And my forced download header doesn't seems to be working. It doesn't pops up a window asking me to save the file.

The array contains orderno, product, qty, total

Best Solution

Concatenate your content into the $html variable using the . concatenation operator:

$html = '<table>';

foreach($your_data_array as $key => $current) {
    $html .= '<tr>
        <td>' . $current['your_var'] . '</td>
        <td>' . $current['another_var'] . '</td>
    </tr>';
}

$html .= '</table>';

$pdf->writeHTML($html, true, false, true, false, '');

As for forcing download, have a look at these articles on StackOverflow:

... and the manual:

D: send to the browser and force a file download with the name given by name.

$pdf->Output($filename, 'D');