DOM pdf error

doctorbill
doctorbill used Ask the Experts™
on
error when using dom pdf to generate pdf files from php records:
Error:
Unable to stream pdf: headers already sent

I have tried all the typical suggested solutions for this but I still get the error
I haven't posted any code yet because I am unsure of exactly what to post
I initially post this error in case anyone has a suggestion of whet the error is actually telling me / doing in terms of code
I am pretty sure it used to work on my server but I have not visited my code for some time so I just cannot remember if anything has changed
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This is because you have code that is trying to set HTTP headers, but the HTTP headers have been sent already.

This is the same error as the PHP one:
Warning: Cannot modify header information - headers already sent

...which I cover in my article:
https://www.experts-exchange.com/articles/32160/How-to-Fix-Just-About-Every-Common-PHP-Error.html

Basically, ANY output at all, even a line break or a single space, will tell PHP / the web server that you're done with the headers portion of the response and you're moving onto the body of the response. So more often than not, this is just a line break somewhere in an included / required file.

Author

Commented:
Attached files:

searchWorkformCompany.php
This file is the source file and the links "Create Individual PDF" sends "id2" data to the following page:

pdf_workformPDF.php
This file uses the dom pdf files and has the following file as an include:
searchWorkFormPDF.php

Can you see where the issues may be?
pdf_workformPDF.php
searchWorkformCompany.php
searchWorkFormPDF.php
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hey Doctorbill,

gr8gonzo has already pointed out that this error is caused by data being sent to the browser before calling stream(). This can include any whitespace, so you need to track that down. In the files you've posted, pdf_workformPDF.php seems to have a blank line right at the top - remove that.

Check what's in the /includes/pdf/dompdf_config.inc.php file to make sure there's no data being output from there.

Also, you seem to be using an older version of DOMPDF, so you might want to consider updating to a later version.

To debug this problem, I would start by stepping back to a very basic setup. Create a very simple static HTML file just containing a Heading. Call it template.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Chris Stanyon</title>
    </head>
    <body>
        <h1>Test PDF</h1>
    </body>
</html>

Open in new window

And now create a php file containing the folliwing (call it test.php)

<?php
require_once("/includes/pdf/dompdf_config.inc.php");

ob_start();
include('template.php');
$html = ob_get_contents();
ob_end_clean();

$dompdf = new DOMPDF();
$dompdf->load_html($html);

$dompdf->render();
$dompdf->stream("test.pdf");

Open in new window

Now call that test.php file in your browser and see if you get the test.pdf file. If you do, build up from there.

Author

Commented:
Once again - thanks all

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial