PHP Count Number of Pages in PDF File

  342 views   2 months ago PHP

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.

Example

<?php
  
$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;
}

Output

125

i hope it can help a lot...

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles