简体   繁体   English

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

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

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. TCPDF 和 mPDF 错误:一些数据已经输出到浏览器,无法发送 PDF 文件我放弃了尝试使用 TCPDF 修复错误并安装 mPDF 只是为了在尝试将文档呈现到浏览器时出现相同的错误。 I can save the document just fine and have it displayed in the browser upon retrieval.我可以很好地保存文档,并在检索时将其显示在浏览器中。

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

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).当我将整个 mPDF 库以及类和文件夹移动到 public_html 文件夹,而不是从我的应用程序文件夹(一个 symfony 项目)内部时,我终于能够了解发生了什么。 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).所以,我知道它有效,并且我知道没有与空白相关的问题或任何其他相关问题,关于代码或安装(在 mPDF/TCPD 方面)。 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.这让我相信 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?什么都没有跳到我身上……还有其他想法吗?

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();

?>

It could be some warning issued from PHP before the pdf->output.这可能是在 pdf-> 输出之前 PHP 发出的一些警告。 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.如果 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".就我而言,使用 TCPDF,我收到了很多警告,例如“date() 依赖系统的时区设置是不安全的...”,然后出现错误“某些数据已经输出到浏览器,无法发送PDF”。
Adding the function date_default_timezone_set() in my php source code solved the warnings and the error.在我的 php 源代码中添加函数 date_default_timezone_set() 解决了警告和错误。

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.然后我发现我在某些文件上有 BOM。 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 ...可能是因为在您的 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 ...因为一周前我的项目中发生了这种类型的错误......

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 的提示)

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

I have got the same error.我有同样的错误。

Data has already been sent to output, unable to output PDF file

This means before creating pdf with mPDF some data is stored in the buffer which is sended to the browser.这意味着在使用 mPDF 创建 pdf 之前,一些数据存储在发送到浏览器的缓冲区中。 Therefore it is unable to create PDF.因此无法创建PDF。

Just do this.. Add this below php built-in function at the first line of your page were you are preparing data for pdf.只需这样做.. 在页面的第一行添加以下 php 内置函数,如果您正在为 pdf 准备数据。

op_start();

And add this below php built-in function before mPDF code (before where you are calling mpdf)并在 mPDF 代码之前(在您调用 mpdf 之前)在 php 内置函数下方添加此内容

ob_end_flush();

require_once __DIR__ . '/vendor/autoload.php';

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

So that it will clear all buffer output before processing mPDF.以便在处理 mPDF 之前清除所有缓冲区输出。

I have got the same error.我有同样的错误。

Solve this using op_start();使用op_start();解决这个问题op_start(); and ob_end_clean();ob_end_clean();

PHP is an interpreted language thus each statement is executed one after another, therefore PHP tends to send HTML to browsers in chunks thus reducing performance. PHP 是一种解释性语言,因此每条语句都会一个接一个地执行,因此 PHP 倾向于将 HTML 分块发送到浏览器,从而降低性能。 Using output buffering the generated HTML gets stored in a buffer or a string variable and is sent to the buffer to render after the execution of the last statement in the PHP script.使用输出缓冲,生成的 HTML 被存储在缓冲区或字符串变量中,并在 PHP 脚本中的最后一条语句执行后发送到缓冲区进行渲染。

But Output Buffering is not enabled by default.但默认情况下不启用输出缓冲。 In order to enable the Output Buffering one must use the ob_start() function before any echoing any HTML content in a script.为了启用输出缓冲,必须在脚本中回显任何 HTML 内容之前使用 ob_start() 函数。

[reference credit][1] [参考信用][1]

[PHP | [PHP | ob_start() Function][2] ob_start() 函数][2]

public function gen_pdf($html, $user_id, $paper = 'A4') {
        ob_start();//Enables Output Buffering
        $mpdf = new mPDF('UTF-8', $paper, '', '', 15, 15, 30, 20, 15, 5);
        $mpdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins

        $header = '';
        $footer = '';
        
        $mpdf->SetHTMLHeader($header);
        $mpdf->SetHTMLFooter($footer);

        $mpdf->SetWatermarkText('Watermark', 0.1);
        $mpdf->showWatermarkText = true;
        $mpdf->WriteHTML($html);
        $fileName = date('Y_m_d_H_i_s');
        ob_end_clean();//End Output Buffering
        $mpdf->Output('Example_' . $fileName . '.pdf', 'I');
    }

So that it will clear all buffered output before processing mPDF.以便在处理 mPDF 之前清除所有缓冲输出。

Best Luck... [1]: https://www.geeksforgeeks.org/php-ob_start-function/ [2]: https://www.php.net/manual/en/function.ob-start.php祝你好运... [1]: https://www.geeksforgeeks.org/php-ob_start-function/ [2]: https://www.php.net/manual/en/function.ob-start.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mPDF错误:部分数据已经输出到浏览器,无法发送PDF文件 - mPDF error: Some data has already been output to browser, can't send PDF file TCPDF ERROR: Some data has been output to browser, can't send PDF file - TCPDF ERROR: Some data has already been output to browser, can't send PDF file TCPDF ERROR:部分数据已经输出,无法发送PDF文件 - TCPDF ERROR: Some data has already been output, can't send PDF file 获取TCPDF:一些数据已经输出,无法在Laravel 4中发送PDF文件 - Getting TCPDF:Some data has already been output, can't send PDF file in Laravel 4 TCPDF错误:已经输出了一些数据,尝试将数据放入视图时无法发送PDF文件 - TCPDF ERROR: Some data has already been output, can't send PDF file while trying to put data in view cakephp tcpd错误:一些数据已经输出到浏览器,无法在远程服务器上发送PDF文件 - cakephp tcpd error: Some data has already been output to browser, can't send PDF file on remote server 使用FPDF致命错误:有些数据已经输出,无法发送PDF文件? - Fatal error using FPDF:Some data has already been output, can't send PDF file? FPDF 错误:部分数据已经输出。 无法发送 PDF 文件 - FPDF error: some data has already been output. Can't send PDF file FPDF错误:已经输出了一些数据,无法发送PDF文件 - FPDF error: Some data has already been output, can't send PDF file magento FPDF错误:已经输出了一些数据,无法发送PDF文件 - magento FPDF error: Some data has already been output, can't send PDF file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM