PortyR
PortyR

Reputation: 385

Find the number of pages in a PDF using PHP

I have researched this for a few days now and finally found something that seemed to work, but I am getting the wrong result. I need to count the number of pages in a PDF file on a remote server. My code opens the PDF, but it's not finding the correct number of pages and I'm not sure why.

Here is my code so far:

    $CI = &get_instance();
    $CI->load->library('Awss3', null, 'S3');
    $CI->load->library('Pdflib');
    $data = $CI->S3->readFile('uploads/225572/filename.pdf', false, 'bucket-name');
    $needle = 'Page';
    $positions = array();
    $lastPos = 0;
    while (($lastPos = strpos($data, $needle, $lastPos))!==false) {
        $positions[] = $lastPos;
        $lastPos = $lastPos + strlen($needle);
    }
    echo count($positions);
    foreach ($positions as $value) {
        echo $value . '<br />';
    }
    $test = strpos($data, 'Page');

If I echo out the $data, I get lots of symbols, etc. and some words, but the $test comes out to 0 when it should be 16. Does it depend on the type of PDF or do I need to decode it or something like that?

Upvotes: 1

Views: 1205

Answers (1)

Abd Abughazaleh
Abd Abughazaleh

Reputation: 5495

Simplest of all is using ImageMagick here is a sample code

 $image = new Imagick();
 $image->pingImage('myPdfFile.pdf');
 echo $image->getNumberImages();

otherwise you can also use PDF libraries like MPDF or TCPDF for PHP

Upvotes: 1

Related Questions