【发布时间】:2020-07-09 01:06:48
【问题描述】:
我正在尝试使用 html2pdf 创建 php 文件的 pdf,该文件包含从 mysql 数据库获取的数据并通过使用 while 循环生成的表,但是当我尝试运行 php 文件时,它说以下内容错误
TCPD 错误:一些数据已经输出,无法发送 PDF 文件
谁能帮我解决这个问题?这是我的代码:
<?php
session_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
//error_reporting(0);
/*
Author : yaser kazi
Description : This File Contain Additions and Editing part for the Paxes
*/
if (!isset($_SESSION["username"])) {
header("location: unauthorize_access.html");
exit();
}
if (isset($_GET["cityprin"])) {
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db('webapp') or die(mysql_error());
$get = mysql_query("SELECT code, `name` FROM city ORDER BY `name`") or die(mysql_error());
$ctr = 0;
ini_set("memory_limit", "50M");
ob_start();
?>
<style type="text/css" media="all">
<!--
body {
margin:0px;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
span {
font-size:14px;
}
td {
width: 100mm;
max-width: 100mm;
height: 33mm;
max-height:33mm;
top:0mm;
}
table {
border-collapse:collapse;
}
-->
</style>
<?php
echo "<table border=\"1\" align=\"right\" style =\"width: 205mm;\">";
echo "<th>Srno</th>";
echo "<th>Code</th>";
echo "<th>Name</th>";
while ($city = mysql_fetch_array($get)) {
$ctr++;
echo "<td style=\"width: 100mm; max-width: 100mm; height:33mm; padding-left:1mm;\" valign=\"right\">".$ctr."</td>";
echo "<td style=\"width: 100mm; max-width: 100mm; height:33mm; padding-left:7mm;\" valign=\"right\">".$city['code']."</td>";
echo "<td style=\"width: 100mm; max-width: 100mm; height:33mm; padding-left:7mm;\" valign=\"right\">".$city['name']."</td>";
}
echo "</table>";
} // Submit Tag Close
?>
<?php
$content = ob_get_clean();
// conversion HTML => PDF
require_once('html2pdf/html2pdf.class.php');
try {
$html2pdf = new HTML2PDF('P', 'A4', 'EN', false, 'ISO-8859-15', array("5mm", "13mm", "0mm", "0mm"));
$html2pdf->pdf->SetAuthor('CepheiSys');
$html2pdf->pdf->SetTitle('City_list');
// $html2pdf->pdf->SetSubject('HTML2PDF Wiki');
// $html2pdf->pdf->SetKeywords('HTML2PDF, TCPDF, example, wiki');
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($content);
$html2pdf->Output('City_list.pdf');
} catch (HTML2PDF_exception $e) {
echo $e;
}
?>
【问题讨论】:
-
如果您查看 HTML 而不是生成 PDF 会发生什么?
-
显示错误 TCPDF ERROR: Some data has been output, can't send PDF file![enter image description here][1]
-
可以帮我解决这个问题
-
这不能回答我的问题。如果您注释掉所有生成 PDF 的代码并打印出 HTML,您会得到什么?我怀疑你在某个地方有一个错误的空间,但是这段代码看起来像是在输出缓冲区中捕获了所有的空间。
-
然后它显示一个没有内容的空白文件