Question

I'm using the follwing TCPDF code to generate PDFs with the writeHTML function. I have the page number footer function which puts page numbers at the bottom of each page as the pdf total pages grows. I'm trying to find a way at the end of creating all the pages to determine how many total pages the produced document has so that i can store that information into a variable and submit that data to a DB.

I've tried:

$total = $pdf->getAliasNbPages();

but doesnt work, Any ideas?

Thanks

// PAGE NUMBERED FOOTER

class MYPDF extends TCPDF {

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
    $this->SetFont('Calibri', '', 8);
        // Page number

    $pageNumbers = 'Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages();

        $this->Cell(0, 10, $pageNumbers, 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }

}


$html = 'html content';

$pdf->writeHTML($html, true, false, true, false, '');

$pdf->Output('file.pdf', 'I');
Was it helpful?

Solution

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()

OTHER TIPS

$pdf->Cell(0, 10,'{:ptp:}', 0, false, 'C', 0, '', 0, false, 'T', 'M');

To get the total number of pages, use $this->getAliasNbPages() as chown in this example https://tcpdf.org/examples/example_003/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top