TCPDF错误:部分数据已经输出,无法发送PDF文件
TCPDF ERROR: Some data has already been output, can't send PDF file
当尝试将自己的数组添加到代码中时,我一直收到此错误。这是我的数组;
$array = array();
while (odbc_fetch_row($rs))
{
$array[] = odbc_result($rs,'Product Name');
}
$test = print_r($array);
原始代码在这里。我使用一个示例页面来尝试它,因为我知道示例页面工作得很好。
http://www.tcpdf.org/examples/example_001.phps
这段代码在$html变量之前,当它被设置时,我只是将$test变量添加到$html变量中。在我添加任何代码之前,odbc连接工作正常,示例工作正常,但当我运行脚本时,我得到这个错误;
Array ( [0] => Test1 [1] => Test2 ) TCPDF ERROR: Some data has already been output, can't send PDF file
数组中也有超过2个项目。什么好主意吗?
添加函数ob_end_clean();在调用Output函数之前。它在自定义Wordpress功能中为我工作!
ob_end_clean();
$pdf->Output($pdf_name, 'I');
在调用Output函数之前添加ob_end_clean()函数
我只是想补充说,我得到这个错误,没有什么会修复它,直到我改变Output
目的地参数从F
到FI
。换句话说,我必须同时输出到文件和内联。
Output('doc.pdf', 'I')
Output('doc.pdf', 'FI')
我不知道为什么这会产生差异,但它为我修复了错误…
导致"data has already output"的tcpdf文件位于tcpdf文件夹tcpdf.php中。你可以修改它:
添加行ob_end_clean();如下(最后第三行):
public function Output($name='doc.pdf', $dest='I') {
//LOTS OF CODE HERE....}
switch($dest) {
case 'I': {
// Send PDF to the standard output
if (ob_get_contents()) {
$this->Error('Some data has already been output, can''t send PDF file');}
//some code here....}
case 'D': { // download PDF as file
if (ob_get_contents()) {
$this->Error('Some data has already been output, can''t send PDF file');}
break;}
case 'F':
case 'FI':
case 'FD': {
// save PDF to a local file
//LOTS OF CODE HERE..... break;}
case 'E': {
// return PDF as base64 mime email attachment)
case 'S': {
// returns PDF as a string
return $this->getBuffer();
}
default: {
$this->Error('Incorrect output destination: '.$dest);
}
}
ob_end_clean(); //add this line here
return '';
}
现在让我们看看你的代码。
我看到你混淆了$rs和$sql。这是两种不同的东西一起工作。
$conn=odbc_connect('northwind','****','*****');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql="SELECT * FROM products"; //is products your table name?
$rs=odbc_exec($conn,$sql);
if (!$rs) {
exit("Error in SQL");
}
while (odbc_fetch_row($rs)) {
$prodname=odbc_result($rs,"Product Name"); //but preferably never use spaces for table names.
$prodid=odbc_result($rs,"ProdID"); //prodID is assumed attribute
echo "$prodname";
echo "$prodid";
}
odbc_close($conn);
now you can use the $prodname and output it to the TCPDF output.
,我假设你正在连接到一个MS访问数据库。
对于我的情况,Footer方法是有错误的html代码(缺少td),导致osx上的错误。
public function Footer() {
$this->SetY(-40);
$html = <<<EOD
<table>
<tr>
Test Data
</tr>
</table>
EOD;
$this->writeHTML($html);
}
我有这个,但不像OP,我看不到TCPDF错误消息之前的任何输出。
原来有一个UTF8 BOM(字节顺序标记)在我的脚本的开始,在
我有这个奇怪的错误罪魁祸首是PHP打开标签开头的空白
即使没有ob_flush
和ob_end_clean
只要确保没有多余的white spaces
在任何<?php ?>
块上或之后
使用
ob_end_clean ();
pdf ->输出美元(美元)文件中,"我");打开pdf。
我有同样的错误,但最后我通过抑制PHP错误解决了它把这个代码error_reporting(0);
放在打印页面的顶部
<?php
error_reporting(0); //hide php errors
if( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once dirname(__FILE__) . '/tohtml/tcpdf/tcpdf.php';
.... //continue
错误提示"部分数据已输出,无法发送PDF文件"是PHP的输出缓冲区。
所以你需要在发送输出之前清理输出缓冲区的所有内容。
ob_end_clean(); // Clean any content of the output buffer
然后
$pdf->Output('example_001.pdf', 'I'); // Send the PDF !
这个问题是当apache/php
显示错误。
此数据(html
)破坏pdf输出。
必须关闭php.ini中的显示错误。
对于那些仍然面临这个问题的人,请尝试添加:
libxml_use_internal_errors(true);
在loadHtml
调用前添加
libxml_use_internal_errors(false);
。