php DOMPDF:无法流式传输 pdf:标题已发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19524709/
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
DOMPDF: Unable to stream pdf: headers already sent
提问by sridhar
This question has been raised hundred of times in various fora; some have been solved, some not. I checked plenty of resources, but my issue could not be resolved. I am generating php page using smarty template based on html form input and uploaded files. Using dompdf, I want to save the generated page as a pdf file. When the user submits the multipart/form-data, data is posted to itself. Then it undergoes validation process. When all is fine, a new page is generated using a template file. There is no output, instead, dompdf utilizes the template file to stream the pdf file. After solving several stages of problems such as "DOMPDF not found", insufficient memory etc, I am now stuck with "Unable to stream pdf: headers already sent" error. One of the most common problems is presence of line break, white space or any output being before stream() is called. I checked for white space before and after ?. There are nor print_f or echo statements either. How can I troubleshoot this problem? Where does the problem lie...in the smarty template file or the php file itself? Here is the code:
这个问题已经在各种论坛上提出了数百次;有些已经解决,有些没有。我检查了大量资源,但我的问题无法解决。我正在使用基于 html 表单输入和上传文件的 smarty 模板生成 php 页面。使用dompdf,我想将生成的页面保存为pdf文件。当用户提交 multipart/form-data 时,数据被发布到它自己。然后它经历验证过程。当一切正常时,将使用模板文件生成一个新页面。没有输出,而是 dompdf 利用模板文件来流式传输 pdf 文件。在解决了“未找到 DOMPDF”、内存不足等几个阶段的问题后,我现在遇到了“无法流式传输 pdf:已发送标头”错误。最常见的问题之一是存在换行符,调用 stream() 之前的空白或任何输出。我在 ? 之前和之后检查了空格。也没有 print_f 或 echo 语句。我该如何解决这个问题?问题出在哪里......在smarty模板文件或php文件本身中?这是代码:
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$html = $smarty->fetch('index.tpl');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();
$dompdf->stream("newfile.pdf");
回答by Muhammad
simple solution :
简单的解决方案:
write below lines before stream, it will show where exactly new line or space is coming
在流之前写在下面几行,它将显示新行或空格的确切位置
$f;
$l;
if(headers_sent($f,$l))
{
echo $f,'<br/>',$l,'<br/>';
die('now detect line');
}
回答by periklis
Most probably there's a whitespace or new line somewhere in your code causing this. Here's a simple way to debug it:
很可能在您的代码中某处有一个空格或新行导致了这种情况。这是一个简单的调试方法:
- echo something at the very end of your script (above the stream() call), for example
echo "end!";exit;
- Click to "view source" of your page, which makes spaces easier to see
- If your "end!" string does not appear at the very start of your script, then somewhere there's a character printed
- Move the "echo end!" line further up to your code, until you locate where the space was inserted
- 例如,在脚本的最后(在 stream() 调用之上)回显某些内容
echo "end!";exit;
- 点击页面的“查看源代码”,让空间更易于查看
- 如果你的“结束!” 字符串没有出现在脚本的开头,然后在某处打印了一个字符
- 移动“回声结束!” 进一步与您的代码对齐,直到您找到插入空格的位置
Another possibility is that you are using a language string somewhere that introduces an unprintable character. If your application is multilingual, make sure you're testing using english
另一种可能性是您在某处使用了引入不可打印字符的语言字符串。如果您的应用程序是多语言的,请确保您正在使用英语进行测试
回答by sridhar
I tried to echo out, but no white spaces or line breaks were found. At the end, I redirected the php to another page instead of PHP_SELF and the problem vanished. I did not alter any code. Looks like presence of html tags after the php ended was the offending factor.
我试图回显,但没有发现空格或换行符。最后,我将 php 重定向到另一个页面而不是 PHP_SELF,问题就消失了。我没有更改任何代码。看起来 php 结束后 html 标签的存在是有问题的因素。
回答by Ksi?ciunio
Be sure your editor does not add a Unicode Bom description - save code to file by Notepad, or (if you work in Dreamweaver) remove check by Asign Unicode Signature (BOM) or something. ;)
确保您的编辑器没有添加 Unicode Bom 描述 - 通过记事本将代码保存到文件,或者(如果您在 Dreamweaver 中工作)通过指定 Unicode 签名 (BOM) 或其他方式删除检查。;)
回答by OnTarget
Replace line 3105 of this file: dompdf/lib/class.pdf.php
替换此文件的第 3105 行: dompdf/lib/class.pdf.php
if ( headers_sent()) {
die("Unable to stream pdf: headers already sent");
}
With
和
$output = ob_get_clean();
if ( headers_sent()) {
echo $output; }
Then in the file that is generating the pdf output e.g if you were outputting from a Joomla Component components/com_joomlacomponent/views/yourpdfdir/tmpl/default.php Enter immediately after the opening php tag
然后在生成 pdf 输出的文件中,例如,如果您从 Joomla Component components/com_joomlacomponent/views/yourpdfdir/tmpl/default.php 输出,请在打开 php 标签后立即输入
<?php
ob_start();
回答by william.eyidi
I came across this issue. You want to check all the variables you are using. One or even more than one variable you are passing comes empty and is messing the render.
我遇到了这个问题。您想检查您正在使用的所有变量。您传递的一个或什至多个变量为空,并且弄乱了渲染。
Start gradually, by getting rid of all the php
and try to generate the pdf, then if it works for you add code block by block.
逐渐开始,通过摆脱所有php
并尝试生成 pdf,然后如果它适合您,则逐块添加代码。
This will help you identify where the problem is.
这将帮助您确定问题所在。
回答by Bob Hockney
I had this issue, with no apparent output when viewing the source. The problem for me was that I had flushed the output, even though there was none except the headers, and that blocked streaming the output giving the "headers already sent" message. Which was true. My fix was to remove the flush() and ob_flush() statements, and the streaming output then worked.
我遇到了这个问题,查看源代码时没有明显的输出。对我来说的问题是我已经刷新了输出,即使除了标题之外没有任何输出,并且阻止了输出的流输出,给出了“标题已经发送”的消息。这是真的。我的解决方法是删除 flush() 和 ob_flush() 语句,然后流输出就可以工作了。
回答by Unkas
for me - solution was to encode my file to UTF-8 instead of UTF-8 BOM
对我来说 - 解决方案是将我的文件编码为 UTF-8 而不是 UTF-8 BOM
回答by David
In my case the problem was solved by setting $_dompdf_show_warnings to false in dompdf_config_inc.php
在我的情况下,问题是通过在 dompdf_config_inc.php 中将 $_dompdf_show_warnings 设置为 false 来解决的