Php 使用tcpdf生成pdf下载

Php 使用tcpdf生成pdf下载,php,tcpdf,Php,Tcpdf,我无法生成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

我无法生成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();
?>
包括“tcpdf.php”;
$pdf=新的TCPDF();
$pdf->AddPage('P','A4');
$html='1
名称
公司
你好
xx技术
';
$pdf->writeHTML($html,true,false,true,false,”);
$pdf->Output();
?>

如果要下载文件,请在
$pdf->Output()之前使用PHP函数
标题
如下所示:

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

请参见php.net上的

我已经修改了您的代码,它可以正常工作[测试]

<?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');
?>

输出:


添加此行
$pdf->Output($downlaodname,'D')而不是
$pdf->Output()。这将迫使浏览器下载文件。

你有PHP报告的任何错误吗?嗨,shankar,我仍然没有收到outputHi Namratha,它的工作方式很有魅力。你能描述一下错误吗?您的tcpdf库是否被正确引用?我正在尝试这样做,但它显示未找到tcpdf类,但我已添加了所有文件。一点也不,这个答案不正确,tcpdf类以本机方式提供方法,只需将此参数传递给输出方法:$pdf->Output('yourfilename.pdf','D');这就是这个答案在Chrome和我的环境中的作用