1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # pkpgcounter : a generic Page Description Language parser |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation; either version 2 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | |
---|
24 | """This modules implements a page counter for PDF documents.""" |
---|
25 | |
---|
26 | import sys |
---|
27 | import re |
---|
28 | |
---|
29 | import pdlparser |
---|
30 | |
---|
31 | class PDFObject : |
---|
32 | """A class for PDF objects.""" |
---|
33 | def __init__(self, major, minor, description) : |
---|
34 | """Initialize the PDF object.""" |
---|
35 | self.major = major |
---|
36 | self.minor = minor |
---|
37 | self.description = description |
---|
38 | self.comments = [] |
---|
39 | self.content = [] |
---|
40 | self.parent = None |
---|
41 | self.kids = [] |
---|
42 | |
---|
43 | class Parser(pdlparser.PDLParser) : |
---|
44 | """A parser for PDF documents.""" |
---|
45 | def isValid(self) : |
---|
46 | """Returns 1 if data is PDF, else 0.""" |
---|
47 | if self.firstblock.startswith("%PDF-") or \ |
---|
48 | self.firstblock.startswith("\033%-12345X%PDF-") or \ |
---|
49 | ((self.firstblock[:128].find("\033%-12345X") != -1) and (self.firstblock.upper().find("LANGUAGE=PDF") != -1)) or \ |
---|
50 | (self.firstblock.find("%PDF-") != -1) : |
---|
51 | self.logdebug("DEBUG: Input file is in the PDF format.") |
---|
52 | return 1 |
---|
53 | else : |
---|
54 | return 0 |
---|
55 | |
---|
56 | def getJobSize(self) : |
---|
57 | """Counts pages in a PDF document.""" |
---|
58 | # First we start with a generic PDF parser. |
---|
59 | lastcomment = None |
---|
60 | objects = {} |
---|
61 | inobject = 0 |
---|
62 | objre = re.compile(r"\s?(\d+)\s+(\d+)\s+obj[<\s/]?") |
---|
63 | for fullline in self.infile.xreadlines() : |
---|
64 | parts = [ l.strip() for l in fullline.splitlines() ] |
---|
65 | for line in parts : |
---|
66 | if line.startswith("% ") : |
---|
67 | if inobject : |
---|
68 | obj.comments.append(line) |
---|
69 | else : |
---|
70 | lastcomment = line[2:] |
---|
71 | else : |
---|
72 | # New object begins here |
---|
73 | result = objre.search(line) |
---|
74 | if result is not None : |
---|
75 | (major, minor) = [int(num) for num in line[result.start():result.end()].split()[:2]] |
---|
76 | obj = PDFObject(major, minor, lastcomment) |
---|
77 | obj.content.append(line[result.end():]) |
---|
78 | inobject = 1 |
---|
79 | elif line.startswith("endobj") \ |
---|
80 | or line.startswith(">> endobj") \ |
---|
81 | or line.startswith(">>endobj") : |
---|
82 | # Handle previous object, if any |
---|
83 | if inobject : |
---|
84 | # only overwrite older versions of this object |
---|
85 | # same minor seems to be possible, so the latest one |
---|
86 | # found in the file will be the one we keep. |
---|
87 | # if we want the first one, just use > instead of >= |
---|
88 | oldobject = objects.setdefault(major, obj) |
---|
89 | if minor >= oldobject.minor : |
---|
90 | objects[major] = obj |
---|
91 | inobject = 0 |
---|
92 | else : |
---|
93 | if inobject : |
---|
94 | obj.content.append(line) |
---|
95 | |
---|
96 | # Now we check each PDF object we've just created. |
---|
97 | # colorregexp = re.compile(r"(/ColorSpace) ?(/DeviceRGB|/DeviceCMYK)[/ \t\r\n]", re.I) |
---|
98 | newpageregexp = re.compile(r"(/Type)\s?(/Page)[/\s]", re.I) |
---|
99 | pagecount = 0 |
---|
100 | for obj in objects.values() : |
---|
101 | content = "".join(obj.content) |
---|
102 | count = len(newpageregexp.findall(content)) |
---|
103 | pagecount += count |
---|
104 | return pagecount |
---|
105 | |
---|
106 | def test() : |
---|
107 | """Test function.""" |
---|
108 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
109 | sys.argv.append("-") |
---|
110 | totalsize = 0 |
---|
111 | for arg in sys.argv[1:] : |
---|
112 | if arg == "-" : |
---|
113 | infile = sys.stdin |
---|
114 | mustclose = 0 |
---|
115 | else : |
---|
116 | infile = open(arg, "rb") |
---|
117 | mustclose = 1 |
---|
118 | try : |
---|
119 | parser = Parser(infile, debug=1) |
---|
120 | totalsize += parser.getJobSize() |
---|
121 | except pdlparser.PDLParserError, msg : |
---|
122 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
123 | sys.stderr.flush() |
---|
124 | if mustclose : |
---|
125 | infile.close() |
---|
126 | print "%s" % totalsize |
---|
127 | |
---|
128 | if __name__ == "__main__" : |
---|
129 | test() |
---|