How to Count The Number of Pages in a PDF file

Count The Number of Pages in a PDF

In this tutorial, you will learn How to Count The Number of Pages in a PDF file. If you need to count the page in the PDF file before performing some tasks in the web application, then we have given the easy code below. We can count the number available page in the PDF file easily. Following the tutorial guide, you step by step to learn it and get the number of pages to count in the PDF file.

Sometimes we need to monitor PDF files in the project without opening it like how many pages the PDF file has. Let’s take an example to suppose you need to perform some action in the web application but the condition is, you only can perform some type of action if the user profile has the option to upload a resume in PDF and the number of pages in it should be more than 10. In that case scenario, this tutorial will help to get the number of pages in the code without opening the file.

In an ordinary way if you have to count the number of pages available in the PDF you have to open the PDF file and check how many pages are there in the PDF file. The same thing can be done by using the PHP function which we have explained below. You only need to put the right path of the document in the function and when you run the function it will read the code and count how many pages are there in the file. You can use it in the live web application in many cases as per requirement.

I will you get to number of pages in PDF. You can simply use this function in PHP. follow bellow example.

<?php

function count_pages($pdfname) {

  $pdftext = file_get_contents($pdfname);
  $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);

  return $num;
}

$pdfname = 'pdf/dummy.pdf';
$pages = count_pages($pdfname);

echo $pages;

?>

Output

10

Conclusion:

Checking the number of the page in the PDF file is very easy and we have tried to explain how you can achieve the same without opening the file in the project. Hope you have lived the post, if you have any queries related to this post please comment below we would love to help you. You can also check out other articles like How to Partially hide or mask email addresses in PHP

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *