0

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.

Rodrypaladin
  • 188
  • 5
  • 1
    Does this answer your question? [Get the number of pages in a PDF document](https://stackoverflow.com/questions/14644353/get-the-number-of-pages-in-a-pdf-document) – Nico Haase Nov 17 '21 at 15:27
  • I saw this thread. But the only solution that gives me I do not clarify with her that is using pa application pdfinfo, I have tried but all it does is that when I run the web, I open the installation of that exe. And what all the other people propose in the answers does not work 100% of the times. – Rodrypaladin Nov 17 '21 at 15:48

1 Answers1

0

You can use functionalities of FPDI for this task:

use setasign\Fpdi\PdfParser\StreamReader;
use setasign\Fpdi\PdfParser\PdfParser;
use setasign\Fpdi\PdfReader\PdfReader;

function getPageCountOfPdf($path) 
{
    $stream = StreamReader::createByFile($path);
    $parser = new PdfParser($stream);

    $pdfReader = new PdfReader($parser);

    return $pdfReader->getPageCount();
}
Jan Slabon
  • 4,456
  • 2
  • 11
  • 26