简体   繁体   中英

to generate pdf download using tcpdf

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

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

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:

在此输入图像描述

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

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

See PHP function header on php.net

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM