[193] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[191] | 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
[211] | 6 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
[191] | 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 |
---|
[211] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[191] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[193] | 23 | |
---|
| 24 | import sys |
---|
| 25 | import re |
---|
| 26 | |
---|
[235] | 27 | import pdlparser |
---|
[193] | 28 | |
---|
[240] | 29 | class PDFObject : |
---|
| 30 | """A class for PDF objects.""" |
---|
| 31 | def __init__(self, major, minor, description) : |
---|
| 32 | """Initialize the PDF object.""" |
---|
| 33 | self.major = major |
---|
| 34 | self.minor = minor |
---|
| 35 | self.description = description |
---|
| 36 | self.comments = [] |
---|
| 37 | self.content = [] |
---|
| 38 | self.parent = None |
---|
| 39 | self.kids = [] |
---|
| 40 | |
---|
[220] | 41 | class Parser(pdlparser.PDLParser) : |
---|
[193] | 42 | """A parser for PDF documents.""" |
---|
[220] | 43 | def isValid(self) : |
---|
| 44 | """Returns 1 if data is PDF, else 0.""" |
---|
| 45 | if self.firstblock.startswith("%PDF-") or \ |
---|
| 46 | self.firstblock.startswith("\033%-12345X%PDF-") or \ |
---|
| 47 | ((self.firstblock[:128].find("\033%-12345X") != -1) and (self.firstblock.upper().find("LANGUAGE=PDF") != -1)) or \ |
---|
| 48 | (self.firstblock.find("%PDF-") != -1) : |
---|
| 49 | if self.debug : |
---|
| 50 | sys.stderr.write("DEBUG: Input file is in the PDF format.\n") |
---|
| 51 | return 1 |
---|
| 52 | else : |
---|
| 53 | return 0 |
---|
| 54 | |
---|
[193] | 55 | def getJobSize(self) : |
---|
| 56 | """Counts pages in a PDF document.""" |
---|
[240] | 57 | # First we start with a generic PDF parser. |
---|
| 58 | lastcomment = None |
---|
| 59 | objects = {} |
---|
| 60 | while 1 : |
---|
| 61 | line = self.infile.readline() |
---|
| 62 | if not line : |
---|
| 63 | break |
---|
| 64 | # now workaround the unavailability of "Universal New Line" |
---|
| 65 | # under Python <2.3. |
---|
| 66 | line = line.strip().replace("\r\n", " ").replace("\r", " ") |
---|
| 67 | if line.startswith("% ") : |
---|
| 68 | lastcomment = line[2:] |
---|
| 69 | if line.endswith(" obj") : |
---|
| 70 | # New object begins here |
---|
| 71 | (n0, n1, dummy) = line.split() |
---|
| 72 | (major, minor) = map(int, (n0, n1)) |
---|
| 73 | obj = PDFObject(major, minor, lastcomment) |
---|
| 74 | while 1 : |
---|
| 75 | line = self.infile.readline() |
---|
| 76 | if not line : |
---|
| 77 | break |
---|
| 78 | line = line.strip() |
---|
| 79 | if line.startswith("% ") : |
---|
| 80 | obj.comments.append(line) |
---|
| 81 | elif line.startswith("endobj") : |
---|
| 82 | break |
---|
| 83 | else : |
---|
| 84 | obj.content.append(line) |
---|
| 85 | try : |
---|
| 86 | # try to find a different version of this object |
---|
| 87 | oldobject = objects[major] |
---|
| 88 | except KeyError : |
---|
| 89 | # not found, so we add it |
---|
| 90 | objects[major] = obj |
---|
| 91 | else : |
---|
| 92 | # only overwrite older versions of this object |
---|
| 93 | # same minor seems to be possible, so the latest one |
---|
| 94 | # found in the file will be the one we keep. |
---|
| 95 | # if we want the first one, just use > instead of >= |
---|
| 96 | if minor >= oldobject.minor : |
---|
| 97 | objects[major] = obj |
---|
| 98 | |
---|
| 99 | # Now we check each PDF object we've just created. |
---|
[208] | 100 | self.iscolor = None |
---|
| 101 | newpageregexp = re.compile(r"(/Type) ?(/Page)[/ \t\r\n]", re.I) |
---|
| 102 | colorregexp = re.compile(r"(/ColorSpace) ?(/DeviceRGB|/DeviceCMYK)[/ \t\r\n]", re.I) |
---|
[193] | 103 | pagecount = 0 |
---|
[240] | 104 | for object in objects.values() : |
---|
| 105 | content = "".join(object.content) |
---|
| 106 | pagecount += len(newpageregexp.findall(content)) |
---|
| 107 | if colorregexp.match(content) : |
---|
[208] | 108 | self.iscolor = 1 |
---|
| 109 | if self.debug : |
---|
[240] | 110 | sys.stderr.write("ColorSpace : %s\n" % content) |
---|
[193] | 111 | return pagecount |
---|
| 112 | |
---|
| 113 | def test() : |
---|
| 114 | """Test function.""" |
---|
[196] | 115 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
| 116 | sys.argv.append("-") |
---|
| 117 | totalsize = 0 |
---|
| 118 | for arg in sys.argv[1:] : |
---|
| 119 | if arg == "-" : |
---|
| 120 | infile = sys.stdin |
---|
| 121 | mustclose = 0 |
---|
| 122 | else : |
---|
[240] | 123 | infile = open(arg, "rU") |
---|
[196] | 124 | mustclose = 1 |
---|
| 125 | try : |
---|
[220] | 126 | parser = Parser(infile, debug=1) |
---|
[196] | 127 | totalsize += parser.getJobSize() |
---|
[200] | 128 | except pdlparser.PDLParserError, msg : |
---|
[196] | 129 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 130 | sys.stderr.flush() |
---|
| 131 | if mustclose : |
---|
| 132 | infile.close() |
---|
| 133 | print "%s" % totalsize |
---|
[193] | 134 | |
---|
| 135 | if __name__ == "__main__" : |
---|
| 136 | test() |
---|