Roofus McDuffle
Roofus McDuffle

Reputation: 105

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.

Upvotes: 4

Views: 2932

Answers (2)

BrianS
BrianS

Reputation: 13914

In versions of dompdf prior to 0.6.1 the document fed to dompdf would be pre-processed using PHP's eval() function when DOMPDF_ENABLE_PHP was set to true. For this reason, in previous versions, you were able to echo some content into the HTML. While this is convenient in some situations for generating documents on-the-fly it also exposes a security vulnerability.

With dompdf 0.6.1 PHP pre-processing was removed from dompdf. The documentation needs to be updated to reflect this change. You should use the technique outlined by Sam Battat.

(Provide us with morel detail about your work flow if you need additional suggestions.)

Upvotes: 2

Sam Battat
Sam Battat

Reputation: 5745

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");

?>

Upvotes: 4

Related Questions