Search code examples
phppdfdompdf

Unable to downlaod pdf using DOMPDF


I checked plenty of resources, but my issue could not be resolved. I tried to generate to PDF by including PHP File. But now I stuck in "Unable to stream pdf: headers already sent" error. I also compress my code and also remove white spaces. Here is my code.

<?php
 //ob_start();
// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$return = include 'download_pdf.php';
$return = stripslashes($return);
$dompdf->loadHtml($return);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
//$dompdf->stream();

// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>0));
?>

Solution

  • Try to using output buffering

    replace

    $return = include 'download_pdf.php';
    

    with

    ob_start();
    include 'download_pdf.php';
    $return  = ob_get_contents();
    ob_end_clean();