lilo's avatar
Level 2

How to detect the last page of pdf

Hi,

I generate a pdf file in my controller. And for each page(except the first page), a header a footer is displaying. At the bottom of the page, I print the page number. I want to remove footer from the last page of pdf so I tried something like this:

            $output .= '
                <html>
                    <head>
                      <style>
                         .pagenum:before { content: "Page " counter(page); }
                      </style>
                    </head>
                    <body>
            
                    <div id="content">

                       ... my content here...

                    </div>  
		//Using this script I print my page number at the end of each page
                    <script type="text/php">
                        if (isset($pdf)) {
                            $text = "page {PAGE_NUM} / {PAGE_COUNT}";
                            $size = 10;
                            $font = $fontMetrics->getFont("Verdana");
                            $width = $fontMetrics->get_text_width($text, $font, $size) / 2;
                            $x = ($pdf->get_width() - $width) / 2;
                            $y = $pdf->get_height() - 35;
                            $pdf->page_text($x, $y, $text, $font, $size);
                        }
                    </script>
                    </body>   
                </html>
            ';
            $first=false;
        }

    }

//Doing this, I want to display the footer on the pages except the last one
if(PAGE_NUM == PAGE_COUNT){
    $output .= '<div id="footer">       
                       
                      </div>';
}
    return $output;

But When I do like this I get this error:

ErrorException Use of undefined constant PAGE_NUM - assumed 'PAGE_NUM' (this will throw an Error in a future version of PHP)

How can I fix this? Hope it is clear. Thanks in advance.

0 likes
7 replies
lilo's avatar
Level 2

@forrestedw nope :( I don't understand why I cannot access PAGE_NUM and PAGE_COUNT variables

forrestedw's avatar

Share the code where PAGE_NUM and PAGE_COUNT get set

lilo's avatar
Level 2

@forrestedw While doing this, I used a tutorial on the internet.( https://github.com/barryvdh/laravel-dompdf/issues/452 ) I firstly set this option:

$pdf->getDomPDF()->set_option("enable_php", true);

Then put the script inside body tag

$output .= '
		....
		<script type="text/php">
                    if (isset($pdf)) {
                        $text = "page {PAGE_NUM} / {PAGE_COUNT}";
                        $size = 10;
                        $font = $fontMetrics->getFont("Verdana");
                        $width = $fontMetrics->get_text_width($text, $font, $size) / 2;
                        $x = ($pdf->get_width() - $width) / 2;
                        $y = $pdf->get_height() - 35;
                        $pdf->page_text($x, $y, $text, $font, $size);
                    }
                </script>';

With the help of this I displayed page number at the end of each page.And I tried to use these page numbers in if condition. I don't know if this is a good option but I tried

RodrigoG's avatar

Hello, I am in a similar problem, I would like to store in a variable the total number of pages of the domPDF document to, for example, use it in a condition outside the script. How did you solve it in the end, thanks!

Tray2's avatar

@RodrigoG Please open your own thread instead of highjacking an old one. It increases the chance that someone can help you.

1 like

Please or to participate in this conversation.