【问题标题】:PHP foreach loop in TCPDF and forced downloadTCPDF中的PHP foreach循环和强制下载
【发布时间】:2014-03-22 08:08:31
【问题描述】:
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();

伙计们,我正在使用 tcpdf 输出 PDF 发票。问题是我如何 foreach 循环到 tcpdf 提供的上面的 $html 中?

而且我的强制下载标题似乎不起作用。它不会弹出一个窗口要求我保存文件。

数组包含orderno、product、qty、total

【问题讨论】:

  • tcpdf 使用 foreach() 没什么特别的,就像你在其他地方一样
  • foreach 循环会在 $html 之内还是之外呢?
  • 很可能两者都有!吓人啊

标签: php foreach header tcpdf


【解决方案1】:

使用. 连接运算符将您的内容连接到$html 变量中:

$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, '');

至于强制下载,请查看 StackOverflow 上的这些文章:

...和the manual:

D: 发送到浏览器并使用 name 指定的名称强制下载文件。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2012-07-19
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多