how to get number of pages in a PDF file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get number of pages in a PDF file
# 1  
Old 03-04-2008
how to get number of pages in a PDF file

Hello,

Can anyone please help me providing script to get the number of pages in a PDF file?


TIA
Prvn
# 2  
Old 03-04-2008
Writing a script will not do it. Depending on the software that created the pdf and whether or not it is encrypted is way beyond shell, and probably something a casual C coder would want to try.

Your best bet is to find some software like pdf2txt. Then run it from the command line, and grep for ^L - ascii 12 - which is a page feed. There is probably something like pdf2txt in the open source area, ie., free. Try connecting to SourceForge.net: Welcome to SourceForge.net, use the search engine there, and find pdf conversion software.

Someone else here may know of a free product that is good. I don't.
# 3  
Old 03-08-2008
You might also try using pdf2ps if you have ghostscript, and then using:
"grep -c showpage" on the output file to count the number of pages.
# 4  
Old 03-08-2008
You could also try the pdftk app pdftk - the pdf toolkit

To get the number of pages:
pdftk file.pdf dump_data output | grep -i Num
NumberOfPages: 1
# 5  
Old 07-26-2008
The following is a short shell script which I hacked together based on the PDF Reference V1.7 which seems to do the job across a wide spectrum of PDF document types.

Code:
#!/bin/ksh93
#
#  USAGE: pdfcount file.pdf
#

[[ "$#" != "1" ]] && {
   print "ERROR: No file specified"
   exit 1
}

numpages=0

strings $1 | grep "/Count" |
while read line
do
   num=${line/*([[:print:]])+(Count )?(-)+({1,4}(\d))*([[:print:]])/\4}
   (( num > numpages)) && numpages=$num
done

print $numpages

exit 0

Note this shell script is written for ksh93, not pdksh or ksh88. It can easily be ported to other shells but that I will leave as an exercise to the reader.
# 6  
Old 04-18-2009
Hello

i´m interestet in your php code for pdf page nummber count.

br ulibo
# 7  
Old 04-20-2009
if you are familiar with Perl, check the PDF CPAN module

something like:
Code:
#!/usr/bin/perl
# get_pages_in_pdf_file.pl

use PDF;
my $filename = shift;

my $this_pdf = PDF->new;
$this_pdf = PDF->new($filename);

print "Has ", $this_pdf->Pages, " Pages \n";

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Page number with total number of pages, EG Page 1 of 5

So I've worked how to add page numbers based on regex. It's using the footer text. How do we get the total amount added so we have page number with the total number of pages? Desired output: Page No:1 of 5 Thanks in advance. (15 Replies)
Discussion started by: tugar
15 Replies

2. Shell Programming and Scripting

Converting secured pdf files to pdf using acroread

Does anybody have idea of Converting secured pdf files to pdf using acroread ? ---------- Post updated at 04:49 PM ---------- Previous update was at 04:44 PM ---------- This file is not password protected. (4 Replies)
Discussion started by: Soham
4 Replies

3. Shell Programming and Scripting

PDF Script to extract PDF Links MOD in Need

In here we have a script to extract all pdf links from a single page.. any idea's in how make this read instead of a page a list of pages.. and extract all pdf links ? #!/bin/bash # NAME: pdflinkextractor # AUTHOR: Glutanimate (http://askubuntu.com/users/81372/), 2013 #... (1 Reply)
Discussion started by: danielldf
1 Replies

4. UNIX for Dummies Questions & Answers

Gs to split a pdf into multiple pages

Hello, Some googling and checking the man pages told me it should be possible to split a pdf (or ps) file into individual pages : man gs You might want to print each page separately. To do this, send the output to a series of files "foo1.xyz, foo2.xyz, ..." using the "-sOut-... (3 Replies)
Discussion started by: jossojjos
3 Replies

5. Shell Programming and Scripting

Need a REGEX to increment the file number of a pdf file

Hello, I have a few thousand .pdf files in various folders each have a naming scheme like this: 006_-_Titled_Document_#34_-_September-25-2011-side-1.pdf In each folder, the number system starts at 001 (as you see on the far left of the file name), and then ends at 999 (maximum .pdf files).... (4 Replies)
Discussion started by: Marcus Aurelius
4 Replies

6. Shell Programming and Scripting

Store and isolate bad pages from a file to new file

I have a file like below . The good pages must have 3 conditions : The pages that containing page total only must have 50 lines. The pages that containing customer total only must have 53 lines. The last page of Customer Total should be the last page. How can I accomplish separating good... (1 Reply)
Discussion started by: ehabaziz2001
1 Replies

7. UNIX for Advanced & Expert Users

creating pdf file with four pages

I have some code in fortran90, example stored in scode.f90 and I want to create a pdf containing the code. I would like to have four pages of code put into each page in the pdf. I was thinking of creating a ps file using mpage and then using ps2pdf after. However, I noticed that ps2pdf shift the... (4 Replies)
Discussion started by: kristinu
4 Replies

8. Shell Programming and Scripting

Perl - Convert html to pdf - PDF::FromHTML

Hi, I am trying to convert html to pdf using perl module PDF::FromHTML, am getting the error as given below. not well-formed (invalid token) at line 2, column 17, byte 56 at C:/Perl/lib/XML/Parser.pm line 187 at C:/Perl/site/lib/PDF/FromHTML.pm line 140 The perl code is as given... (2 Replies)
Discussion started by: DILEEP410
2 Replies

9. Shell Programming and Scripting

[Example] View man pages as pdf

This is a simple example of a shell script. I made it because it's sometimes convenient to search through a manpage and to have access to the terminal while you're reading (maybe to test code). It also serves as a basic example for anyone learning shell scripting. #!/bin/sh nm="/tmp/$1.pdf"... (0 Replies)
Discussion started by: CRGreathouse
0 Replies
Login or Register to Ask a Question