I've been at it for over a month and I can't find a correct way to count the number of pages of PDF files. This code that I show you reads perfectly most of them, but there are some, about 4%, that count more pages than they contain, or count 0 pages.
Some of the pdfs that it does not read correctly I see that coincide in that the sheets are horizontal in that case it counts more pages than it has. In some vertical ones it also fails counting 0 pages.
This is the function that returns me the number of pages of the pdf file
function numeroPaginasPdf($archivoPDF)
{
$pdfname = $archivoPDF;
$pdftext = file_get_contents($pdfname);
$num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
return $num;
}
I'm open to do it with JS if someone knows how to do it, but the truth is that everything I've found to do it so far doesn't work correctly.