473,378 Members | 1,391 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,378 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 23945
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.