By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,557 Members | 1,088 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,557 IT Pros & Developers. It's quick & easy.

Find out number of pages in a file (Doc,TXT.PDF .etc)

P: 1
Hi All,

I have requirement to find out the number of pages in a document.
The document could be DOC , TXT ,PDF images etc.I want to be able to upload and find out number of pages in an uploaded document.

Can any expert guid me a way in PHP to accomblish this tak.

Help would be appreciated.!

thanks'
Sep 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: 1
You can find the number of pages in a PDF document by using the following code, using PHP4:

Expand|Select|Wrap|Line Numbers
  1.         //where $file is the full path to your PDF document.
  2.         if(file_exists($file)) {
  3.                         //open the file for reading
  4.             if($handle = @fopen($file, "rb")) {
  5.                 $count = 0;
  6.                 $i=0;
  7.                 while (!feof($handle)) {
  8.                     if($i > 0) {
  9.                         $contents .= fread($handle,8152);
  10.                     }
  11.                     else {
  12.                           $contents = fread($handle, 1000);
  13.                         //In some pdf files, there is an N tag containing the number of
  14.                         //of pages. This doesn't seem to be a result of the PDF version.
  15.                         //Saves reading the whole file.
  16.                         if(preg_match("/\/N\s+([0-9]+)/", $contents, $found)) {
  17.                             return $found[1];
  18.                         }
  19.                     }
  20.                     $i++;
  21.                 }
  22.                 fclose($handle);
  23.  
  24.                 //get all the trees with 'pages' and 'count'. the biggest number
  25.                 //is the total number of pages, if we couldn't find the /N switch above.                
  26.                 if(preg_match_all("/\/Type\s*\/Pages\s*.*\s*\/Count\s+([0-9]+)/", $contents, $capture, PREG_SET_ORDER)) {
  27.                     foreach($capture as $c) {
  28.                         if($c[1] > $count)
  29.                             $count = $c[1];
  30.                     }
  31.                     return $count;            
  32.                 }
  33.             }
  34.         }
  35.         return 0;
  36.  
  37.  
it has worked with all the PDF docs i've used it with so far, irrelevant of PDF version. I imagine a similar approach could be used for at least .DOC files though i haven't tested that out.
Nov 21 '07 #2

P: 9
for txt and probably doc you can use:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $filename = "file.php";
  3. $num = count(file($filename));
  4. echo $num; 
  5. ?>
  6.  
Nov 25 '07 #3

P: 1
Hi coffeemist,

I tested your code. It works some of the pdf files, but it doesn't work for the pdf files that contain all the images. It returns page count 0. I need to determine page count in user uploaded pdf files either in php or perl. Any help will be greatly appreciated.

Thank you very much!

Amy
Oct 8 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.