2

The inline php script is not executing when I generate the pdf ,only the html part is getting printed on pdf. I have already set $isPhpEnabled =True .

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

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$abc="This is the php text";
$html = <<<'ENDHTML'
<html>
<head>
<style>
h1{color:#555555;width:100%;border-bottom:2px solid powderblue;}
</style>
</head>
 <body>
  <h1 >Name : <script type="text/php"> echo $abc; </script></h1>
 </body>
</html>
ENDHTML;
$dompdf->loadHtml($html);

// (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("dompdf_out.pdf", array("Attachment" => false));
exit(0);
?>
Aman Rathore
  • 88
  • 1
  • 11
  • did you mean `$abc` is not printing? – Paritosh Mahale Oct 10 '17 at 12:26
  • @ParitoshMahale Yes. – Aman Rathore Oct 10 '17 at 12:36
  • @ParitoshMahale How can i execute scripts into the html code , as i want to generate dynamic content ? – Aman Rathore Oct 10 '17 at 13:59
  • somethig like this `$a = array('one','two','three','four','five'); $abc="This is the php text"; $html = " "; foreach($a as $value){ $html .= '

    '.$value.'

    '; } $html .=''; echo $html;`
    – Paritosh Mahale Oct 11 '17 at 06:15
  • Embedded (inline) script does not create document content. It's main purpose is to provide a means of accessing the PDF backend (CPDF, PDFLib) in the middle of the PDF rendering process. But we recommend you avoid using embedded script because it opens a potential security hole and you can do nearly the same using other methods. Based on your description you should just generate the HTML then feed the final document to Dompdf. – BrianS Oct 11 '17 at 22:55

2 Answers2

2

Try

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

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$abc="This is the php text";
$html = "
<html>
<head>
<style>
h1{color:#555555;width:100%;border-bottom:2px solid powderblue;}
</style>
</head>
 <body>
  <h1 >Name : $abc</h1>
 </body>
</html>
";
$dompdf->loadHtml($html);

// (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("dompdf_out.pdf", array("Attachment" => false));
exit(0);
?>
Paritosh Mahale
  • 1,238
  • 2
  • 14
  • 42
  • what should i use if I want to execute some script like a loop to enter many rows in table in the html code beacause the script tag is not working directly? @Paritosh Mahale – Aman Rathore Oct 10 '17 at 13:19
  • 1
    Now i am only able to use variables but not php scripts in the html string – Aman Rathore Oct 10 '17 at 13:35
1

The trick is simply to render your html as a string in php, concatenated with the values you wish to display in your PDF. You have to be careful with quotes, as there will be double quotes in your markup. So it helps to use single quotes as wrappers if you know what I mean.

So something like:

$html = '<div class = "some class" >' . $variable . '</div>';

You can then load this string into your $dompdf instance. $variable could be pulling it's value from anywhere, a database for example, or you could be iterating through a loop to build elements of your layout.

Sharkfin
  • 128
  • 1
  • 7