Search code examples
html2pdf

Total pages in PDF using html2pdf


I have some HTML using which I am printing a PDF, I would like to get the count of total pages the PDF will produce in a PHP Variable.

$html2pdf = new HTML2PDF('P', 'A4');
$html2pdf->writeHTML($html, false);
$html2pdf->Output('myfile.pdf');

I would like to do something like..

$totalpages = $html2pdf->getTotalPageCount(); //should return total pages the myfile.pdf      would produce.

Solution

  • I've used html2pdf myself and couldn't find such a feature either. However, you can use the well-known ImageMagick library after generating your PDF to retrieve the number of pages:

    $im = new Imagick();
    $im->pingImage('generated.pdf');
    echo $im->getNumberImages();
    

    Has helped me, hope you can use that too.