TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file

感情迁移 提交于 2019-11-28 13:34:56

Most probably it's BOM marker, use your IDE to remove it, other hot fix can be:

<?php

$html = "Hello World";
include("../mpdf.php");

ob_clean(); // cleaning the buffer before Output()

$mpdf=new mPDF(); 
$mpdf->WriteHTML($html);
$mpdf->Output();

?>

It could be some warning issued from PHP before the pdf->output. The warning text is sent to the client's browser and thus the file cannot be sent.
If your warning level is not the same for DEV and PROD, that could explain the difference of behavior.

In my case, with TCPDF, I had many warnings such as "date() it is not safe to rely on the system's timezone settings...", then the error "Some data has already been output to browser, can't send PDF".
Adding the function date_default_timezone_set() in my php source code solved the warnings and the error.

Nicolas400

I have the same issue, and add this line before $pdf->output():

error_reporting(E_ALL);

An then I found that I have BOM on some files. And I see a Warning message sent to the browser.

Best Luck !!

Regards

May be it occurs because of in your result of HTML code have some error to output to create the TCPDF ...

OR

If above is not work try set the Charset as UTF-8 in class file of TCPDF may it solve your issue...

Because this type of error was happening in my project before one week ago ...

Remove any file you would have included at the start of the page. In my case it was a file that was connecting with database. It worked for me. (Tip from @Nicolas400 )

Try using ob_clean(); before include("../mpdf.php");.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!