Does this help?
Jan 11, 2021
7
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.
Please or to participate in this conversation.