PHP Count Number of Pages in PDF File

By Hardik Savani May 14, 2024 Category : PHP

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

Sometime, we just need to count how many pages are there in pdf file and show it for our admin pane or front-end. So i will give you very simple example of getting calculate 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 a one argument as path of pdf file. so just see bellow example for your solution.

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



$path = 'pdf/itsolutionstuff_file.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 you...

Tags :