无法使用TCPDF从MySQL查询生成PDF

时间:2014-12-30 09:12:32

标签: php mysql pdf tcpdf

我3天大的PHP编程。我已经能够使用PHP回显下面代码的while循环,但是当尝试使用TCPDF在PDF文件上输出它时,无论如何都无法使它工作。

非常感谢您的帮助。

<?php
//Start building PDF
require_once('.../tcpdf.php');

//Create new PDF document
$pdf = new TCPDF();

//Set default monospaced font
$pdf->SetDefaultMonospacedFont('Arial');

//Add a page
$pdf->AddPage();

//Database parameter
$username = "root";
$password = "password";
$hostname = "localhost";
$database = "testdb";

//connection to database
$dbhandle = mysqli_connect($hostname, $username, $password, $database)
    or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//execute the SQL query and return records
$result = mysqli_query($dbhandle, "SELECT macid, macstatus FROM machid");

//Print query result
$tbl = '<table width="600px" border="1px">';

while($row = mysqli_fetch_array($result)) {
    $macid = $row["macid"];
    $macstatus = $row["macstatus"];

    $tbl .= '<tr><td>' . $macid . '</td><td>' . $macstatus . '<br></td><t/r>';
    }

$tbl .= '</table>';

//Print query result
$pdf->WriteHTML($tbl, true, false, false, false, '');

//Close and output PDF document
$pdf->Output('pdftest.pdf', 'I');

?>

1 个答案:

答案 0 :(得分:0)

请考虑,看看HTTP协议(可能有用) 见:http://php.net/manual/en/function.header.php

并最终添加到您的代码中

header("Cache-Control: private");
header("Content-type: application/pdf");
header("Content-Disposition: attachment filename=myfilename.pdf");

所以在答案标题中,请参阅Firefox中的网络部分(CTRL + MAJ + Q),

希望这可以帮助您查看错误,主要是来自网络服务器的沉默错误