Dennis Madsen
Dennis Madsen

Reputation: 33

DOMPDF: Inline PHP script not working

I've a problem with DOMPDF. I'm running PHP 5.3.3 and have the newest DOMPDF 0.6.0 beta2.

I've created this HTML template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Printed document</title>
</head>
<body>

<script type="text/php">
echo "test";
if ( isset($pdf) ) {
    echo $PAGE_NUM;
}
</script>

</body>
</html>

And this file to render the PDF:

<?php
require_once("dompdf/dompdf_config.inc.php");

$templateFile = 'template.html';
$content = file_get_contents($templateFile); 
if ($content !== false) {
    $dompdf = new DOMPDF();
    $dompdf->load_html($content);
    $dompdf->render();
    $dompdf->stream("test.pdf");
}
?>

In the dompdf_config.inc.php I've set DOMPDF_ENABLE_PHP to true, but still either the test-string or the number of pages is displayed in my rendered PDF. Why?

I've compressed my small example here: http://uploads.dennismadsen.com/pdf.zip

Please tell me if you need further information.

Upvotes: 3

Views: 10620

Answers (2)

Dustin Butler
Dustin Butler

Reputation: 897

See the attachment named issue121.php on this bug report

http://code.google.com/p/dompdf/issues/detail?id=121

Worked for me to draw page 1 of x in the footer

Upvotes: 0

BrianS
BrianS

Reputation: 13914

You've answered your own question as to why the code does not work as expected. Luckily, there is a way to produce what you want in DOMPDF 0.6.0 as of beta 2. Try out the following code (no need for inline script):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title>Printed document</title>
  <style>
    .pagenum:before { content: counter(page); }
  </style>
</head>
<body>
  <p>You are currently reading page <span class="pagenum"></span>.</p>
</body>
</html>

As you can see DOMPDF now supports CSS-based counters, with a page counter implemented by default. Just use a style/element combination similar to the above.

Note: we haven't yet worked out a way to provide the total number of pages using this method.

Upvotes: 6

Related Questions