[193] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[191] | 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
[303] | 6 | # (c) 2003, 2004, 2005, 2006 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) : |
---|
[252] | 49 | self.logdebug("DEBUG: Input file is in the PDF format.") |
---|
[220] | 50 | return 1 |
---|
| 51 | else : |
---|
| 52 | return 0 |
---|
| 53 | |
---|
[193] | 54 | def getJobSize(self) : |
---|
| 55 | """Counts pages in a PDF document.""" |
---|
[240] | 56 | # First we start with a generic PDF parser. |
---|
| 57 | lastcomment = None |
---|
| 58 | objects = {} |
---|
[241] | 59 | inobject = 0 |
---|
[243] | 60 | # objre = re.compile(r"\s*(\d+)\s+(\d+)\s+obj[<\s/]*") |
---|
| 61 | objre = re.compile(r"\s?(\d+)\s+(\d+)\s+obj[<\s/]?") |
---|
[241] | 62 | for fullline in self.infile.xreadlines() : |
---|
| 63 | parts = [ l.strip() for l in fullline.splitlines() ] |
---|
| 64 | for line in parts : |
---|
| 65 | if line.startswith("% ") : |
---|
| 66 | if inobject : |
---|
[240] | 67 | obj.comments.append(line) |
---|
[241] | 68 | else : |
---|
| 69 | lastcomment = line[2:] |
---|
[243] | 70 | else : |
---|
[241] | 71 | # New object begins here |
---|
[243] | 72 | result = objre.search(line) |
---|
| 73 | if result is not None : |
---|
| 74 | (major, minor) = map(int, line[result.start():result.end()].split()[:2]) |
---|
| 75 | obj = PDFObject(major, minor, lastcomment) |
---|
| 76 | obj.content.append(line[result.end():]) |
---|
| 77 | inobject = 1 |
---|
| 78 | elif line.startswith("endobj") \ |
---|
| 79 | or line.startswith(">> endobj") \ |
---|
| 80 | or line.startswith(">>endobj") : |
---|
| 81 | # Handle previous object, if any |
---|
| 82 | if inobject : |
---|
| 83 | # only overwrite older versions of this object |
---|
| 84 | # same minor seems to be possible, so the latest one |
---|
| 85 | # found in the file will be the one we keep. |
---|
| 86 | # if we want the first one, just use > instead of >= |
---|
| 87 | oldobject = objects.setdefault(major, obj) |
---|
| 88 | if minor >= oldobject.minor : |
---|
| 89 | objects[major] = obj |
---|
| 90 | inobject = 0 |
---|
[240] | 91 | else : |
---|
[243] | 92 | if inobject : |
---|
| 93 | obj.content.append(line) |
---|
[240] | 94 | |
---|
| 95 | # Now we check each PDF object we've just created. |
---|
[208] | 96 | self.iscolor = None |
---|
[243] | 97 | newpageregexp = re.compile(r"(/Type)\s?(/Page)[/\s]", re.I) |
---|
[208] | 98 | colorregexp = re.compile(r"(/ColorSpace) ?(/DeviceRGB|/DeviceCMYK)[/ \t\r\n]", re.I) |
---|
[193] | 99 | pagecount = 0 |
---|
[252] | 100 | for obj in objects.values() : |
---|
| 101 | content = "".join(obj.content) |
---|
[243] | 102 | count = len(newpageregexp.findall(content)) |
---|
| 103 | pagecount += count |
---|
[240] | 104 | if colorregexp.match(content) : |
---|
[208] | 105 | self.iscolor = 1 |
---|
[252] | 106 | self.logdebug("ColorSpace : %s" % content) |
---|
[193] | 107 | return pagecount |
---|
| 108 | |
---|
| 109 | def test() : |
---|
| 110 | """Test function.""" |
---|
[196] | 111 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
| 112 | sys.argv.append("-") |
---|
| 113 | totalsize = 0 |
---|
| 114 | for arg in sys.argv[1:] : |
---|
| 115 | if arg == "-" : |
---|
| 116 | infile = sys.stdin |
---|
| 117 | mustclose = 0 |
---|
| 118 | else : |
---|
[241] | 119 | infile = open(arg, "rb") |
---|
[196] | 120 | mustclose = 1 |
---|
| 121 | try : |
---|
[220] | 122 | parser = Parser(infile, debug=1) |
---|
[196] | 123 | totalsize += parser.getJobSize() |
---|
[200] | 124 | except pdlparser.PDLParserError, msg : |
---|
[196] | 125 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 126 | sys.stderr.flush() |
---|
| 127 | if mustclose : |
---|
| 128 | infile.close() |
---|
| 129 | print "%s" % totalsize |
---|
[193] | 130 | |
---|
| 131 | if __name__ == "__main__" : |
---|
| 132 | test() |
---|