Re: Question regarding $PAGE_NUM and $PAGE_COUNT, header and footer objects vs. $canvas->page_text()

1,780 views
Skip to first unread message

BrianS

unread,
Mar 11, 2013, 11:17:40 AM3/11/13
to dom...@googlegroups.com
$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

Christian Kirchhoff

unread,
Mar 11, 2013, 11:35:34 AM3/11/13
to dom...@googlegroups.com
Thanks very much for the fast response, Brian.

I guess I took things for granted, as I was working on and extending code that a co-worker prepared. The calls to page_text where in the code and I didn't question them. Thanks for pointing that out.

Best regards

Christian

Christian Kirchhoff

unread,
Mar 11, 2013, 11:42:24 AM3/11/13
to dom...@googlegroups.com
Just to confirm: If I use $pdf->open_object() and $pdf->close_object() and add the header/footer as an object, should I still use page_text()? My finding is that when using page_text it is direktly writte to the page(s), and not the the object that was opened with open_:object().

Best


Christian

Am Montag, 11. März 2013 16:17:40 UTC+1 schrieb BrianS:

BrianS

unread,
Mar 11, 2013, 3:55:35 PM3/11/13
to dom...@googlegroups.com
Unfortunately it doesn't work that way. page_text() works outside the normal PDF creation flow, it's added to a stack that is parsed for every page after the entire document has been rendered (actually, as the PDF output is generated). To add text to an object you have to use the text() method. This is problematic because it's difficult to generate "page X of Y" text using objects since the values are dependent on when the text() method is executed.

Based on your other post, it's probable that you'll want to use page_script(). As I said, this works like page_text() in that it's processed on every page after the entire document has been rendered, but it's a stack of scripts instead of text. You have access to all the CPDF methods when you push something into the page_script() stack as well as the page number and page count.

So if you want, for example, to only display a header starting on page 2 you could do something like this:

$pdf->page_script('
  if ($PAGE_NUM >= 2) {
    $text = $PAGE_NUM . "/" . $PAGE_COUNT;

    $y = $pdf->get_height() - 24;
    $x = ($pdf->get_width() - Font_Metrics::get_text_width($text, $font, $size))/2;
    $pdf->text($x, $y, $
text, $font, $size);
  }
');

(There's some missing variable instantiation there for $font and $size, but it was missing from your original post so I assume you've got that in your original code.)

Christian Kirchhoff

unread,
Mar 12, 2013, 5:18:21 AM3/12/13
to dom...@googlegroups.com
Thanks again, Brian. I will try that.
Reply all
Reply to author
Forward
0 new messages