Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

linux operating commands and unix operating commands

how to get number of pages in a PDF file

Shell Programming and Scripting


Tags
count, count pages, ksh, page count, pdf, script

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-04-2008   -   Original Discussion by prvnrk
prvnrk's Unix or Linux Image
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 24 March 2017, 10:06 PM EDT
Posts: 216
Thanks: 10
Thanked 7 Times in 7 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 03-04-2008   -   Original Discussion by prvnrk
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 7 January 2018, 1:26 PM EST
Location: NM
Posts: 11,277
Thanks: 581
Thanked 1,123 Times in 1,035 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 03-08-2008   -   Original Discussion by prvnrk
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 7 January 2018, 9:25 AM EST
Location: 44.21.48N 80.50.15W
Posts: 2,021
Thanks: 2
Thanked 277 Times in 259 Posts
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 Unix and Linux 03-08-2008   -   Original Discussion by prvnrk
in2nix4life's Unix or Linux Image
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 9 October 2016, 12:41 PM EDT
Location: East Coast
Posts: 621
Thanks: 1
Thanked 177 Times in 163 Posts
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
Sponsored Links
    #5  
Old Unix and Linux 07-26-2008   -   Original Discussion by prvnrk
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline
Registered User
 
Join Date: Dec 2003
Last Activity: 12 June 2016, 11:03 PM EDT
Location: /dev/ph
Posts: 4,996
Thanks: 73
Thanked 475 Times in 437 Posts
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.
Sponsored Links
    #6  
Old Unix and Linux 04-18-2009   -   Original Discussion by prvnrk
ulibo's Unix or Linux Image
ulibo ulibo is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 21 April 2009, 4:16 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hello

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

br ulibo
Sponsored Links
    #7  
Old Unix and Linux 04-20-2009   -   Original Discussion by prvnrk
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 20 December 2017, 6:02 AM EST
Location: Rossem, Tazenda
Posts: 1,213
Thanks: 7
Thanked 18 Times in 17 Posts
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";

Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
file separated into paragraphs or pages dodasajan Shell Programming and Scripting 3 11-24-2011 04:16 AM
creating pdf file with four pages kristinu UNIX for Advanced & Expert Users 4 09-29-2011 10:48 PM
Finding what pages link to a specific file iansocool Shell Programming and Scripting 2 08-26-2008 09:31 AM
Split text file by pages ranri UNIX for Dummies Questions & Answers 2 06-01-2001 03:43 AM



All times are GMT -4. The time now is 04:51 PM.