DOMPDF: Unable to stream pdf: headers already sent
Solution 1
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');
}
Solution 2
I had the same problem, and I solved this problem by adding this code in the top file:
ob_start();
Solution 3
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
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
Solution 4
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();
sridhar
Updated on July 20, 2021Comments
-
sridhar almost 3 years
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 <?php and >?.
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");
-
periklis over 10 yearsYou should be calling
exit;
after thestream()
function -
dhaupin over 8 years@periklis Thanks. That
exit;
solves the headers warn when DOMPDF is wrapped with Opencart as $this->document->pdf() method. That should be noted in docs somewhere (maybe i missed it). -
rAthus almost 8 yearsWow, that's genius! Helped me find out that a remote file included was in UTF-8 but not without BOM.
-
khushi over 7 yearsGreat!!! Awesome that help me to resolved my header already sent issue while using dompdf library in Codeingitor. Thanks!!!
-
Kshitij Soni about 7 yearsI remove all white space still getting same error .Please let me know if you have another solution.
-
Dan almost 5 yearsI wrapped my PDF markup in the php brackets and then did logic after them.
<?php ob_start(); ?> /* pdf markup */ <?php $html = ob_get_clean(); ?> <?php // more logic.
Removing the?>
after theob_get_clean();
fixed it. -
Arnis Juraga about 4 yearsGenious solution!
-
Uriahs Victor about 4 yearsHiding errors aren't really solving them, even though they might not be critical