$PAGE_NUM and $PAGE_COUNT are parsed as encountered. So if you use them in this way then your page text will always represent the values of those variables on the page they were used. You're also confusing the two functions. $pdf->text() will add text to the current page, whereas $pdf->page_text() will add text to all pages
after the document has been rendered. Thus why you get different results. You can rewrite your first code block as follows:
$pageText = "{PAGE_NUM}/{PAGE_COUNT}";
$y = $pdf->get_height() - 24;
$x = ($pdf->get_width() - Font_Metrics::get_text_width($pageText, $font, $size))/2;
$pdf->page_text($x, $y, $pageText, $font, $size);
Or, you could continue doing as you have with your second code block. Or you could use page_script with your first code block.
$pdf->page_script('
$pageText = $PAGE_NUM . "/" . $PAGE_COUNT;
$y = $pdf->get_height() - 24;
$x = ($pdf->get_width() - Font_Metrics::get_text_width($pageText, $font, $size))/2;
$pdf->page_text($x, $y, $pageText, $font, $size);
');
Perhaps that last one is a bit confusing. It defines a script that is implemented in a way similar to page_text() in that it is applied to each page after the document has been rendered.
On Monday, March 11, 2013 7:33:05 AM UTC-4, Christian Kirchhoff wrote:
Hello,
I am using DOMPDF 0.6.0 beta 3.
When I use inline php within a script element, the variables $PAGE_NUM and $PAGE_COUNT are always set with their current values? I am asking this because my finding is that if I put the following code:
$pageText = $PAGE_NUM . "/" . $PAGE_COUNT;
$y = $pdf->get_height() - 24;
$x = ($pdf->get_width() - Font_Metrics::get_text_width($pageText, $font, $size))/2;
$pdf->text($x, $y, $pageText, $font, $size);
In an inline script at the beginning of the html output (right after the opening body tag), all pages show "1/1" in the footer.
If I add the inline script at the end, before the closing body tag, the footer text only appears on the last page, e.g. when I have a docukent that's rendered on three pages it says "3/3" on page 3.
Am I doing something wrong there?
If I first call $pdf->render() and then work on the canvas with:
$canvas = $dompdf->get_canvas();
$font = Font_Metrics::get_font("sans-serif", "normal");
$canvas->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
It works as expected.
Best regards,
Christian