【问题标题】:to generate pdf download using tcpdf使用 tcpdf 生成 pdf 下载
【发布时间】:2013-08-14 05:20:22
【问题描述】:

我无法生成pdf下载,我的代码如下,谁能告诉我这段代码有什么问题。

include 'tcpdf.php';
$pdf = new TCPDF();
$pdf->AddPage('P', 'A4');
$html = '<html>
<head></head>
<body><table border="1">
<tr><th>name</th>
<th>company</th></tr>
<tr>
<td>hello</td>
<td>xx technologies</td>
</tr>
</table>
</body>
</html>';

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

$pdf->Output();
?>

【问题讨论】:

  • 你有PHP报告的错误吗?

标签: php tcpdf


【解决方案1】:

我已经修改了您的代码并且它可以工作。 [测试]

<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();
$html = '<html>
<head></head>
<body><table border="1">
<tr><th>name</th>
<th>company</th></tr>
<tr>
<td>hello</td>
<td>xx technologies</td>
</tr>
</table>
</body>
</html>';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output('htmlout.pdf', 'I');
?>

输出:

【讨论】:

  • 嗨 shankar,仍然没有得到输出
  • 嗨,Namratha,它就像魅力一样。你能描述一下错误吗?您的 tcpdf 库是否被正确引用?
  • 我正在尝试这样做,但它显示 TCPDF 类未找到,但我已添加所有文件。
【解决方案2】:

如果要下载文件,请在 $pdf-&gt;Output(); 之前使用 PHP 函数 header,如下所示:

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
$pdf->Output(); # terminate your file with TCPDF output

在 php.net 上查看 PHP function header

【讨论】:

  • 一点也不,这个答案不正确,tcpdf 类本身提供方法,只需将此参数传递给输出方法: $pdf->Output('yourfilename.pdf', 'D');就是这样
【解决方案3】:

添加这一行 $pdf-&gt;Output($downlaodname, 'D'); 而不是 $pdf-&gt;Output();。它将强制浏览器下载文件。

【讨论】:

  • 此答案适用于 Chrome 和我的环境
猜你喜欢
  • 2016-09-29
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
相关资源
最近更新 更多