DOMPDF: Unable to stream pdf: headers already sent

2019-01-25 18:20发布

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:

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

9条回答
可以哭但决不认输i
2楼-- · 2019-01-25 18:45

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');
}
查看更多
该账号已被封号
3楼-- · 2019-01-25 18:46

Replace line 3105 of this file: 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

<?php
ob_start();
查看更多
Animai°情兽
4楼-- · 2019-01-25 18:48

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.

查看更多
不美不萌又怎样
5楼-- · 2019-01-25 18:50

for me - solution was to encode my file to UTF-8 instead of UTF-8 BOM

查看更多
贼婆χ
6楼-- · 2019-01-25 18:52

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.

This will help you identify where the problem is.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-25 18:55

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.

查看更多
登录 后发表回答