TCPDF和mPDF错误:某些数据已经输出到浏览器,无法发送PDF文件 [英] TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file

查看:757
本文介绍了TCPDF和mPDF错误:某些数据已经输出到浏览器,无法发送PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

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

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.

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

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).

故障排除:

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

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

在writeHtml之前,使用ob_clean()方法对TCPDF进行了相同的尝试.在所有情况下都存在相同的错误(我可以向所有人保证这不是与空格相关的问题-我什至以十六进制查看了文件,以确保编辑器不会插入任何奇数字符).

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).

可能的线索:

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

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.

有人对这里发生的事情有任何想法吗?

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

更新:流转储:

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?

推荐答案

很有可能是BOM标记,请使用IDE删除它,其他修补程序可以是:

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

?>

这篇关于TCPDF和mPDF错误:某些数据已经输出到浏览器,无法发送PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆