我使用TCPDF制作了pdf文件,并使用了writeHTML()
函数。我有一个从MySQL代码生成的表,该表显示在我的网站上,该表也显示在pdf上,并添加了一个名为fetch_data()
的函数。第一个表的代码如下:
if (isset($_GET['report'])) {
$order_id = ($_GET['report']);
function fetch_data() {
$output = '';
require 'dbh.php';
$order_id = ($_GET['report']);
$sql = "SELECT * FROM samples_database JOIN results_database ON samples_database.sample_id = results_database.sample_id JOIN microbiology_analysis_database ON results_database.m_analysis_id = microbiology_analysis_database.id WHERE samples_database.order_id = $order_id;";
$result = mysqli_query($conn, $sql);
$input = mysqli_fetch_array($result);
$order_number = $input['order_number'][0];
while($row = mysqli_fetch_array($result)) {
$output .= '
<table>
<thead>
<tr>
<th>Sample ID</th>
<th>Client ID</th>
<th>Analysis</th>
<th>Detected</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row["env_sam_id"]; ?></td>
<td><?php echo $row["c_sam_id"]; ?></td>
<td><?php echo $row["m_analysis"]; ?></td>
<td><?php echo $row["detected"]; ?></td>
<td><?php echo $row["result"]; ?></td>
</tr>
</tbody>
</table>
';
}
return $output;
}
}
生成的pdf效果很好,直到添加了名为fetch_data2()
的附加html表格元素,如下所示:
if (isset($_GET['report'])) {
$order_id = ($_GET['report']);
function fetch_data2() {
$output = '';
require 'dbh.php';
$order_id = ($_GET['report']);
$sql = "SELECT * FROM order_database JOIN client_database ON order_database.client_id = client_database.id WHERE order_database.id = $order_id;";
$result = mysqli_query($conn, $sql);
$input = mysqli_fetch_array($result);
$order_number = $input['order_number'];
$time1 = $input['time1'];
$date1 = $input['date1'];
$client_first_name = $input['client_first_name'];
$client_last_name = $input['client_last_name'];
$company_name = $input['company_name'];
$phone = $input['phone'];
$email = $input['email'];
{
$output .= '
<table>
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Order Number:</strong></td>
<td><input type="text" name="date1" value="'.$order_number.'" readonly></td>
</tr>
<tr>
<td><strong>Order Date:</strong></td>
<td><input type="text" name="date1" value="'.$date1.'" readonly></td>
</tr>
<tr>
<td><strong>Time Placed:</strong></td>
<td><input type="text" name="time1" value="'.$time1.'" readonly></td>
</tr>
<tr>
<td><strong>Client:</strong></td>
<td><input type="text" name="client_name" value="'.$client_first_name.' '.$client_last_name.'" readonly></td>
</tr>
<tr>
<td><strong>Company:</strong></td>
<td><input type="text" name="company_name" value="'.$company_name.'" readonly></td>
</tr>
<tr>
<td><strong>Email*:</strong></td>
<td><input type="text" name="email" value="'.$email.'"></td>
</tr>
<tr>
<td><strong>Contact Number*:</strong></td>
<td><input type="text" name="phone" value="'.$phone.'"></td>
</tr>
</tbody>
</table>
';
}
return $output;
}
}
以下是用于生成PDF文件的代码:
if(isset($_POST["generate_pdf"])) {
$file_name = 'Microbiology_Report_'.$order_number.'.pdf';
require_once('tcpdf/tcpdf.php');
require_once('tcpdf/config/tcpdf_config.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($_SESSION['logged_in_id']);
$pdf->SetTitle($file_name);
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setFooterData();
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 10, '', true);
$pdf->AddPage();
$html = '';
$html .= fetch_data2();
$pdf->writeHTML($html, true, false, true, false, '');
$html = '<h1 style="color:green;">Testing</h1>';
$html .= '
<h4 align="center">Envirocare Microbiology Report</h4><br />
<table border="1" cellspacing="0" cellpadding="3">
<thead>
<tr>
<th>Sample ID</th>
<th>Client ID</th>
<th>Analysis</th>
<th>Detected</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr> ' ;
$html .= fetch_data();
$html .= '
</tr>
</tbody>
</table>
';
$pdf->writeHTML($html, true, false, true, false, '');
$html = '
<h3>Abbreviations and remarks:</h3>
<p>CFU: Colony Forming Units</p>
';
$pdf->writeHTML($html, true, false, true, false, '');
ob_end_clean();
$pdf->Output($file_name, 'I');
}
我知道错误与fetch_data2()
有关,但我不知道是什么原因导致错误或如何格式化代码以完成工作。该错误包含各种未定义的索引错误以及以下内容:TCPDF ERROR: Some data has already been output to browser, can't send PDF file
。任何人都可以帮助减轻这个问题吗?
编辑
这是完整的错误报告:
Notice: Undefined index: startcolumn in
C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19480
Notice: Undefined index: startx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19481
Notice: Undefined index: startpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19484
Notice: Undefined index: startpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19487
Notice: Undefined index: in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19487
Notice: Undefined index: startpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19488
Notice: Undefined index: in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19488
Notice: Undefined offset: -1 in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19543
Notice: Undefined index: startx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19740
Notice: Undefined variable: startpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19741
Notice: Undefined variable: endpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19741
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269
Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123
Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php:18188) in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 7625
TCPDF ERROR: Some data has already been output to browser, can't send PDF file
答案 0 :(得分:0)
我发现问题在于下面的代码段。在这种情况下,我试图系统地创建一个表,其中标题位于第一列,数据库信息位于第二列。原来,PDF生成器对于哪个是哪个以及如何将其系统地附加到PDF感到困惑。
if (isset($_GET['report'])) {
$order_id = ($_GET['report']);
function fetch_data2() {
$output = '';
require 'dbh.php';
$order_id = ($_GET['report']);
$sql = "SELECT * FROM order_database JOIN client_database ON order_database.client_id = client_database.id WHERE order_database.id = $order_id;";
$result = mysqli_query($conn, $sql);
$input = mysqli_fetch_array($result);
$order_number = $input['order_number'];
$time1 = $input['time1'];
$date1 = $input['date1'];
$client_first_name = $input['client_first_name'];
$client_last_name = $input['client_last_name'];
$company_name = $input['company_name'];
$phone = $input['phone'];
$email = $input['email'];
{
$output .= '
<table>
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Order Number:</strong></td>
<td><input type="text" name="date1" value="'.$order_number.'" readonly></td>
</tr>
<tr>
<td><strong>Order Date:</strong></td>
<td><input type="text" name="date1" value="'.$date1.'" readonly></td>
</tr>
<tr>
<td><strong>Time Placed:</strong></td>
<td><input type="text" name="time1" value="'.$time1.'" readonly></td>
</tr>
<tr>
<td><strong>Client:</strong></td>
<td><input type="text" name="client_name" value="'.$client_first_name.' '.$client_last_name.'" readonly></td>
</tr>
<tr>
<td><strong>Company:</strong></td>
<td><input type="text" name="company_name" value="'.$company_name.'" readonly></td>
</tr>
<tr>
<td><strong>Email*:</strong></td>
<td><input type="text" name="email" value="'.$email.'"></td>
</tr>
<tr>
<td><strong>Contact Number*:</strong></td>
<td><input type="text" name="phone" value="'.$phone.'"></td>
</tr>
</tbody>
</table>
';
}
return $output;
}
}
解决方案是创建一个传统表,其中列标题位于<thead>
标记的顶部,而实际表中的行来自php代码。
if (isset($_GET['report'])) {
$order_id = ($_GET['report']);
function fetch_data2() {
$output = '';
require 'dbh.php';
$order_id = ($_GET['report']);
$sql = "SELECT * FROM order_database JOIN client_database ON order_database.client_id = client_database.id WHERE order_database.id = $order_id;";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
$output .= '
<td>'.$row["order_number"].'</td>
<td>'.$row["date1"].'</td>
<td>'.$row["time1"].'</td>
<td>'.$row["client_first_name"].' '.$row["client_last_name"].'</td>
<td>'.$row["company_name"].'</td>
<td>'.$row["email"].'</td>
<td>'.$row["phone"].'</td>
';
}
return $output;
}
}
最后,将fetch_data2()
添加到了生成的PDF代码中,并将表头添加为html代码:
$pdf->AddPage();
$html = '';
$html .= '<h1 style="color:green;">Microbiology Report</h1>';
$html .= '
<table border="1" cellspacing="0" cellpadding="3">
<thead>
<tr>
<th><strong>Order Number:</strong></th>
<th><strong>Order Date:</strong></th>
<th><strong>Time Placed:</strong></th>
<th><strong>Client:</strong></th>
<th><strong>Company:</strong></th>
<th><strong>Email*:</strong></th>
<th><strong>Contact Number*:</strong></th>
</tr>
</thead>
<tbody>
<tr>
';
$html .= fetch_data2();
$html .= '
</tr>
</tbody>
</table>
';
$pdf->writeHTML($html, true, false, true, false, '');