php TCPDF ERROR:部分数据已经输出,无法发送PDF文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16011050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:22:26  来源:igfitidea点击:

TCPDF ERROR: Some data has already been output, can't send PDF file

phptcpdf

提问by Alex

I keep receiving this error when trying to add my own array into the code. Here is my array;

尝试将我自己的数组添加到代码中时,我不断收到此错误。这是我的数组;

$array = array();

while (odbc_fetch_row($rs))
{
$array[] = odbc_result($rs,'Product Name');
} 
$test = print_r($array);

The original code is here. I'm using an example page to try it because I know the example page works fine.

原始代码在这里。我正在使用示例页面来尝试它,因为我知道示例页面工作正常。

http://www.tcpdf.org/examples/example_001.phps

http://www.tcpdf.org/examples/example_001.phps

This code is before the $html variable and when it is set I just add the $test variable into the $html variable. The odbc connection works fine and the example works fine before I add any code but when I run the script I get this error;

这段代码在 $html 变量之前,当它被设置时,我只是将 $test 变量添加到 $html 变量中。在我添加任何代码之前,odbc 连接工作正常,示例工作正常,但是当我运行脚本时出现此错误;

Array ( [0] => Test1 [1] => Test2 ) TCPDF ERROR: Some data has already been output, can't send PDF file

And there is also more than 2 items in the Array. Any ideas?

并且Array中还有2个以上的项目。有任何想法吗?

回答by vishal jaura

Just use ob_start(); at the top of the page.

只需使用 ob_start(); 在页面顶部。

回答by fmarrot

Add the function ob_end_clean(); before call the Output function. It worked for me within a custom Wordpress function!

添加函数 ob_end_clean(); 在调用输出函数之前。它在自定义 Wordpress 功能中对我有用!

ob_end_clean();
$pdf->Output($pdf_name, 'I');

回答by user2928471

Add the function ob_end_clean() before call the Output function.

在调用输出函数之前添加函数 ob_end_clean()。

回答by Black

I just want to add that I was getting this error, and nothing would fix it until I changed the Outputdestination parameter from Fto FI. In other words, I have to output to both file and inline.

我只想补充一点,我得到这个错误,直到我改变了什么都不会解决它Output从目标参数FFI。换句话说,我必须输出到文件和内联。

Output('doc.pdf', 'I')

to

Output('doc.pdf', 'FI')

I have no idea why this made the difference, but it fixed the error for me...

我不知道为什么这会有所不同,但它为我修复了错误......

回答by xavit

This problem means you have headers. Deletes tags

这个问题意味着你有标题。删除标签

?>

?>

at the end of your code and make sure not to have whitespace at the beginning.

在代码的末尾,并确保开头没有空格。

回答by Clark Superman

The tcpdf file that causes the "data has already been output" is in the tcpdf folder called tcpdf.php. You can modify it:

导致“数据已输出”的 tcpdf 文件位于名为 tcpdf.php 的 tcpdf 文件夹中。你可以修改它:

add the line ob_end_clean();as below (3rd last line):

添加行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 '';
}

Now lets look at your code.
I see you have $rs and $sql mixed up. These are 2 different things working together.

现在让我们看看你的代码。
我看到你把 $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.  

and I assume your are connecting to a MS access database.

我假设您正在连接到 MS Access 数据库。

回答by him

use ob_end_clean();

使用 ob_end_clean();

$pdf->Output($file, 'I'); to open pdf. It works for me

$pdf->输出($文件,'我');打开pdf。这个对我有用

回答by Dipanshu Chaubey

Use ob_start(); at the beginning of your code.

使用 ob_start(); 在代码的开头。

回答by ranjeetcao

for my case Footer method was having malformed html code (missing td) causing error on osx.

就我而言,页脚方法的 html 代码格式错误(缺少 td)导致 osx 错误。

public function Footer() {
$this->SetY(-40);
$html = <<<EOD
<table>
<tr>
 Test Data
</tr>
</table>
EOD;
$this->writeHTML($html);
}

回答by Syboor

I had this but unlike the OP I couldn't see any output before the TCPDF error message.

我有这个,但与 OP 不同,我在 TCPDF 错误消息之前看不到任何输出。

Turns out there was a UTF8 BOM (byte-order-mark) at the very start of my script, before the <?php tag so before I had any chance to call ob_start(). And there was also a UTF8 BOM before the TCPDF error message.

原来在我的脚本的一开始就有一个 UTF8 BOM(字节顺序标记),在 <?php 标签之前,所以在我有机会调用 ob_start() 之前。并且在 TCPDF 错误信息之前还有一个 UTF8 BOM。