php 使用tcpdf生成pdf下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18223743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:12:09  来源:igfitidea点击:

to generate pdf download using tcpdf

phptcpdf

提问by namratha

i am not able to generate pdf download,my code is as follows,can anyone tell me what is wrong with this code.

我无法生成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();
?>

采纳答案by Shankar Damodaran

I have modified your code and it works. [TESTED]

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

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

OUTPUT:

输出:

enter image description here

在此处输入图片说明

回答by netvision73

If you want to make the file downloads, use PHP function headerbefore your $pdf->Output();like this :

如果要下载文件,请header$pdf->Output();像这样之前使用 PHP 函数:

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

See PHP function headeron php.net

请参阅php.net 上的PHP 函数标头

回答by krishnaisdinesh

Add this line $pdf->Output($downlaodname, 'D');instead of $pdf->Output();. It will force browser to download file.

添加此行$pdf->Output($downlaodname, 'D');而不是$pdf->Output();. 它会强制浏览器下载文件。