0

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));
?>
Kshitij Soni
  • 394
  • 3
  • 17
  • `$return = include 'download_pdf.php';` this does not load information into `$return` unless you have a `return` in that php file, you are probably echoing out something there which will cause the exact error you are saying you have. – cmorrissey Mar 06 '17 at 18:41
  • Yess I am not retuning any variable.. I am just create html to one php file and just include that php file..So please suggest me any suggestions to overcome this problem – Kshitij Soni Mar 06 '17 at 18:44
  • One more thing is when I echo the return variable I can see whole html in the browser but facing error "headers already sent" – Kshitij Soni Mar 06 '17 at 18:46

1 Answers1

0

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();
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • I try this .But it gives me below error. Fatal error: Uncaught Dompdf\Exception: The row #1 could not be found, please file an issue in the tracker with the HTML code in D:\xampp\htdocs\convert_html_to_pdf_using_php\dompdf\src\Cellmap.php:417 Stack trace: #0 – Kshitij Soni Mar 06 '17 at 18:56
  • @KshitijSoni it looks like this answer solves your original issue, you should go ahead and accept it. The original issue was that content was being sent to the browser (probably from your include) and using output buffering works around that by capturing output before it is sent to the browser. – BrianS Mar 06 '17 at 23:25
  • @KshitijSoni as for your comment, it sounds like you have a new issue related to the structure of your document. You should start a new question for the new problem. – BrianS Mar 06 '17 at 23:25