473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
3 23956
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
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
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

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

Similar topics

4
by: Andreas Neudecker | last post by:
Hi. I know you can read the filename of a program as sys.argv. But what about modules? Is there a similar way to find out the file name of a module (called by some other module or program) from...
0
by: genc ymeri | last post by:
Hi over there, Does the .Net20 framework provide library classes/drivers to convert multiple single tiff files into a single multi-pages file or should I use third party tools ? Genc.
1
by: piseth | last post by:
Hi. I have one problem and don't know how to do. I want to write a file doc into table by using sqlplus so can you help me. best regard.
8
by: Filippo1980 | last post by:
Hi, all, I have a question for you, I must to do an application that create a file .doc from some fields that I have on a Oracle DB. I did some search and I find only 2 choise: 1) I can use POI...
0
by: JM | last post by:
Hi, I have uploaded an application written in asp.net 2.0 on server. By web controls started giving Javascript error and I figured it out from discussion groups that I have to put WebForms.js...
2
by: Kururu | last post by:
Hi Does anyone know how to make a form application which can get no. of file in the directory and size??? Thousand Thanks Kururu
3
by: sunmat | last post by:
To find number of character without space using java Example: String = prabu sun No. of char =8 plz send me code
2
by: bling via DotNetMonster.com | last post by:
greetings fellow programmers!! im kinda new in vb.net and i need your professional help! im using DSOframer to open .doc files but i disabled the menubar and toolbar because i just want to show...
3
by: Praveen Raj V | last post by:
"socket.Available" is not working. How would you Determine number of bytes present in a TCP/IP socket before reading those bytes without using socket.Available void Receive(Socket socket, byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.