Tiago Nicastro Tiago Nicastro - 4 months ago 130
PHP Question

dompdf error Unable to stream pdf: headers already sent

My case is a little bit more complicated than the others answered before. with the same code the results are different.

WORKING CENARIO - 1st step

Go to http://renamchristofoletti.com/en/works/covers/

Click on Thumbnails, and ADD TILL 4 images.

Click on Download, and than, Download Portfolio.

PDF was generated as expected. =)

ERROR CENARIO - 2nd step

Do the same thing as above, but now, add more than 6 images in the Thumbnails.

PDF get a "Unable to stream pdf: headers already sent error"

Doesn't matter with image are you selecting, same thing happens.

DOMPDF CODE

ob_clean();
echo '<html><body>...'; // above html and css goes here

$images2 = str_replace(' ', "", $_COOKIE['rc_folio']);
$images = explode(',', $images2);
$images = array_filter($images);

foreach ( $images as $image ) {
$img_id = get_image_id($image);

$imglocal = wp_get_attachment_metadata( $img_id );

$img_abs_path = ABSPATH . 'wp-content/uploads/' . $imglocal['file'];
list($width, $height) = getimagesize($img_abs_path);
if ($width > $height) {
// Landscape
echo '<div style="page-break-after: always; text-align: center;"><div class="content-line"></div><div class="content-line-2"></div><img src="' . $img_abs_path . '" style="width: 80%; max-width: 80%; height: auto; margin-top: 250px;" /></div>';
} else {
// Portrait or Square
echo '<div style="page-break-after: always; text-align: center;"><div class="content-line"></div><div class="content-line-2"></div><img src="' . $img_abs_path . '" style="width: auto; height: 60%; max-height: 60%; margin-top: 180px;" /></div>';
}
}
echo '</body></html>';

$html = ob_get_contents();
ob_get_clean();

$pdf_path = ABSPATH . 'wp-content/themes/moqueca/dompdf-master/dompdf_config.inc.php';
require_once( $pdf_path );

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('letter', 'portrait');
$dompdf->render();
$dompdf->stream("renam_christofoletti_customfolio.pdf");
exit();


Please can anyone clarify my mind about this? I've tried many other solutions, but nothing seens to work.

Thanks in advance!

Answer

You appear to be running up against a limit in the output buffer size. You can check to see if there's a size limit by checking the value of the output_buffering configuration parameter:

echo ini_get('output_buffering');

If this is the case you can either modify the buffer size:

ini_set('output_buffering', true); // no limit
ini_set('output_buffering', 12288); // 12KB limit

or follow the instructions in Dharam's comment to your question and push the content into a variable directly rather than relying on output buffering:

$html = '<html><body>...'; // above html and css goes here

$images2 = str_replace(' ', "", $_COOKIE['rc_folio']);
$images = explode(',', $images2);
$images = array_filter($images);

foreach ( $images as $image ) {
    $img_id = get_image_id($image);

    $imglocal = wp_get_attachment_metadata( $img_id );

        $img_abs_path = ABSPATH . 'wp-content/uploads/' . $imglocal['file'];
        list($width, $height) = getimagesize($img_abs_path);
        if ($width > $height) {
            // Landscape
            $html .= '<div style="page-break-after: always; text-align: center;"><div class="content-line"></div><div class="content-line-2"></div><img src="' . $img_abs_path . '" style="width: 80%; max-width: 80%; height: auto; margin-top: 250px;" /></div>'; 
        } else {
            // Portrait or Square
            $html .= '<div style="page-break-after: always; text-align: center;"><div class="content-line"></div><div class="content-line-2"></div><img src="' . $img_abs_path . '" style="width: auto; height: 60%; max-height: 60%; margin-top: 180px;" /></div>';
        }
}
$html .= '</body></html>';