#!/usr/bin/perl -w #********************************************************************** # pagecount - Copyright (C) 2004 - Cameron Morland # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. #********************************************************************** # count the number of pages present in a set of postscript or pdf files. # note: my ($rev) = '$Revision: 1.4 $'; # store the CVS revision that we are $rev =~ s/[^0-9.]*//g; # remove non-numeric component use Getopt::Std; our($options); getopts("h", \%options); # read commandline switches @files = @ARGV; # remaining arguments are filenames. # print basic help if ($options{h}) { print </dev/null`)) && (!($pdf2ps = `which pdftops 2>/dev/null`))){ print STDERR "pdf2ps or pdftops is required for pdf files.\n"; $pdf2ps = 'false'; } chomp($pdf2ps); if ((!($ps2ps = `which ps2ps 2>/dev/null`)) && (!($ps2ps = `which pstops 2>/dev/null`))){ print STDERR "ps2ps or pstops is recommended for ps files.\n"; $ps2ps = ''; } chomp($ps2ps); # print "Using '$pdf2ps' and '$ps2ps'.\n"; foreach $file (@files) { $file =~ s/"/\\"/g; if ($file =~ m/\.pdf$/) { # it's a pdf, so translate it to postscript first. The resulting # file should be well-formed. # If no pdf translator, just ignore the files. open(FILE, "$pdf2ps \"$file\" - |") or die "Can't run $pdf2ps for pdf: $!\n"; } else { # it's a postscript file, but translate it to postscript again # anyway, to ensure the file is well-formed. if (! ($file =~ m/\.ps$/)) { print STDERR "Assuming '$file' is a postscript file.\n"; } if ($ps2ps) { # the filter is installed, so use it. open(FILE, "ps2ps \"$file\" - |") or die "Can't run $ps2ps for ps: $!\n"; } else { # no ps2ps filter, so just assume the file is well-formed open(FILE, $file) or die "Can't read $file: $!\n"; } } $found = 0; while() { # do the work; # seek through the file for information stating page count if (m/^%%Pages: ([0-9]+)/) { printf("% 7d %s\n", $1, $file); $sum += $1; $found = 1; last; } } # something bad happened, for example we tried to read a badly # formed postscript file without ps2ps if (!$found) { printf(" 0 %s\n", $file); print STDERR "Couldn't determine page count for '$file'; reporting zero.\n"; } close(FILE); } printf("% 7d total\n", $sum)