PHP Count Number of Pages in PDF File


In this post, I will give you a simple example of how to get a number of pages in pdf file using PHP. If you need to get a number of pages in pdf using PHP then I will help you PHP count number of pages in pdf.

Sometimes, we just need to count how many pages are there in the pdf file and show it for our admin pane or front-end. So i will give you very simple example of getting the calculate a number of pages in pdf file using core PHP. you can also use in PHP, laravel, Codeigniter framework too.

In this example, we will create countPages() and it will take one argument as a path of a pdf file. so just see bellow example for your solution.

Make Sure you have itsolutionstuff_file.pdf file in your pdf folder.


$path = 'pdf/your_pdf.pdf';
$totoalPages = countPages($path);
echo $totoalPages;
function countPages($path) {
  $pdftext = file_get_contents($path);
  $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
  return $num;



i hope it can help a lot...