php TCPDF & mPDF 错误:部分数据已经输出到浏览器,无法发送PDF文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20146967/
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 02:54:54  来源:igfitidea点击:

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

phpsymfonypdftcpdfmpdf

提问by Patrick

The Problem:

问题:

TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file I gave up on trying to fix the error with TCPDF and installed mPDF only to get the same error when attempting to render the document to the browser. I can save the document just fine and have it displayed in the browser upon retrieval.

TCPDF 和 mPDF 错误:某些数据已经输出到浏览器,无法发送 PDF 文件我放弃了尝试使用 TCPDF 修复错误并安装 mPDF 只是为了在尝试将文档呈现到浏览器时出现相同的错误。我可以很好地保存文档,并在检索时将其显示在浏览器中。

Additionally, this error only presented itself after switching from my dev server to my host server. Works fine on DEV server (DEV server = WAMPSERVER, PROD server = Hostgator Linux).

此外,此错误仅在从我的开发服务器切换到我的主机服务器后才出现。在 DEV 服务器上工作正常(DEV 服务器 = WAMPSERVER,PROD 服务器 = Hostgator Linux)。

Troubleshooting:

故障排除:

I've read the many volumes of other discussions around the internet regarding this problem and I can find no white space related issue. I have condensed the request to the following:

我已经阅读了互联网上关于这个问题的许多其他讨论,但我找不到与空白相关的问题。我已将请求浓缩为以下内容:

<?php
ob_start(); 
$html = "Hello World";
include("../mpdf.php");
$mpdf=new mPDF(); 
$mpdf->WriteHTML($html);
$mpdf->Output();
ob_end_clean();
?>

Tried the same concept with TCPDF using ob_clean() method before writeHtml. Same error in all cases (I can assure everyone this is no white space related issue - I even viewed the file in hex to make sure there were no odd characters being inserted by the editor).

在 writeHtml 之前使用 ob_clean() 方法尝试了与 TCPDF 相同的概念。在所有情况下都出现相同的错误(我可以向所有人保证这不是与空格相关的问题 - 我什至以十六进制查看文件以确保编辑器没有插入奇怪的字符)。

Possible Clue:

可能的线索:

I was finally able to get a clue as to what's going on when I moved the entire mPDF library and classes and folders to the public_html folder, rather than from inside my application folder (a symfony project). Under this scenario, when I pointed my browser to the example pages, it rendered just fine with no errors at all (and it was super fast btw). So, I know it works, and I know there is no white-space related issue, or any other related issue, regarding the code or installation (on the mPDF/TCPDF side of things). Which leads me to believe either symfony is inserting headers of some sort (which I tried removing using: clearHttpHeaders() ), or there is a PHP INI or CONFIG setting I am missing somewhere on the PROD server.

当我将整个 mPDF 库以及类和文件夹移动到 public_html 文件夹,而不是从我的应用程序文件夹(一个 symfony 项目)内部时,我终于能够了解发生了什么。在这种情况下,当我将浏览器指向示例页面时,它呈现得很好,根本没有错误(顺便说一句,它非常快)。所以,我知道它有效,并且我知道没有与空白相关的问题或任何其他相关问题,关于代码或安装(在 mPDF/TCPD 方面)。这让我相信 symfony 正在插入某种类型的标头(我尝试使用: clearHttpHeaders() 删除它),或者有一个 PHP INI 或 CONFIG 设置我在 PROD 服务器上的某个地方丢失了。

Does anyone have ANY idea of what's going on here??

有没有人知道这里发生了什么?

Update:stream dump:

更新:流转储:

Request URL:http://www.example.com/mpdf
Request Method:GET
Status Code:200 OK

Request Headers
GET /mpdf HTTP/1.1
Host: www.example.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: __utma=44708724.1463191694.1383759419.1383759419.1383765151.2; __utmz=44708724.1383759419.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); PHPSESSID=9c7c802200b9d8eefe718447755add5f; __utma=1.813547483.1383767260.1385127878.1385130071.38; __utmb=1.7.10.1385130071; __utmc=1; __utmz=1.1383767260.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

Response Headers
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Type:text/html
Date:Fri, 22 Nov 2013 14:59:52 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=75
Pragma:no-cache
Server:Apache
Transfer-Encoding:chunked

Nothing is jumping out at me... any other thoughts?

什么都没有跳到我身上……还有其他想法吗?

回答by biesior

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

很可能是 BOM 标记,使用您的 IDE 将其删除,其他修补程序可以是:

<?php

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

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

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

?>

回答by tetorea

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.

这可能是在 pdf-> 输出之前 PHP 发出的一些警告。警告文本被发送到客户端的浏览器,因此无法发送文件。
如果 DEV 和 PROD 的警告级别不同,则可以解释行为的差异。

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.

就我而言,使用 TCPDF,我有很多警告,例如“date() 依赖系统的时区设置是不安全的...”,然后是错误“某些数据已经输出到浏览器,无法发送PDF”。
在我的 php 源代码中添加函数 date_default_timezone_set() 解决了警告和错误。

回答by Nicolas400

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

我有同样的问题,并在 $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.

然后我发现我在某些文件上有 BOM。我看到一条警告消息发送到浏览器。

Best Luck !!

好运 !!

Regards

问候

回答by Jignesh Vagh

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

可能是因为在您的 HTML 代码结果中有一些错误输出以创建 TCPPDF ...

OR

或者

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

如果以上方法无效,请尝试在 TCPDF 的类文件中将字符集设置为 UTF-8 可能会解决您的问题...

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

因为一周前我的项目中发生了这种类型的错误......

回答by Zigsaw Consultancy

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 )

删除您在页面开头包含的任何文件。就我而言,它是一个与数据库连接的文件。它对我有用。(来自@Nicolas400 的提示)

回答by Sawan

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

尝试使用ob_clean();之前include("../mpdf.php");