What are some common pitfalls when comparing PageNo() and AliasNbPages in FPDF?
When comparing PageNo() and AliasNbPages in FPDF, a common pitfall is not taking into account the fact that PageNo() starts counting from 1 while AliasNbPages includes the total number of pages. To accurately compare the current page number with the total number of pages, you need to subtract 1 from AliasNbPages before making the comparison.
$pdf = new FPDF();
$pdf->AddPage();
$totalPages = $pdf->AliasNbPages();
for ($i = 1; $i <= $totalPages; $i++) {
$pdf->AddPage();
$currentPage = $pdf->PageNo();
if ($currentPage == $totalPages - 1) {
$pdf->Cell(0, 10, 'Second to last page', 0, 1);
}
}
$pdf->Output();