DOMPDF: Unable to stream pdf: headers already sent
Asked Answered
M

10

7

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");
Maenad answered 22/10, 2013 at 17:39 Comment(2)
Check that dompdf itself (or smarty) aren't doing some hidden output. e.g. comment out the $dompdf-stream() call, hit the url, and save the contents... see what's in there.Berlauda
which smarty version are you using? In some old versions there was a problem of smarty adding spaces at the beginning of the compiled fileMuricate
Z
19

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');
}
Zwart answered 6/7, 2015 at 13:12 Comment(5)
Wow, that's genius! Helped me find out that a remote file included was in UTF-8 but not without BOM.Scaleboard
Great!!! Awesome that help me to resolved my header already sent issue while using dompdf library in Codeingitor. Thanks!!!Enrage
I remove all white space still getting same error .Please let me know if you have another solution.Schoolroom
I 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 the ob_get_clean(); fixed it.Yoga
Genious solution!Connotative
W
4

I had the same problem, and I solved this problem by adding this code in the top file:

ob_start();
Whitleather answered 18/10, 2020 at 4:12 Comment(0)
E
3

Most probably there's a whitespace or new line somewhere in your code causing this. Here's a simple way to debug it:

  1. echo something at the very end of your script (above the stream() call), for example echo "end!";exit;
  2. Click to "view source" of your page, which makes spaces easier to see
  3. If your "end!" string does not appear at the very start of your script, then somewhere there's a character printed
  4. 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

Exhibition answered 23/10, 2013 at 8:58 Comment(0)
S
1

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();
Sopor answered 8/10, 2014 at 9:27 Comment(0)
M
0

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.

Maenad answered 23/10, 2013 at 11:28 Comment(2)
You should be calling exit; after the stream() functionExhibition
@Exhibition 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).Verlie
C
0

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

Couthie answered 16/7, 2014 at 0:0 Comment(0)
P
0

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.

Phebephedra answered 3/2, 2015 at 14:54 Comment(0)
B
0

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.

Birdlime answered 6/8, 2016 at 20:11 Comment(0)
A
0

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

Aggri answered 10/9, 2016 at 0:21 Comment(0)
O
-1

In my case the problem was solved by setting $_dompdf_show_warnings to false in dompdf_config_inc.php

Ostrander answered 20/3, 2015 at 12:19 Comment(1)
Hiding errors aren't really solving them, even though they might not be criticalMutazilite

© 2022 - 2025 — McMap. All rights reserved.