Search code examples
phpechodompdf

DOMPDF echo() not outputting text


I just upgraded to dompdf 6.1 and now when I do something like

<?php echo 'hello'; ?> 

inside my document, the 'hello' does not show up on the pdf. According to the documentation I should be allowed to use

<?php ?> 

in my html to render text... here is my html, when I render it all that shows is "Test" on the pdf document

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
Test <?php echo 'Hello'; ?>
</body>
</html>

Any ideas? I am not sure what I am doing wrong.

Thank you

EDIT: For people googling for this...

I now use the method of capturing the output buffer. I should have updated this question long ago. Now, instead of using file_get_contents("myFile.php") I do this:

ob_start();
require_once("myFile.php");
$dompdf = new DOMPDF();
$myHtmlInput = ob_get_clean();
$dompdf->load_html($myHtmlInput);

Using this method will resolve any issues I outline above, though I think after 4 years, most people will have figured this out already.


Solution

  • Try using php code to generate your HTML (so HTML inside PHP not the other way around) See if it works

    <?php
    require_once("dompdf_config.inc.php");
    
    $string  = 'hello';
    
    $html =
      '<html><body>'.
      '<p>Test '.$string.'</p>'.
      '</body></html>';
    
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $dompdf->stream("sample.pdf");
    
    ?>