Use the getNumPages function instead:
$total = $pdf->getNumPages();
Note that this counts the amount of pages that have been created so far using the Addpage() function. So if you want the total amount of pages declare it after your last use of Addpage()