Search code examples
phpdompdfhtml-to-pdf

inline php script variables not working in DOMPDF


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

Solution

  • 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);
    ?>