'How to get total pages of PDF with FPDF?

I've tried with AliasNbPage() to get total pages of pdf. i do called '{nb}' to get total pages and it doesn't give output numbers instead of {nb}. there's another way to get total of pages ?

    $tot = strval(explode('/', strval($this->pdf->PageNo().'/{nb}'))[1]);
    $this->pdf->SetX(44);
    $this->pdf->Cell(40,5,": ".$tot.' Pages',0,1);
    //while i'm tried to convert the number to the text, it doesn't show.
    $this->pdf->Cell(40,5,": ".numb_to_text(intval($tot)).' Pages',0,1);
function init_number($nilai) {
            $nilai = abs($nilai);
            $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
            $temp = "";
            if ($nilai < 12) {
                $temp = " ". $huruf[$nilai];
            } else if ($nilai <20) {
                $temp = $this->init_number($nilai - 10). " belas";
            } else if ($nilai < 100) {
                $temp = $this->init_number($nilai/10)." puluh". $this->init_number($nilai % 10);
            } else if ($nilai < 200) {
                $temp = " seratus" . $this->init_number($nilai - 100);
            } else if ($nilai < 1000) {
                $temp = $this->init_number($nilai/100) . " ratus" . $this->init_number($nilai % 100);
            } else if ($nilai < 2000) {
                $temp = " seribu" . $this->init_number($nilai - 1000);
            } else if ($nilai < 1000000) {
                $temp = $this->init_number($nilai/1000) . " ribu" . $this->init_number($nilai % 1000);
            } else if ($nilai < 1000000000) {
                $temp = $this->init_number($nilai/1000000) . " juta" . $this->init_number($nilai % 1000000);
            } else if ($nilai < 1000000000000) {
                $temp = $this->init_number($nilai/1000000000) . " milyar" . $this->init_number(fmod($nilai,1000000000));
            } else if ($nilai < 1000000000000000) {
                $temp = $this->init_number($nilai/1000000000000) . " trilyun" . $this->init_number(fmod($nilai,1000000000000));
            }     
            return $temp;
        }

    function numb_to_text($nilai) {
        $nilai = (int) $nilai;
        if($nilai<0) {
            $hasil = "minus ". trim($this->init_number($nilai));
        } else {
            $hasil = trim($this->init_number($nilai));
        }           
        return $hasil;
    }


Solution 1:[1]

The total number of pages can only be known just before the document is finished. For example:

$pdf = new FPDF();
$pdf->AddPage();
$pdf->AddPage();
$nb = $pdf->PageNo();
$pdf->Output();

$nb contains 2.

Solution 2:[2]

    // Get the file into path 
    $path = 'LargePDF.pdf';
    // Call a the function FunctionCountPages 
    $totalPageCount=  FunctionCountPages($path);;
    echo $totalPages;
    function FunctionCountPages($path) 
          {
              $pdftextfile = file_get_contents($path);
              $pagenumber = preg_match_all("/\/Page\W/", $pdftextfile, $dummy);
              return $pagenumber;
            }
//I hope this work for you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Olivier
Solution 2 Kester George