Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$PAGE_NUM variable is always the same. #3382

Open
Tadaz opened this issue Jan 24, 2024 · 3 comments
Open

$PAGE_NUM variable is always the same. #3382

Tadaz opened this issue Jan 24, 2024 · 3 comments
Labels

Comments

@Tadaz
Copy link

Tadaz commented Jan 24, 2024

I tried to calculate the X distance for my pagination based on the $PAGE_NUM and $PAGE_COUNT variables. Apparently, for every page, $PAGE_NUM will return the last page number. Basically, $PAGE_NUM is always equal to $PAGE_COUNT. Looks like a bug to me.

Calculations snippet:

$pns = strlen($PAGE_NUM) - 1;
$pcs = strlen($PAGE_COUNT) - 1;
$x = 517 - ($pns - pcs) * 6;
$pdf->page_text($x, $y, "Page {PAGE_NUM} of {PAGE_COUNT}", $font, $size, [0, 0, 0]);

You can use this snippet to see that the $PAGE_NUM variable is always the same.

$pdf->page_text($x, $y, "Page " . $PAGE_NUM . " of {PAGE_COUNT}", $font, $size, [0, 0, 0]);
@bsweeney
Copy link
Member

bsweeney commented Jan 25, 2024

Not a bug. You're using the $PAGE_NUM variable to calculate the width. Presumably you're running this logic at the end of the document so, yes, the calculated value of $x will be the same for all pages. It would help to see more context.

@Tadaz
Copy link
Author

Tadaz commented Jan 25, 2024

The whole blade file. Changing the script's location just after the opening tag does not help. By the way, I am using v.2.0.0.

<!DOCTYPE html>
<html lang="en-gb">
<head>
   ADDING SOME RAW CSS HERE
</head>

<body>
<div class="header">
    <div class="logo">
        <img src="{{ asset('images/logo.png') }}" width="50%">
    </div>
</div>

<div class="two-columns>
    <div class="service">{{ $serviceName }}</div>
    <div class="date">{{ $date }}</div>
</div>

<div class="title">
    {{ $title }}
</div>

<div class="author">
    {{ $authorName }}
</div>

<div class="content">
    {!! $content !!}
</div>

<script type="text/php">
      if (isset($pdf)) {
        $h = $pdf->get_height();

        $size = 9;
        $font = $fontMetrics->getFont("Roboto");
        $textHeight = $fontMetrics->getFontHeight($font, $size);
        $y = $h - $textHeight - 8;

        $pns = strlen($PAGE_NUM) - 1;
        $pcs = strlen($PAGE_COUNT) - 1;
        $x = 517 - ($pns - pcs) * 6;
        $pdf->page_text($x, $y, "Page {PAGE_NUM} of {PAGE_COUNT}", $font, $size, [0, 0, 0]);
      }
</script>
</body>
</html>

@bsweeney
Copy link
Member

bsweeney commented Jan 25, 2024

Because you want to re-calculate the position on each page a page script would be more appropriate.

I'd recommend not using embedded script if you can. Rewriting what you currently have, the following can be used on the back end:

$canvas = $dompdf->getCanvas();
$canvas->page_script(function ($pageNumber, $pageCount, $canvas, $fontMetrics) {
    $h = $canvas->get_height();

    $size = 9;
    $font = $fontMetrics->getFont("Roboto");
    $textHeight = $fontMetrics->getFontHeight($font, $size);
    $y = $h - $textHeight - 8;

    $pns = strlen($PAGE_NUM) - 1;
    $pcs = strlen($PAGE_COUNT) - 1;
    $x = 517 - ($pns - pcs) * 6;
    $canvas->text($x, $y, "Page $pageNumber of $pageCount", $font, $size, [0, 0, 0]);
});

But if you need to use embedded script, here's the equivalent for that implementation:

<script type="text/php">
    if (isset($pdf)) {
        $pdf->page_script(function ($pageNumber, $pageCount, $canvas, $fontMetrics) {
            $h = $canvas->get_height();
        
            $size = 9;
            $font = $fontMetrics->getFont("Roboto");
            $textHeight = $fontMetrics->getFontHeight($font, $size);
            $y = $h - $textHeight - 8;
        
            $pns = strlen($PAGE_NUM) - 1;
            $pcs = strlen($PAGE_COUNT) - 1;
            $x = 517 - ($pns - pcs) * 6;
            $pdf->text($x, $y, "Page $pageNumber of $pageCount", $font, $size, [0, 0, 0]);
        });
    }
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants