In this post, I will try to give you a short and simple example “How to count number of pages pdf file in php”.

If you want to know how many number of pages are in pdf using php then it can help you:

Sometimes, we just need to get count of PDF file pages. So, I can give you very short and simple example to getting number of pages pdf file in php. You can also use this snippet in any type of php framework.

In this example, we will create just a simple function called ( getCountPDFPages($arg) ) which will return us calculated value of pdf by getting PDF file. We will pass file name in arg to make it generic.

So, just see bellow for example.

Caution: Ensure you’re giving proper file route in ( getCountPDFPages($arg) ).



    $path = 'file.pdf';

    $getTotalPages = getCountPDFPages($path);

    echo $getTotalPages;

    function getCountPDFPages($arg) {


            $pdf = file_get_contents($arg);

            $num = preg_match_all("/\/Page\W/", $pdf);

            //return actual pages from pdf file.
            return $num;


        //default it is 0; if not exist.
        return 0;



I hope now you clearly know “How to Count Number of Pages PDF File in PHP”. If it is perfectly getting right thing then you have to share this post with others by clicking on share button


Please enter your comment!
Please enter your name here