pdftools-0.37/0000775000076500007650000000000011004732617012252 5ustar daviddavidpdftools-0.37/pdftools/0000775000076500007650000000000011004732617014104 5ustar daviddavidpdftools-0.37/pdftools/__init__.py0000644000076500001440000000201411003433240016236 0ustar davidusers# pdftools - A library of classes for parsing and rendering PDF documents. # Copyright (C) 2001-2008 by David Boddie # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library 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 # Library General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ __init__.py Initialisation for the pdftools package. """ __all__ = ["pdfdefs", "pdffile", "pdfpath", "pdftext"] __version__ = "0.37" version = '%s (Tuesday 22nd April 2008)' % __version__ pdftools-0.37/pdftools/pdfdefs.py0000644000076500001440000006231310767573552016155 0ustar davidusers# pdftools - A library of classes for parsing and rendering PDF documents. # Copyright (C) 2001-2008 by David Boddie # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library 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 # Library General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Created: 2004 """ pdfdefs.py Definitions of PDF-related classes and values that are needed by the other pdftools modules. """ import copy, string, sys, types class boolean: """Generic boolean class. Used to define "true" and "false". """ def __init__(self, value): self.value = value def __repr__(self): return "" % self.value def __cmp__(self, other): if not isinstance(other, boolean): # Invalid comparison return -1 if self.value == other.value: return 0 elif self.value == "true": return 1 elif self.value == "false": return -1 # Invalid value(s) return -1 true = boolean('true') false = boolean('false') class empty: """The empty class of which null is an instance.""" pass null = empty() class reference: """Class for defining references to objects. Instantiate with object and generation attributes, "obj" and "gen". Example: ref = pdftools.reference(15, 0) """ def __init__(self, obj, gen): self.obj = obj self.gen = gen class name: """Class for defining names. Typically the / symbol which defines names in a PDF document is removed when the name is stored in the "name" attribute. Example: n = pdftools.name("Font") """ def __init__(self, name): self.name = name def __repr__(self): return '' % self.name def __cmp__(self, other): if not isinstance(other, name): # Invalid comparison return -1 return cmp(self.name, other.name) class comment: """Comment class with "comment" attribute. Example: comment = pdftools.comment("Some text") """ def __init__(self, comment): self.comment = comment def __repr__(self): return "" % self.comment class object: """Object class with "object" attribute which usually contains a list of other elements of a PDF file, such as dictionaries, objects, references, arrays, etc. Example: obj = pdftools.object(["Some text", [1,2,3]]) """ def __init__(self, object): self.object = object class Stream: """Stream class. The "start" attribute of an instance of this class points to the start of a stream in a file. The "end" points to the character after the end of the stream, so that slice notation can be used to extract the stream from the document. Example: s = pdftools.Stream(32, 64) """ def __init__(self, start, end): self.start = start self.end = end class matrix: def __init__(self, rows): self.rows = rows def __repr__(self): values = reduce(lambda x, y: x + y, self.rows) format = ("((%03f, %03f, %03f),\n" " (%03f, %03f, %03f),\n" " (%03f, %03f, %03f))") return format % tuple(values) def ___mul___(self, r1, r2): rows = [[r1[0][0]*r2[0][0] + r1[0][1]*r2[1][0] + r1[0][2]*r2[2][0], r1[0][0]*r2[0][1] + r1[0][1]*r2[1][1] + r1[0][2]*r2[2][1], r1[0][0]*r2[0][2] + r1[0][1]*r2[1][2] + r1[0][2]*r2[2][2]], [r1[1][0]*r2[0][0] + r1[1][1]*r2[1][0] + r1[1][2]*r2[2][0], r1[1][0]*r2[0][1] + r1[1][1]*r2[1][1] + r1[1][2]*r2[2][1], r1[1][0]*r2[0][2] + r1[1][1]*r2[1][2] + r1[1][2]*r2[2][2]], [r1[2][0]*r2[0][0] + r1[2][1]*r2[1][0] + r1[2][2]*r2[2][0], r1[2][0]*r2[0][1] + r1[2][1]*r2[1][1] + r1[2][2]*r2[2][1], r1[2][0]*r2[0][2] + r1[2][1]*r2[1][2] + r1[2][2]*r2[2][2]]] return rows def __mul__(self, other): r1 = self.rows r2 = other.rows return matrix(self.___mul___(r1, r2)) def __rmul__(self, other): r1 = other.rows r2 = self.rows return matrix(self.___mul___(r1, r2)) def copy(self): return matrix(copy.deepcopy(self.rows)) def identity(size): return matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) class vector: def __init__(self, x, y): self.x, self.y = x, y def __repr__(self): return "" % (self.x, self.y) def __add__(self, other): x = self.x + other.x y = self.y + other.y # Return a new object. return vector(x, y) __radd__ = __add__ def __sub__(self, other): x = self.x - other.x y = self.y - other.y # Return a new object. return vector(x, y) def __rsub__(self, other): x = other.x - self.x y = other.y - self.y # Return a new object. return vector(x, y) def __cmp__(self, other): # This next expression will only return zero (equals) if both # expressions are false. return self.x == other.x or self.y == other.y def __abs__(self): return (self.x ** 2 + self.y ** 2) ** 0.5 def copy(self): """vector = copy(self) Copy the vector so that new vectors containing the same values are passed around rather than references to the same object. """ return vector(self.x, self.y) # The point class is a synonym for the vector class. # We subclass vector in order to make it clear that point is a class. # (It's easier to search for classes if they are declared in some way.) class point(vector): pass delimiter = '<>()[]{}/%' not_regular = string.whitespace + delimiter escaped = (('\\n', '\012'), ('\\r', '\015'), ('\\t', '\011'), ('\\b', '\177'), ('\\f', '\014'), ('\\(', '('), ('\\)', ')'), ('\\\\', '\\')) hexadecimal = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15} whitespace = '\000\011\012\014\015 ' # hexa = '0123456789abcdef' integer = '0123456789+-' real = '0123456789+-.' base85m4 = long(pow(85,4)) base85m3 = long(pow(85,3)) base85m2 = long(pow(85,2)) class FileWrapper: def __init__(self, file): self.file = file def __getitem__(self, item): if type(item) == types.SliceType: self.file.seek(item.start) length = max(0, item.stop - item.start) data = self.file.read(length) if len(data) != length: raise IndexError, "string index out of range" else: self.file.seek(item) data = self.file.read(1) if not data: raise IndexError, "string index out of range" return data if sys.version_info < 2.0: def __getslice__(self, i, j): return self[max(0, i):max(0, j):] def __len__(self): offset = self.file.tell() self.file.seek(0, 2) length = self.file.tell() self.file.seek(offset, 0) return int(length) def find(self, sub, start = 0, end = None): if end is None: end = len(self) elif end < 0: end = len(self) + end length = len(sub) if start < 0: window_start = end + start else: window_start = start self.file.seek(window_start, 0) # Try to read twice the length of the substring. window_end = window_start + length * 2 # Only read as much as is available. read_to = min(window_end, end) read_length = read_to - window_start data = self.file.read(read_length) while end - window_start >= length: at = data.find(sub) if at != -1: return window_start + at # Start searching after the substring length. window_start = window_start + length # Try to read another substring worth of data. window_end = window_end + length # Only read as much as is available. next_read_to = min(window_end, end) read_length = next_read_to - read_to read_to = next_read_to data = data[length:] + self.file.read(read_length) return -1 def rfind(self, sub, start = 0, end = None): if end is None: window_end = end = len(self) elif end < 0: window_end = len(self) + end else: window_end = end length = len(sub) if start < 0: window_start = end + start else: window_start = start # Try to read twice the length of the substring. window_start = window_end - length * 2 # Only read as much as is available. read_from = max(start, window_start) read_length = window_end - read_from self.file.seek(read_from, 0) data = self.file.read(read_length) while window_end - start >= length: at = data.rfind(sub) if at != -1: return read_from + at # Start searching before the substring length. window_end = window_end - length # Try to read another substring worth of data. window_start = window_start - length # Only read as much as is available. next_read_from = max(start, window_start) read_length = read_from - next_read_from self.file.seek(next_read_from, 0) read_from = next_read_from data = self.file.read(read_length) + data[:-length] return -1 class PDFError(Exception): pass # Abstract class from which the PDFDocument and PDFContents classes inherit # common methods. class Abstract: def _skip_whitespace(self, offset): while offset < self.length: if self.file[offset] in whitespace: offset = offset + 1 else: break return offset def _read_comment(self, offset): at = offset while self.file[at] not in '\012\015': at = at + 1 text = self.file[offset:at] while self.file[at] in '\012\015': at = at + 1 return at, comment(text) def _read_string(self, offset): # Strings start with ( and end with a matched ) # The level of parentheses is the number of ) required to end # the string level = 1 at = offset while (level > 0) and (at < self.length): # Look for \ backslash = string.find(self.file, '\\', at) if backslash == -1: backslash = self.length # Look for ( start = string.find(self.file, '(', at) if start == -1: start = self.length # Look for ) end = string.find(self.file, ')', at) if end == -1: end = self.length # \ ( ) or \ ) ( if (backslash < start) and (backslash < end): # Skip escaped character at = backslash + 2 # ( \ ) or ( ) \ elif start < end: level = level + 1 at = start + 1 # ) \ ( or ) ( \ elif end < start: level = level - 1 at = end + 1 else: raise PDFError, 'Problem with string at %s' % hex(offset) # All text from the character at "offset" until the character before # "at" is comment. return at, self._clean_string(self.file[offset:at-1]) def _clean_string(self, a): for old, new in escaped: a = string.replace(a, old, new) # Octal numbers are the same in Python as they are in PDF strings return a def _read_hexadecimal(self, offset): # Hexadecimal strings start with a < and end with a > # Nesting is not allowed # The characters contained are 0-9 A-F a-f at = string.find(self.file, '>', offset) return at + 1, self._clean_hexadecimal(string.lower(self.file[offset:at])) def _clean_hexadecimal(self, a): # Read the string, converting the pairs of digits to # characters b = '' shift = 4 value = 0 try: for i in a: value = value | (hexadecimal[i] << shift) shift = 4 - shift if shift == 4: b = b + chr(value) value = 0 except ValueError: raise PDFError, 'Problem with hexadecimal string %s' % a return b def _asciihexdecode(self, text): at = string.find(text, '>') return self._clean_hexadecimal(string.lower(text[:at])) def _ascii85decode(self, text): end = string.find(text, '~>') new = [] i = 0 ch = 0 value = 0 while i < end: if text[i] == 'z': if ch != 0: raise PDFError, 'Badly encoded ASCII85 format.' new.append('\000\000\000\000') ch = 0 value = 0 else: v = ord(text[i]) if v >= 33 and v <= 117: if ch == 0: value = ((v-33) * base85m4) elif ch == 1: value = value + ((v-33) * base85m3) elif ch == 2: value = value + ((v-33) * base85m2) elif ch == 3: value = value + ((v-33) * 85) elif ch == 4: value = value + (v-33) c1 = int(value >> 24) c2 = int((value >> 16) & 255) c3 = int((value >> 8) & 255) c4 = int(value & 255) new.append(chr(c1) + chr(c2) + chr(c3) + chr(c4)) ch = (ch + 1) % 5 i = i + 1 if ch != 0: c = chr(value >> 24) + chr((value >> 16) & 255) + \ chr((value >> 8) & 255) + chr(value & 255) new.append(c[:ch-1]) return string.join(new, "") def _read_name(self, offset): # Read characters in the range 33-126 but not delimiters b = '' at = offset while at < self.length: n = ord(self.file[at]) if n >= 33 and n <= 126 and (self.file[at] not in delimiter): at = at + 1 else: break return at, name(self._clean_name(self.file[offset:at])) def _clean_name(self, a): # Find # symbols followed by hexadecimal b = '' at = 0 try: while at < self.length: h = string.find(a, '#', at) if h == -1: b = b + a[at:] break else: b = b + a[at:h] + self._clean_hexadecimal(a[h+1:h+3]) at = h + 3 except IndexError: raise PDFError, 'Problem with name %s' % a return b def _read_next(self, offset, this_array): #print "_read_next", hex(offset) c = self.file[offset] # Comment if c == '%': #print 'comment', hex(offset) at, element = self._read_comment(offset+1) # Boolean elif c == 't': if self.file[offset:offset+4] == 'true': #print 'true', hex(offset) element = true at = offset + 4 else: raise PDFError, 'Expected true at %s' % hex(offset) elif c == 'f': if self.file[offset:offset+5] == 'false': #print 'false', hex(offset) element = false at = offset + 5 else: raise PDFError, 'Expected false at %s' % hex(offset) # Integer or Real elif c in integer: value = c offset = offset + 1 is_real = 0 while offset < self.length: n = self.file[offset] if n in integer: value = value + n elif n in real: value = value + n is_real = 1 else: break offset = offset + 1 at = offset if is_real == 0: #print 'integer', hex(offset), int(value) element = int(value) else: #print 'real', hex(offset), float(value) element = float(value) elif c in real: value = c offset = offset + 1 while offset < self.length: n = self.file[offset] if n in real: value = value + n else: break offset = offset + 1 at = offset element = float(value) # Name elif c == '/': #print 'name', hex(offset) at, element = self._read_name(offset+1) # String elif c == '(': #print 'string', hex(offset) at, element = self._read_string(offset+1) # Hexadecimal string or dictionary elif c == '<': if self.file[offset+1] != '<': #print 'hexadecimal string', hex(offset) at, element = self._read_hexadecimal(offset+1) else: #print 'dictionary', hex(offset) at, element = self._read_dictionary(offset+2) # Array elif c == '[': #print "array", hex(offset) at, element = self._read_array(offset+1) # null elif c == 'n': if self.file[offset:offset+4] == 'null': #print 'null', hex(offset) at = offset + 4 element = null else: raise PDFError, 'Expected null at %s' % hex(offset) # Object reference elif c == 'R': #print 'reference', hex(offset) # Take the last two items in the array and check that they # are integers obj, gen = this_array[-2], this_array[-1] this_array.pop() this_array.pop() element = reference(obj, gen) at = offset + 1 # Stream or other token beginning with s elif c == 's': if self.file[offset:offset+6] == 'stream': #print 'stream', hex(offset) at, element = self._read_stream(offset+6) elif self.file[offset:offset+9] == 'startxref': return offset, None # Object elif c == 'o': if self.file[offset:offset+3] == 'obj': #print 'object', hex(offset) # Take the last two items in the array and check that they # are integers obj, gen = this_array[-2], this_array[-1] this_array.pop() this_array.pop() # Read the object at, element = self._read_object(offset+3) element.obj = obj element.gen = gen else: raise PDFError, 'Expected obj at %s' % hex(offset) else: raise PDFError, 'Unknown object found at %s' % hex(offset) return at, element def _read_array(self, offset): #print '_read_array', hex(offset) # Arrays begin with [ and end with ] # They can be nested like strings, but contain objects rather # than just a series of bytes this_array = [] at = offset while at < self.length: at = self._skip_whitespace(at) # print hex(at) # Look at the next character # End of array if self.file[at] == ']': break else: at, element = self._read_next(at, this_array) # Add the element to the array if element is not None: this_array.append(element) else: break #print '_read_array return', hex(at) return at + 1, this_array def _read_dictionary(self, offset): #print '_read_dictionary', hex(offset) # Dictionaries start with << and end with >> # They can be nested at = offset this_array = [] while at < self.length: at = self._skip_whitespace(at) # Look at the next character # End of dictionary if self.file[at:at+2] == '>>': break else: #print "dictionary next", hex(at) at, element = self._read_next(at, this_array) if element is not None: this_array.append(element) else: break # Collate the list into key value pairs dict = {} keyvalue = 0 for element in this_array: # Key or value if keyvalue == 0: if isinstance(element, name) == 0: raise PDFError, \ 'Key found which was not a name "'+repr(element) + \ '" in dictionary at %s' % hex(offset - 1) else: key = element keyvalue = 1 else: # Use the textual form of the name to make the key dict[key.name] = element keyvalue = 0 if keyvalue == 1: print 'Incomplete dictionary at %s ?' % hex(offset - 1) #print '_read_dictionary return', hex(at) return at + 2, dict def _read_object(self, offset): # Objects start with obj and end with endobj #print '_read_object', hex(offset) at = offset this_array = [] while at < self.length: at = self._skip_whitespace(at) if self.file[at:at+6] == 'endobj': break else: #print "object next", hex(at) at, element = self._read_next(at, this_array) if element is not None: this_array.append(element) else: break #print '_read_object return' return at+6, object(this_array) def _read_ref(self, offset): at, obj = self.read_integer(offset, self.length) at = self._skip_whitespace(at) at, gen = self.read_integer(at, self.length) at = self._skip_whitespace(at) at, element = self._read_next(at, [obj, gen]) return element def _read_stream(self, offset): #print '_read_stream', hex(offset) # Expect either a carriage return then a linefeed or just a linefeed. if self.file[offset:offset + 2] == '\r\n': offset = offset + 2 elif self.file[offset] == '\n': offset = offset + 1 else: raise PDFError, "Unexpected start to the stream at %x" % offset # Temporary solution at = string.find(self.file, 'endstream', offset) #print 'endstream', hex(at) return at+9, Stream(offset, at) pdftools-0.37/pdftools/pdffile.py0000644000076500001440000026116511003433203016125 0ustar davidusers# pdftools - A library of classes for parsing and rendering PDF documents. # Copyright (C) 2001-2008 by David Boddie # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library 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 # Library General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Historical information # Created: Fri 9th March 2001 """ Example of use: from pdftools.pdffile import PDFDocument file = "MyFile.pdf" doc = PDFDocument(file) print "Document uses PDF format version", doc.document_version() pages = doc.count_pages() print "Document contains %i pages." % pages if pages > 123: page123 = doc.read_page(123) contents123 = page123.read_contents() print "The objects found in this page:" print print contents123.contents """ import sys, types, zlib # Import useful definitions. from pdfdefs import * # Import related modules. import pdftext, pdfpath class PDFDocument(Abstract): """PDF document reading class. Open a document by reading a PDF file using a file name passed as a string on instantiation. Example: file = "MyFile.pdf" doc = PDFDocument(file) Useful methods: total_number = count_pages(self) page_object = read_page(self, number) # 1 <= number <= total_number """ def __init__(self, path, in_memory = 0): try: if in_memory: self.file = open(path, 'rb').read() else: self.file = FileWrapper(open(path, 'rb')) except IOError: raise PDFError, "Couldn't open the specified file: %s" % path self.path = path self.length = len(self.file) # Prepare the document for reading. self._read_document() # Return version information def document_version(self): """Read the version of PDF used to encode the document. Returns a string containing the version number. Example: version = doc.document_version() """ offset, element = self._read_comment(1) if isinstance(element, comment) == 0: print 'Not a PDF file' elif element.comment[:4] != 'PDF-': raise PDFError, 'Not a PDF file' else: self.version = element.comment[4:] return self.version def find_page(self, number, count = 0, tree = None): """number, dict = find_page(self, number, count = 0, tree = None) Takes a page number and a starting tree object and returns the page number and either a dictionary containing the page attributes (if successful) or None (if unsuccessful). """ # Using the catalogue entry if necessary, descend until a # particular page is found. if tree == None: pages = self.catalog['Pages'] tree = self._dereference(pages) # Page tree if tree['Type'].name == 'Pages': if count < number <= count + tree['Count']: # The page is contained within a child of this node. kids = tree['Kids'] # We do not know how many pages are contained within each # child node, so we must search through them rather than # trying to directly index the "Kids" array. for ref in kids: kid = self._dereference(ref) count, kid = self.find_page(number, count, kid) if kid is not None: # The required page was found. return count, kid else: # The page is outside the range of pages contained beneath # this node. count = count + tree["Count"] elif tree['Type'].name == 'Page': # Page # Increment the page counter to provide this page with the correct # number and compare it with the page number we are looking for. count = count + 1 if count == number: return count, tree return count, None def count_pages(self): # Read the "Pages" catalogue entry. pages = Pages(self, self._dereference(self.catalog["Pages"])) # Read the "Count" property (dictionary entry). return pages["Count"] # _read_catalog() must be called before this function is called. def read_page(self, number): """page_object = read_page(self, number) Return a Page object corresponding to the specified page. If the page is not found then a PDFError exception is raised. Example: page = doc.read_page(123) """ # Look up the page details. number, page = self.find_page(number) if page is None: raise PDFError, "No such page" return Page(self, page) def write_document(self, path = None, linearized = 0): """write_document(self, path = None, linearized = 0) Write the document to a file, either using an optional path or to the original file. If linearized, the file will be optimised for access by readers that read the file sequentially. """ if path is None: path = self.path try: fh = open(path, "wb") self._write_document(fh, linearized) except IOError: raise PDFError, "Failed to write the document to %s" % path def _read_trailer(self, offset): at = self._skip_whitespace(offset) at, trailer_dict = self._read_dictionary(at+2) at = string.find(self.file, 'startxref', at) at = self._skip_whitespace(at+9) value = '' while at < self.length: n = self.file[at] if n in integer: value = value + n else: break at = at + 1 # Return the dictionary and the location of the xref table return trailer_dict, int(value) def _read_integer(self, offset, end): value = self.file[offset] at = offset + 1 while at < end: n = self.file[at] if n in integer: value = value + n else: break at = at + 1 return at, int(value) def _read_xref(self, offset, end = None): """used, free = _read_xref(self, offset, end = None) Read the cross reference table found at the offset given in the file and ending at the end specified. The used and free dictionaries returned take the form: { object_number : (generation, offset), ... } """ if end is None: end = self.length used = {} free = {} next = [] at = offset at = self._skip_whitespace(at) at, start = self._read_integer(at, end) at = self._skip_whitespace(at) at, number = self._read_integer(at, end) at = self._skip_whitespace(at) item = start # Read items until we either reach the end of the xref table or # we have read the given number of items. #while at < end and number > 0: while number > 0: c = self.file[at] if c == 'f': if len(next) != 2: raise PDFError, 'Problem in xref table at %s' % hex(at) at = at + 1 free[item] = next next = [] item = item + 1 number = number - 1 elif c == 'n': if len(next) != 2: raise PDFError, 'Problem in xref table at %s' % hex(at) at = at + 1 used[item] = next next = [] item = item + 1 number = number - 1 # Integer elif c in integer: if len(next) < 2: at, value = self._read_integer(at, end) # Append value to the list next.append(value) else: # An integer after a pair of integers: the last pair must be a subsection. item = next[0] number = next[1] at, value = self._read_integer(at, end) # Append value to the list next.append(value) else: # End of table - can't guarantee that the xref table # will immediately precede the trailer. There might be # an old trailer after it, but searching for "trailer" # from the start of the xref table isn't reliable. raise PDFError, 'Unexpected element in xref table at %s' % hex(at) at = self._skip_whitespace(at) return start, number, used, free def _read_linearized(self): # Starting at the beginning of the file, skip the initial # version declaration and binary data. version = self.document_version() try: if float(version) > 1.4: raise ValueError except ValueError: raise PDFError, "Unsupported PDF version: %s" % version i = 1 + len("PDF-") + len(version) i = self._skip_whitespace(i) # We are now at the parameters following the document version. while self.file[i] == '%': i, comment = self._read_comment(i + 1) if self.file[i] in whitespace: i = self._skip_whitespace(i) # We should now be at document data which may contain linearization # parameters. Therefore, look for object and generation numbers # followed by the "obj" operator. # Use a list for the parameter stack required for operators. this_array = [] # Search up to the end of the file. Note that the file may not # have been completely loaded. found = None while i < self.length: i, element = self._read_next(i, this_array) i = self._skip_whitespace(i) if isinstance(element, object): # We have found an object. Assume that it is a dictionary # containing linearization parameters. found = element break else: # Keep the value on the stack. this_array.append(element) if found is None: return None # Check the object to discover whether it contains a dictionary # defining the linearization parameters. if found.object == [] or type(found.object[0]) != types.DictType: return None d = found.object[0] if not d.has_key('Linearized'): return None # Copy useful information stored in the dictionary. if d.has_key('L'): self.length = d['L'] # File length if d.has_key('N'): self.number_of_pages = d['N'] # Since this is a linearized document then this dictionary should # be followed by the cross reference table. i = self._skip_whitespace(i) # Record the cross reference table position. xref_pos = i # Look for the trailer. trailer_pos = string.find(self.file, "trailer", xref_pos) trailer_dict, false_xref_pos = self._read_trailer(trailer_pos + 7) return trailer_pos, trailer_dict, xref_pos def _trailer_info(self, backwards = 1): """dict = _trailer_info(self, backwards = 1) Return the trailer dictionary of the document which contains the location of the cross-reference table. This sets up an internal copy of the dictionary for later use, although it isn't necessary to do this. By default, the trailer is searched for from the end of the file. If backwards equals zero then it is searched for from the beginning of the file. Example: dict = doc._trailer_info() """ if backwards == 1: trailer_pos = string.rfind(self.file, 'trailer') else: trailer_pos = string.find(self.file, 'trailer') # print hex(trailer_pos) trailer_dict, xref_pos = self._read_trailer(trailer_pos + 7) return trailer_pos, trailer_dict, xref_pos # _trailer_info must be called before _xref_info() def _xref_info(self, xref_pos): """Return the contents of the cross reference table. Example: objects_in_use, free_space = doc._xref_info() """ start, number, used, free = \ self._read_xref(xref_pos + 4) #, self.trailer_pos) return used, free def _find_in_used(self, obj, gen): for key, value in self.used.items(): if obj == key and gen == value[1]: # Read the object number and generation number from the object's # location in the file. at, this_obj = self._read_integer(value[0], self.length) gen_at = self._skip_whitespace(at) if this_obj != obj: raise PDFError, \ "Object number %i at %s disagrees with that the " + \ "number %i from the cross reference table." % \ (this_obj, hex(value[0]), obj) at, this_gen = self._read_integer(gen_at, self.length) at = self._skip_whitespace(at) if this_gen != gen: raise PDFError, \ "Generation number %i at %s disagrees with the " + \ "number %i given in the cross reference table." % \ (this_gen, hex(gen_at), gen) at, element = self._read_next(at, [obj, gen]) return element return None # *Required* before any page lookup is done def _read_catalog(self): """cat = _read_catalog(self) Read the document's catalog(ue). This is necessary before any page lookup is attempted. This method will read the document trailer's dictionary and cross-reference table if required. Example: cat = doc._read_catalog() """ # Read the trailer to find the /Root object root = self.trailer_dict['Root'] catalog = self._dereference(root) return catalog # Find an inherited attribute: move up the document structure until # the attribute is found or we reach the top. def _inherit(self, tree, key): while tree.has_key(key)==0 and tree.has_key('Parent')==1: ref = tree['Parent'] tree = self._dereference(ref) if tree.has_key(key) == 0: return else: return tree[key] # Dereference an entry in an array or dictionary def _dereference(self, element): # Reduce to the simplest description if isinstance(element, reference): element = self._find_in_used(element.obj, element.gen).object if isinstance(element, object): element = element.object if type(element) == types.ListType: if len(element) == 1: return element[0] else: return element else: return element return element def _collect_dict(self, dict): new_dict = {} for key in dict.keys(): # Dereference if required element = self._dereference(dict[key]) # Dictionary if type(element) == types.DictionaryType: new_dict[key] = self._collect_dict(element) else: new_dict[key] = element return new_dict def _read_document(self): """_read_document(self) Prepare the document for access by finding the document trailer and cross reference table. """ self.trailer_dict = {} # Check whether the document is "linearized". linear_info = self._read_linearized() if linear_info is not None: trailer_pos, trailer_dict, xref_pos = linear_info self.is_linearized = 1 else: # Look for the trailer at the end of the file. backwards_info = self._trailer_info(backwards = 1) trailer_pos, trailer_dict, xref_pos = backwards_info self.is_linearized = 0 # Look for a valid trailer dictionary, containing a "Root" entry, # and a valid cross reference table. if trailer_dict.has_key("Root"): self.trailer_dict = trailer_dict self.trailer_pos = trailer_pos if xref_pos != 0: # Examine any cross reference tables present. self.used, self.free = self._xref_info(xref_pos) self.xref_pos = xref_pos if trailer_dict.has_key("Prev"): # Examine any previous cross reference dictionaries. used, free = self._xref_info(trailer_dict["Prev"]) # Merge the used and free dictionaries. for key, value in used.items(): if not self.used.has_key(key): self.used[key] = value for key, value in free.items(): if not self.free.has_key(key): self.free[key] = value # Retrieve the document's catalogue. self.catalog = self._read_catalog() def _write_document(self, fh, linearized): # Write the PDF version to the file as a comment. fh.write("%%PDF-%s\r" % self.version) # Create the trailer from the objects in the file. trailer, xref_table, catalog = self._create_structure() if self.linearized: self._write_linearized(trailer) self._write_xref(xref_table) self._write_catalog(catalog) # Write the objects to the file. if not self.linearized: self._write_linearized(trailer) self._write_xref(xref_table) self._write_catalog(catalog) fh.write("%%EOF\r") class Page(Abstract): """Page(Abstract) page = Page(document, page_dictionary) Create a page object to represent a given page in a document. The document parameter is the PDFDocument object from which the page_dictionary dictionary was obtained for a given page. These objects are usually created by the PDFDocument.read_page method. Useful methods: contents = read_contents(self) """ def __init__(self, document, page_dictionary): # Keep a reference to the document as we will need to # use it to retrieve information required to read the page # contents. self.document = document # Record the page dictionary. self.page_dict = page_dictionary # Find required items and cache them for later use. self.required = {} # Read the page type. self.required["Type"] = \ self.document._dereference(self.page_dict["Type"]) # Fetch the MediaBox. self.required["MediaBox"] = \ self.document._dereference( self.document._inherit(self.page_dict, 'MediaBox') ) # Fetch the Parent and convert it into a Pages object. self.required["Parent"] = \ Pages( self.document, self.document._dereference(self.page_dict["Parent"]) ) # Fetch the Resources. resources = \ self.document._dereference( self.document._inherit(self.page_dict, 'Resources') ) # Collect all the resources in the Resources entry into one dictionary. self.required["Resources"] = self.document._collect_dict(resources) # Fetch the Contents (if present). This is not a required property # but it is useful to process this item here. contents_element = self.document._inherit(self.page_dict, 'Contents') if contents_element is not None: self.required["Contents"] = \ self._read_contents_element(contents_element) def _read_contents_element(self, contents_element): if isinstance(contents_element, reference): # A reference to an object rather than a list. Enclose it in # a list for processing as for an array (list) of references. contents_element = [self.document._dereference(contents_element)] contents_list = map( lambda item: self.document._dereference(item), contents_element ) contents_output = [] for item in contents_list: # Each item in the contents list contains a dictionary with # various entries describing the stream and the stream itself. # The length of the stream is given in the dictionary. length = self.document._dereference(item[0].get('Length', 0)) # A list of the filters to be used to process the stream is also # given. filters = self.document._dereference(item[0].get('Filter', [])) # The second entry contains the stream. We check that the stream # start and end positions determined when the file was read agree # with the length provided by the dictionary entry. stream = self.document._dereference(item[1]) start = stream.start end = stream.end #print length, start, end, end - start # #if length != (end - start): # # raise PDFError, "Problem found in reading stream at %x (" % \ # start + \ # "I may have made a mistake when reading the file)." # Read the stream contents. contents = self.document.file[start:start + length] if isinstance(filters, name): filters = [filters] # Examine the Contents dictionary for filter in filters: # print filter.name if filter.name == '_asciihexdecode': contents = self._asciihexdecode(contents) elif filter.name == '_ascii85decode' or filter.name == 'ASCII85Decode': contents = self._ascii85decode(contents) elif filter.name == 'FlateDecode': contents = zlib.decompress(contents) else: raise PDFError, 'Unknown Filter method: %s' % filter.name contents_output.append(contents) return string.join(contents_output, "") def __getitem__(self, key): # Check whether this item has been cached. if self.required.has_key(key): return self.required[key] # Try to find the requested key from the page dictionary, using # the document as appropriate. # Retrieve a value from the page dictionary or an ancestor, if it # is an inherited property. value = self.document._inherit(self.page_dict, key) if value is None: raise KeyError, key # Dereference the value provided; this can be done whether the # value is an indirect reference or not. return self.document._dereference(value) def keys(self): k = self.required.keys() return k + filter(lambda x: x not in k, self.page_dict.keys()) def values(self): k = self.required.values() return k + filter(lambda x: x not in k, self.page_dict.values()) def items(self): k = self.required.items() return k + filter(lambda x: x not in k, self.page_dict.items()) def __getattr__(self, attr): if attr == "__call__" or attr == "__repr__": raise AttributeError, attr try: return self.__getitem__(attr) except KeyError: raise AttributeError, attr def read_contents(self): """content = read_contents(self) Return a PDFContents object containing the necessary information required to display the page. """ return PDFContents(self) class Pages(Abstract): """Pages(Abstract) pages = Pages(document, pages_dictionary) Create a pages object to represent a collection of pages in a document. The document parameter is the PDFDocument object from which the pages_dictionary dictionary was obtained for a given set of pages. These objects can be used to encapsulate pages dictionaries found by looking at the parents of Page objects. """ def __init__(self, document, pages_dictionary): # Keep a reference to the document as we will need to # use it to retrieve information required to read the page # contents. self.document = document # Record the page dictionary. self.pages_dict = pages_dictionary # Find required items and cache them for later use. self.required = {} # Read the pages type. self.required["Type"] = \ self.document._dereference(self.pages_dict["Type"]) # Read the Kids property. self.required["Kids"] = self.pages_dict["Kids"] # Read the Count property (number of pages below this object). self.required["Count"] = self.pages_dict["Count"] # Fetch the Parent and convert it into a Pages object. # Every Pages object except the root Pages object has a Parent # property. if self.pages_dict.has_key("Parent"): self.required["Parent"] = \ Pages( self.document, self.document._dereference(self.pages_dict["Parent"]) ) def __getitem__(self, key): # Check whether this item has been cached. if self.required.has_key(key): return self.required[key] # Try to find the requested key from the page dictionary, using # the document as appropriate. # Retrieve a value from the page dictionary or an ancestor, if it # is an inherited property. value = self.document._inherit(self.pages_dict, key) if value is None: raise KeyError, key # Dereference the value provided; this can be done whether the # value is an indirect reference or not. return self.document._dereference(value) def keys(self): k = self.required.keys() return k + filter(lambda x: x not in k, self.pages_dict.keys()) def values(self): k = self.required.values() return k + filter(lambda x: x not in k, self.pages_dict.values()) def items(self): k = self.required.items() return k + filter(lambda x: x not in k, self.pages_dict.items()) def __getattr__(self, attr): if attr == "__call__" or attr == "__repr__": raise AttributeError, attr try: return self.__getitem__(attr) except KeyError: raise AttributeError, attr class command: def __init__(self, command): self.command = command # Define a graphics state class to contain the dictionaries created by the # PDFContents class. class GraphicsState: def __init__(self, graphics_state): # Make a copy of the dictionary to avoid problems with mutable # objects. self.graphics_state = graphics_state.copy() def __getitem__(self, key): return self.graphics_state[key] def get(self, key, default = None): return self.graphics_state.get(key, default) def keys(self): return self.graphics_state.keys() def values(self): return self.graphics_state.values() def items(self): return self.graphics_state.items() class PushGS: def __init__(self, graphics_state): # Make a copy of the dictionary to avoid problems with mutable # objects. self.graphics_state = graphics_state.copy() class PopGS: def __init__(self, graphics_state): # Make a copy of the dictionary to avoid problems with mutable # objects. self.graphics_state = graphics_state.copy() # Define the characters used to start commands in stream objects for the # benefit of the PDFContents class. commands = string.letters+'*"'+"'" class PDFContents(Abstract): """PDFContents(Abstract) Class used to interpret and render content streams. This is achieved by creating an instance for a particular MediaBox, Resources dictionary and content stream obtained from the Page._read_contents method. Example: doc = PDFDocument("MyFile.pdf") ... page = doc.read_page(123) co = PDFContents(page) Useful attributes: self.contents # contains the objects found in the page whose # contents this object represents. """ def __init__(self, page): # Keep a reference to the page. self.page = page #self.mediabox = page.MediaBox #self.resources = page.Resources # Determine the media dimensions. self.mediabox = page.MediaBox # Define the graphics origin. self.origin = point( min(self.mediabox[0], self.mediabox[2]), min(self.mediabox[1], self.mediabox[3]) ) # Set the current point to the origin. self.current_point = self.origin # Set the rendering matrix to the unit matrix. self.rendering_matrix = identity(3) # Set the current transformation matrix to the unit matrix. self.CTM = identity(3) # Define a graphics state and a stack. self.graphics_state = \ { "flatness": 0, # line end caps: 0 butt, 1 round end, 2 squared end "linecap": 0, # line dash pattern: solid line "linedash": ([], 0), # line join type: 0 mitre, 1 round, 2 bevel "linejoin": 0, # line width: 0 thinnest line on device "linewidth": 1, # mitre limit: >= 1 "miterlimit": 10, # stroke adjust: true/false "stroke adjust": boolean("true"), # overprint strokes: other separations are left unchanged (true), # other separations are overwritten (false) "stroke overprint": boolean("false"), # overprint fills: as overprint strokes but for fills "fill overprint": boolean("false"), # overprint mode: 0 (see p328 of the PDF 1.3 specification) "overprint mode": 0, # smoothness: value in the range [0, 1] (default value unknown) "smoothness": 0, # Colour properties # Fill colour space "fill color space": "DeviceGray", # Use a dictionary to define a fill colour for each colour space # used. "fill color": { "DeviceGray": 0, "DeviceRGB": [0, 0, 0], "DeviceCMYK": [0, 0, 0, 0] }, # Stroke colour space "stroke color space": "DeviceGray", # Use a dictionary to define a stroke colour for each colour space # used. "stroke color": { "DeviceGray": 0, "DeviceRGB": [0, 0, 0], "DeviceCMYK": [0, 0, 0, 0] } } self.graphics_state_stack = [] # Read the contents. self.contents = [] self.file = page.Contents self.length = len(self.file) self.contents = self._read_contents() def _read_next(self, offset, this_array): c = self.file[offset] # Comment if c == '%': # print 'comment' return self._read_comment(offset+1) # Boolean if c == 't': if self.file[offset:offset+4] == 'true': # print 'true' return offset + 4, true if c == 'f': if self.file[offset:offset+5] == 'false': # print 'false' return false, offset + 5 # Integer or Real if c in integer: value = c offset = offset + 1 is_real = 0 while offset < self.length: n = self.file[offset] if n in integer: value = value + n elif n in real: value = value + n is_real = 1 else: break offset = offset + 1 if is_real == 0: # print 'integer' return offset, int(value) else: # print 'real' return offset, float(value) if c in real: value = c offset = offset + 1 while offset < self.length: n = self.file[offset] if n in real: value = value + n else: break offset = offset + 1 return offset, float(value) # Name if c == '/': # print 'name' return self._read_name(offset+1) # String if c == '(': # print 'string' return self._read_string(offset+1) # Hexadecimal string or dictionary if c == '<': if self.file[offset+1] != '<': # print 'hexadecimal string' return self._read_hexadecimal(offset+1) else: return self._read_dictionary(offset+2) # Array if c == '[': return self._read_array(offset+1) # null if c == 'n': if self.file[offset:offset+4] == 'null': # print 'null' return offset + 4, null # # Stream # if c == 's': # # at, element = self._read_stream(offset+6) # None of the above # Must be a command # Read until next whitespace at = offset while self.file[at] in commands: at = at + 1 element = command(self.file[offset:at]) return at, element def _read_contents(self): """Read a content stream. For example, assuming that "doc" is an instance of PDFDocument: page = doc.read_page(123) contents = page.read_contents() # contents._read_contents() is called by the PDFContents.__init__ method. page_contents = contents.contents """ if self.page.MediaBox == None: print 'No MediaBox defined.' return [] if self.page.Resources == None: print 'No associated resources.' return [] if self.file == None: print 'No content stream to render.' return [] # The items found while reading the contents items = [] # The contents generated from the initial raw contents. contents = [] at = self._skip_whitespace(0) while at < self.length: next, item = self._read_next(at, items) # if isinstance(item, command): print at, self.length, item.command if isinstance(item, command): com = item.command if com == 'q': # Save graphics state on the graphics state stack. self.graphics_state_stack.append(self.graphics_state) contents.append(PushGS(self.graphics_state)) elif com == 'Q': # Restore the graphics state from the graphics state # stack. self.graphics_state = self.graphics_state_stack.pop() contents.append(PopGS(self.graphics_state)) elif com == 'cm': # Modify the coordinate transformation matrix # (for user to device space transformations) by # concatenating a matrix defined by the given six # numbers. render_matrix = matrix( [items[-6:-4]+[0], items[-4:-2]+[0], items[-2:]+[1]] ) items = items[:-6] self.CTM = self.CTM * render_matrix elif com == 'i': # Set the flatness parameter. self.graphics_state["flatness"] = items.pop() elif com == 'J': # Set the line end cap parameter. self.graphics_state["linecap"] = items.pop() elif com == 'd': # Set the line dash pattern. phase = items.pop() dash_array = items.pop() self.graphics_state["linedash"] = (dash_array, phase) elif com == 'j': # Set the line join parameter. self.graphics_state["linejoin"] = items.pop() elif com == 'w': # Set the line width. self.graphics_state["linewidth"] = items.pop() elif com == 'M': # Set the mitre limit. self.graphics_state["miterlimit"] = items.pop() elif com == 'gs': # Use the generic graphics state operator to set a # parameter in the general graphics state using an # extended graphics state dictionary. self.generic_graphics_state(items.pop()) # Colour/Color operators elif com == 'g': # Set the colour space to DeviceGray and set the grey # tint for filling paths. self.graphics_state["fill color space"] = "DeviceGray" self.graphics_state["fill color"]["DeviceGray"] = \ items.pop() elif com == 'G': # Set the colour space to DeviceGray and set the grey # tint for path strokes. self.graphics_state["stroke color space"] = "DeviceGray" self.graphics_state["stroke color"]["DeviceGray"] = \ items.pop() elif com == 'rg': # Set the colour space to DeviceRGB and set the colour # for filling paths. self.graphics_state["fill color space"] = "DeviceRGB" self.graphics_state["fill color"]["DeviceRGB"] = items[-3:] items = items[:-3] elif com == 'RG': # Set the colour space to DeviceRGB and set the colour # for path strokes. self.graphics_state["stroke color space"] = "DeviceRGB" self.graphics_state["stroke color"]["DeviceRGB"] = \ items[-3:] items = items[:-3] elif com == 'k': # Set the colour space to DeviceCMYK and set the colour # for filling paths. self.graphics_state["fill color space"] = "DeviceCMYK" self.graphics_state["fill color"]["DeviceCMYK"] = items[-4:] items = items[:-4] elif com == 'K': # Set the colour space to DeviceCMYK and set the colour # for path strokes. self.graphics_state["stroke color space"] = "DeviceCMYK" self.graphics_state["stroke color"]["DeviceCMYK"] = \ items[-4:] items = items[:-4] elif com == 'cs': # Set the colour space to use for filling paths. space = items.pop() # The "space" variable should contain a name object. self.graphics_state["fill color space"] = space.name elif com == 'CS': # Set the colour space to use for path strokes. space = items.pop() # The "space" variable should contain a name object. self.graphics_state["stroke color space"] = space.name elif com == 'sc': # Set the colour for filling paths. # The items list will be modified appropriately by the # following method. items = self.set_colour( items, self.graphics_state["fill color space"], "fill color" ) elif com == 'SC': # Set the colour for path strokes. # The items list will be modified appropriately by the # following method. items = self.set_colour( items, self.graphics_state["stroke color space"], "stroke color" ) elif com == 'scn': # Set the colour and/or pattern for filling paths. # Shorthand fcs_name = self.graphics_state["fill color space"] if fcs_name == "Pattern": # Read the last item on the stack. pattern_name = items.pop() # Find the pattern in the page's Resources dictionary. pattern = self.page.Resources[pattern_name] if pattern["PatternType"] == 1: if pattern["PaintType"] == 1: # No colour components should be specified. pass elif pattern["PaintType"] == 2: # Use the colour components to specify the # colour. items = self.set_colour( items, self.graphics_state["fill color space"], "fill color" ) elif pattern["PatternType"] == 2: # No colour components should be specified. pass elif fcs_name == "Separation": # Set the tint using a single colour component. # [Not implemented.] items.pop() elif fcs_name == "ICCBased": # Set the fill colour using the colour components # given. # [Not implemented.] pass else: # Fall back on the support method for the 'sc' # command. items = self.set_colour( items, self.graphics_state["fill color space"], "fill color" ) elif com == 'SCN': # Set the colour and/or pattern for path strokes. # Shorthand scs_name = self.graphics_state["stroke color space"] if scs_name == "Pattern": # Read the last item on the stack. pattern_name = items.pop() # Find the pattern in the page's Resources dictionary. pattern = self.page.Resources["Pattern"][pattern_name] if pattern["PatternType"] == 1: if pattern["PaintType"] == 1: # No colour components should be specified. pass elif pattern["PaintType"] == 2: # Use the colour components to specify the # colour. items = self.set_colour( items, self.graphics_state["fill color space"], "fill color" ) elif pattern["PatternType"] == 2: # No colour components should be specified. pass elif scs_name == "Separation": # Set the tint using a single colour component. # [Not implemented.] items.pop() elif scs_name == "ICCBased": # Set the stroke colour using the colour components # given. # [Not implemented.] pass else: # Fall back on the support method for the 'SC' # command. items = self.set_colour( items, self.graphics_state["stroke color space"], "stroke color" ) elif com == 'ri': # Colour rendering intent # [Not implemented (see p333 of PDF 1.3 specification).] items.pop() # Path objects elif com == 'm': # Move the current point to a new position. # (Start of a new path.) # Before reading this path, store the graphics state # in the contents list for renderers to use. #contents.append(GraphicsState(self.graphics_state)) # Read the path information (re-reading this command # and supplying the necessary operands). #operands = items[-2:] items, path, next = self.read_path(at, items) contents.append(path) #items = items[:-2] elif com == 're': # Draw a rectangle. # Before reading the rectangle, store the graphics state # in the contents list for renderers to use. #contents.append(GraphicsState(self.graphics_state)) # Read the path information (re-reading this command # and supplying the necessary operands). #operands = items[-4:] items, path, next = self.read_path(at, items) contents.append(path) #items = items[:-4] # Objects elif com == 'BT': # Text information is given. # Before reading this text, store the graphics state # in the contents list for renderers to use. #contents.append(GraphicsState(self.graphics_state)) #end = string.find(self.file, 'ET', at) # Read from the content following this command. text_contents, next = self.read_textobject(next, 'ET') contents.append(text_contents) #at = end + 2 elif com == 'BI': # In-line image object is given. # Read from the content following this command. end = string.find(self.file, 'EI', next) # self.read_inline_imageobject(at, end) next = end + 2 else: # print 'Command', com, 'not known.' pass else: # A command has not been found so this must be a parameter. items.append(item) # Skip any whitespace following the current offset into the content. at = self._skip_whitespace(next) # Return the contents found. return contents def generic_graphics_state(self, name): # Find the entry in the Resources dictionary corresponding to the # name supplied and set the appropriate parameter in the general # graphics state. # Find the "ExtGState" entry in the page's Resources dictionary. extgstate = self.page.Resources["ExtGState"] # Read a dictionary of parameters to change and their new values. # [If the name is not in the dictionary then a KeyError will be # raised. We may want to catch this.] changes = extgstate[name.name] add_state = {} for key, value in changes.items(): if key == 'SA': # Stroke adjustment add_state["stroke adjust"] = value elif key == 'OP': # Overprint for strokes add_state["stroke overprint"] = value elif key == 'op': # Overprint for fills add_state["fill overprint"] = value elif key == 'OPM': # Overprint mode add_state["overprint mode"] = value # Various missing commands from p328-329 of the PDF 1.3 # specification. elif key == 'SM': # Smoothness add_state["smoothness"] = value # Copy the contents of the add_state dictionary into the graphics # state, taking into account any restrictions on values. # Ensure that the fill overprint parameter is set if the stroke # overprint parameter is defined. if add_state.has_key("OP") and not add_state.has_key("op"): add_state["op"] = add_state["OP"] for key, value in add_state.items(): self.graphics_state[key] = value def set_colour(self, items, colour_space, colour_type): """set_colour(self, items, colour_space, colour_type) Set the colour (fill or stroke) in the colour space given using the relevant number of parameters from the items list. The items list will be modified by this method. """ # Shorthand if isinstance(colour_space, name): cs_name = colour_space.name else: cs_name = colour_space if cs_name == "DeviceGray": # Expect only one operand. self.graphics_state[colour_type][cs_name] = items.pop() elif cs_name == "CalGray": # Expect only one operand. self.graphics_state[colour_type][cs_name] = items.pop() elif cs_name == "Indexed": # Expect only one operand. self.graphics_state[colour_type][cs_name] = items.pop() elif cs_name == "DeviceRGB": # Expect three operands. self.graphics_state[colour_type][cs_name] = items[-3:] items = items[:-3] elif cs_name == "CalRGB": # Expect three operands. self.graphics_state[colour_type][cs_name] = items[-3:] items = items[:-3] elif cs_name == "Lab": # Expect three operands. self.graphics_state[colour_type][cs_name] = items[-3:] items = items[:-3] elif cs_name == "DeviceCMYK": # Expect four operands. self.graphics_state[colour_type][cs_name] = items[-4:] items = items[:-4] elif cs_name == "CalCMYK": # Expect four operands. self.graphics_state[colour_type][cs_name] = items[-4:] items = items[:-4] else: # Unknown colour space. self.graphics_state[colour_type][cs_name] = items items = [] return items def read_path(self, start, items): # Record the contents of the path. subpaths = [] # Skip initial whitespace and read the path elements. # Path segment operators at = self._skip_whitespace(start) while at < self.length: next, item = self._read_next(at, items) if isinstance(item, command): com = item.command if com == 'm': # Move the current point to a new position. # (Start of a new subpath.) self.current_point = point(*items[-2:]) items = items[:-2] move = pdfpath.Move(self.current_point) # Read the subpath information, adding the move operation # to the rest of that subpath. The items list is passed # because parameters may be found when reading the # subpath that will have to be used back at this level. items, subpath, next = self.read_subpath(next, move, items) # Append the subpath to the list of subpaths. subpaths.append(subpath) elif com == 're': # Draw a rectangle. p = point(*items[-4:-2]) width, height = items[-2:] items = items[:-4] # Add the rectangle to the list of subpaths. subpaths.append( pdfpath.Rectangle(p, width, height) ) else: # Not a list of path segments or a rectangle. # Leave the loop, maintaining the offset pointing # to this command. break else: # A command has not been found so this must be a parameter. items.append(item) # Skip any whitespace following the current offset into the content. at = self._skip_whitespace(next) #print "%i unused stack items" % len(items) # Path clipping operators clipping = [] while at < self.length: next, item = self._read_next(at, items) if isinstance(item, command): com = item.command if com == 'W': # Clip with the current path, using the non-zero winding # rule to determine the regions inside the clipping path. clipping.append( pdfpath.Clip("non-zero") ) elif com == 'W*': # Clip with the current path, using the even-odd winding # rule to determine the regions inside the clipping path. clipping.append(pdfpath.Clip("even-odd")) else: # Not a clipping operator/command. # Leave the loop, maintaining the offset pointing # to this command. break else: # A command has not been found so this must be a parameter. items.append(item) # Skip any whitespace following the current offset into the content. at = self._skip_whitespace(next) # Path painting operators painting = [] while at < self.length: next, item = self._read_next(at, items) if isinstance(item, command): com = item.command if com == 'n': # End the path. at = next break elif com == 'S': # Stroke the path. painting.append(pdfpath.Stroke()) at = next break elif com == 's': # Close then stroke the path. painting.append(pdfpath.Close()) painting.append(pdfpath.Stroke()) at = next break elif com == 'f': # Fill the path using the non-zero winding rule. painting.append(pdfpath.Fill("non-zero")) at = next break elif com == 'F': # Fill the path using the non-zero winding rule. painting.append(pdfpath.Fill("non-zero")) at = next break elif com == 'f*': # Fill the path using the even-odd winding rule. painting.append(pdfpath.Fill("even-odd")) at = next break elif com == 'B': # Fill and stroke the path using the non-zero winding rule. # Equivalent to q f Q S painting.append(pdfpath.Fill("non-zero")) painting.append(pdfpath.Stroke()) at = next break elif com == 'b': # Close, fill and stroke the path using the non-zero # winding rule. Equivalent to h B painting.append(pdfpath.Close()) painting.append(pdfpath.Fill("non-zero")) painting.append(pdfpath.Stroke()) at = next break elif com == 'B*': # Even-odd fill and stroke. # Equivalent to q f* Q S painting.append(pdfpath.Fill("even-odd")) painting.append(pdfpath.Stroke()) at = next break elif com == 'b*': # Close path, perform an even-odd fill and stroke. # Equivalent to h B* painting.append(pdfpath.Close()) painting.append(pdfpath.Fill("even-odd")) painting.append(pdfpath.Stroke()) at = next break elif com == 'sh': # Gradient fill using a Shading dictionary. name = items.pop() painting.append(pdfpath.Gradient(name)) at = next break else: # print 'Command', com, 'not known.' # Leave the loop, maintaining the offset pointing # to this command. break else: # A command has not been found so this must be a parameter. items.append(item) # Skip any whitespace following the current offset into the content. at = self._skip_whitespace(next) # Return the items found. return items, pdfpath.Path(subpaths, clipping, painting), at def read_subpath(self, start, initial, items): """subpath = read_subpath(self, start, initial) Return a subpath found in the contents starting at the offset given. The initial value is the operation which initiated this subpath. """ # Path segment operators # Record the contents of the subpath including the initial # operation/command which started the subpath. contents = [initial] at = self._skip_whitespace(start) while at < self.length: next, item = self._read_next(at, items) if isinstance(item, command): com = item.command if com == 'l': # Construct a straight line from the current point to the # new current point specified. new_point = point(*items[-2:]) contents.append( pdfpath.Line(self.current_point, new_point) ) # Set the new current point. self.current_point = new_point items = items[:-2] elif com == 'c': # Construct a cubic Bezier curve from the current point to # a new current point using two control points. c1 = point(*items[-6:-4]) c2 = point(*items[-4:-2]) new_point = point(*items[-2:]) contents.append( pdfpath.Bezier(self.current_point, c1, c2, new_point) ) # Set the new current point. self.current_point = new_point items = items[:-6] elif com == 'v': # Construct a cubic Bezier curve from the current point to # a new current point using the current point as the first # control point and the given control point as the second # control point. c1 = self.current_point c2 = point(*items[-4:-2]) new_point = point(*items[-2:]) contents.append( pdfpath.Bezier(self.current_point, c1, c2, new_point) ) # Set the new current point. self.current_point = new_point items = items[:-4] elif com == 'y': # Construct a cubic Bezier curve from the current point to # a new current point using the given control point as the # first control point and the current point as the second # control point. c1 = point(*items[-4:-2]) c2 = point(*items[-2:]) new_point = c2 contents.append( pdfpath.Bezier(self.current_point, c1, c2, new_point) ) # Set the new current point. self.current_point = new_point items = items[:-4] elif com == 'h': # Close the path. contents.append( pdfpath.Close() ) elif com == 'm': # A new subpath has been found. # Leave the loop, maintaining the offset pointing # to this command. break elif com == 're': # A rectangle has been found. # Leave the loop, maintaining the offset pointing # to this command. break else: # print 'Command', com, 'not known.' # Leave the loop, maintaining the offset pointing # to this command. break else: # A command has not been found so this must be a parameter. items.append(item) # Skip any whitespace following the current offset into the content. at = self._skip_whitespace(next) #print "%i unused stack items" % len(items) # Return the items found. return items, pdfpath.Subpath(contents), at def read_textobject(self, start, ending): """read_textobject(self, start, end) Implemented commands: Tf Tf Set font name and size Td Td Move to next line (displace following text) TD TD Move to next line (displace following text and set leading to -y) Tj Tj Write text ' ' Write text (equivalent to T* Tj) " Write text with character and word space attributes. Equivalent to Tw Tc ' TJ Tc Tc Text character spacing Tw Tw Word spacing Tz Tz Set horizontal scale to a percentage of its current value TL TL Set leading Tr Tr Set rendering mode Ts Ts Set text rise in text space units Tm Tm Define text matrix and text line matrix. Reset current point and line start position to the origin. T* T* Move to the start of the next line (equivalent to 0 Tl Td). """ items = [] self.text_character_spacing = 0 # T_c self.text_word_spacing = 0 # T_w self.text_horizontal_scale = 100 # T_h self.text_leading = 0 # T_l # self.text_font is initially undefined T_f # self.text_size is initially undefined T_fs self.text_rendering_mode = 0 # T_mode self.text_rise = 0 # T_rise # Define the text matrix - this transforms coordinates from text # space to user space. self.text_matrix = identity(3) # T_m self.line_matrix = identity(3) # T_LM self.text_rendering_matrix = identity(3) # T_RM self.text_line_start = self.origin # Record the contents of the text object. contents = [] at = self._skip_whitespace(start) while at < self.length: at, item = self._read_next(at, items) if isinstance(item, command): com = item.command if com == 'Tf': # Specify font Tf text_font, self.text_size = \ self.select_font(items[-2].name, items[-1]) items.pop() items.pop() self.text_font = pdftext.Font( text_font, self.text_size ) # Add the font details to the contents found. contents.append(self.text_font) elif com == 'Td': # Displace text (in points) tx, ty = items[-2:] items.pop() items.pop() self.text_matrix = self.move_text_to( tx, ty, self.text_matrix, self.line_matrix ) self.line_matrix = self.text_matrix.copy() self.text_line_start = self.text_line_start + point(tx, ty) self.current_point = self.text_line_start elif com == 'TD': # Move to the next line (equivalent to setting the # leading to the first value then displacing the text # by the following values). self.text_leading = -items[-1] tx, ty = items[-2:] items.pop() items.pop() self.text_matrix = self.move_text_to( tx, ty, self.text_matrix, self.line_matrix ) self.line_matrix = self.text_matrix.copy() self.text_line_start = self.text_line_start + point(tx, ty) self.current_point = self.current_point + point(tx, ty) elif com == 'Tj': # Write text text = items[-1] items.pop() # Calculate the text rendering matrix. self.text_rendering_matrix = \ self.calculate_rendering_matrix( self.text_size, self.text_horizontal_scale, self.text_rise, self.text_matrix, self.CTM ) # Create a text object. text_object = pdftext.Text( text, self.text_font, self.text_size, self.text_character_spacing, self.text_word_spacing, self.text_rendering_matrix, self.current_point ) # Add the text to the contents list. contents.append(text_object) # Update the current point using the relevant text object # method. self.current_point = \ self.current_point + text_object.after() elif com == "'": # Write text (equivalent to T* Tj) tx = 0 ty = self.text_leading self.text_matrix = self.move_text_to( tx, ty, self.text_matrix, self.line_matrix ) text = items[-1] items.pop() # Calculate the text rendering matrix. self.text_rendering_matrix = \ self.calculate_rendering_matrix( self.text_size, self.text_horizontal_scale, self.text_rise, self.text_matrix, self.CTM ) # Create a text object. text_object = pdftext.Text( text, self.text_font, self.text_size, self.text_character_spacing, self.text_word_spacing, self.text_rendering_matrix, self.current_point ) # Add the text to the contents list. contents.append(text_object) # Update the current point using the relevant text object # method. self.current_point = \ self.current_point + text_object.after() elif com == '"': # Write text with character and word space attributes. # (Equivalent to Tw Tc ') text = items.pop() self.text_character_spacing = items.pop() self.text_word_spacing = items.pop() tx = 0 ty = self.text_leading self.text_matrix = self.move_text_to( tx, ty, self.text_matrix, self.line_matrix ) # Calculate the text rendering matrix. self.text_rendering_matrix = \ self.calculate_rendering_matrix( self.text_size, self.text_horizontal_scale, self.text_rise, self.text_matrix, self.CTM ) # Create a text object. text_object = pdftext.Text( text, self.text_font, self.text_size, self.text_character_spacing, self.text_word_spacing, self.text_rendering_matrix, self.current_point ) # Add the text to the contents list. contents.append(text_object) # Update the current point using the relevant text object # method. self.current_point = \ self.current_point + text_object.after() elif com == 'TJ': # Show text string with individual character positioning. # TJ # Iterate through the items in the array, treating them # as individual text strings and position information. for item in items[-1]: if type(item) == types.StringType: # Text # Calculate the text rendering matrix. self.text_rendering_matrix = \ self.calculate_rendering_matrix( self.text_size, self.text_horizontal_scale, self.text_rise, self.text_matrix, self.CTM ) # Create a text object. text_object = pdftext.Text( item, self.text_font, self.text_size, self.text_character_spacing, self.text_word_spacing, self.text_rendering_matrix, self.current_point ) # Add the text to the contents list. contents.append(text_object) # Update the current point using the relevant text # object method. self.current_point = \ self.current_point + text_object.after() else: # Position information # Subtract the value from the coordinate used to # specify the position of words on a line for the # current writing direction; x for horizontal # text, y for vertical text. # User space coordinates correspond to points # and these units are in thousandths of an em, # so we have to divide the current text size by # a thousand and multiply it by this number. amount = item # * Assume horizontal writing for the moment. * self.current_point.x = self.current_point.x - ( self.text_size * amount / 1000.0 ) #self.position_text(items) #print "TJ ---" #print repr(items[-1]) #print "--- TJ" elif com == 'Tc': # Text character spacing self.text_character_spacing = items.pop() elif com == 'Tw': # Word spacing self.text_word_spacing = items.pop() elif com == 'Tz': # Horizontal scaling self.text_horizontal_scale = (items.pop() / 100.0) elif com == 'TL': # Leading self.text_leading = items.pop() elif com == 'Tr': # Rendering mode self.text_rendering_mode = items.pop() elif com == 'Ts': # Text rise self.text_rise = items.pop() elif com == 'Tm': # Set text and line matrices. self.text_matrix = matrix( [items[-6:-4]+[0], items[-4:-2]+[0], items[-2:]+[1]] ) items = items[:-6] self.line_matrix = self.text_matrix.copy() self.current_point = self.origin self.text_line_start = self.origin elif com == "T*": # Move to the next line using the leading parameter # as the vertical displacment. tx = 0 ty = self.text_leading self.text_matrix = self.move_text_to( tx, ty, self.text_matrix, self.line_matrix ) self.line_matrix = self.text_matrix.copy() self.text_line_start = self.text_line_start + point(tx, ty) self.current_point = self.current_point + point(tx, ty) elif com == ending: # The ending of the text object has been found. break else: pass # print 'Command', com, 'not known.' else: # A command has not been found so this must be a parameter. items.append(item) at = self._skip_whitespace(at) #print "%i unused stack items" % len(items) # Return the items found. return contents, at def move_text_to(self, tx, ty, text_matrix, line_matrix): text_matrix = matrix( [[1, 0, 0], [0, 1, 0], [tx, ty, 1]] ) * line_matrix return text_matrix def select_font(self, name, size): # Look in the resources dictionary for the font dictionary if self.page.Resources.has_key('Font') == 0: return -1 # Failed if self.page.Resources['Font'].has_key(name) == 0: return -1 text_font = self.page.Resources['Font'][name] text_size = size return text_font, text_size def calculate_rendering_matrix(self, T_fs, T_h, T_rise, T_m, CTM): # Construct a matrix to modify the text matrix. render_matrix = matrix([[T_fs * T_h, 0, 0],[0, T_fs, 0], [0, T_rise, 0]]) # Multiply the newly constructed matrix by the text matrix and the CTM. return render_matrix * (T_m * CTM) def read_text(self, objects = None, position = None, threshold = None): """read_text(self, objects = None, position = None, threshold = None) Read the contents of the PDFContents object and return a tuple containing all of the text found and the position of the final text object found on the page. None of the keyword arguments should be specified. """ if objects is None: objects = self.contents if position is None: position = point(0, 0) text = [] for obj in objects: if type(obj) == types.ListType: new_text, position = self.read_text(obj, position, threshold) text.append(new_text) elif isinstance(obj, pdftext.Text): if obj.position.x < position.x: text.append("\n") position = obj.position if obj.text != " ": dp = abs(obj.position - position) # Assume horizontal writing. if threshold is not None and dp > (threshold * obj.size): # Infer that a space could be inserted before this # piece of text. text.append(" " * int(dp/obj.size)) text.append(obj.text) position = obj.position return "".join(text), position if __name__ == '__main__': if len(sys.argv) > 1: doc = PDFDocument(sys.argv[1]) print 'Version', doc.document_version() #sys.exit() pdftools-0.37/pdftools/pdfpath.py0000644000076500001440000000544210767573571016171 0ustar davidusers# pdftools - A library of classes for parsing and rendering PDF documents. # Copyright (C) 2001-2008 by David Boddie # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library 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 # Library General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Created: 2003 """ pdfpath.py Classes for representing path information in PDF documents. Path state command support. """ class Path: def __init__(self, subpaths, clipping, painting): self.subpaths = subpaths self.clipping = clipping self.painting = painting class Subpath: def __init__(self, contents): self.contents = contents class Move: def __init__(self, point): self.point = point def __repr__(self): return "" % repr(self.point) class Line: def __init__(self, point1, point2): self.point1 = point1 self.point2 = point2 def __repr__(self): return "" % ( repr(self.point1), repr(self.point2) ) class Bezier: def __init__(self, point1, control1, control2, point2): self.point1 = point1 self.control1 = control1 self.control2 = control2 self.point2 = point2 def __repr__(self): return "" % ( repr(self.point1), repr(self.point2), repr(self.control1), repr(self.control2) ) class Rectangle: def __init__(self, point, width, height): self.point = point self.width = width self.height = height def __repr__(self): return "" % ( repr(self.point), self.width, self.height ) class Close: pass class Clip: def __init__(self, winding): self.winding = winding def __repr__(self): return "" % self.winding # Painting operations class Stroke: pass class Fill: def __init__(self, winding): self.winding = winding def __repr__(self): return "" % self.winding class Gradient: def __init__(self, name): self.name = name pdftools-0.37/pdftools/pdftext.py0000644000076500001440000000762210767573601016215 0ustar davidusers# pdftools - A library of classes for parsing and rendering PDF documents. # Copyright (C) 2001-2008 by David Boddie # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library 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 # Library General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Created: 2003 """ pdftext.py Classes for representing textual information in PDF documents. Text state command support. """ # Import required items from the pdftools module. from pdfdefs import point try: from PyQt4.QtGui import QFontDatabase, QFontMetricsF with_metrics = True except ImportError: with_metrics = False class Font: def __init__(self, font, size): self.font = font self.size = size def _width_without_metrics(self, text): # Return the width of this piece of text when rendered using this # font. # * To be replaced: return a value for a monospaced font. * return len(text) * self.size def _width_with_metrics(self, text): # Return the width of this piece of text when rendered using this # font. fontName = self.font["BaseFont"].name at = fontName.find("+") if at != -1: fontName = fontName[at+1:] if "-" in fontName: family, style = fontName.split("-")[:2] elif " " in fontName: family, style = fontName.split(" ")[:2] elif "," in fontName: family, style = fontName.split(",")[:2] else: family = fontName style = "" font = QFontDatabase().font(family, style, self.size) font.setPointSizeF(self.size) fm = QFontMetricsF(font) return fm.width(text) def width(self, text, use_metrics = False): if use_metrics and with_metrics: return self._width_with_metrics(text) else: return self._width_without_metrics(text) class Text: def __init__(self, text, font, size, character_spacing, word_spacing, rendering_matrix, position): self.text = text # Keep a reference to the font dictionary used. self.font = font self.size = size self.character_spacing = character_spacing self.word_spacing = word_spacing self.rendering_matrix = rendering_matrix.copy() self.position = position # This is temporary #sys.stdout.write(text) def after(self): # Calculate the location of the current point after the text has # been placed using the position given as the initial location of the # current point. # We must first determine the direction of the text. # * Assume horizontal text. * # We examine each character in turn, calculating its width or # height, depending on the writing direction, then apply the # character spacing correction to this length value. # If a space character is found then the word spacing correction is # additionally applied to the length found for the space. # Finally, the total displacement of the current point is added to # its initial value to determine its new value. return point(self.font.width(self.text) + self.character_spacing, 0) pdftools-0.37/LICENSE0000664000076500007650000004310310713361770013264 0ustar daviddavid GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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 of the License, 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. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. pdftools-0.37/MANIFEST0000644000076500001440000000021210767600627013447 0ustar davidusersLICENSE MANIFEST README.txt setup.py pdftools/__init__.py pdftools/pdfdefs.py pdftools/pdffile.py pdftools/pdfpath.py pdftools/pdftext.py pdftools-0.37/README.txt0000664000076500007650000000375310767600617013771 0ustar daviddavid======== pdftools ======== Introduction ------------ pdftools is a library of classes for parsing and rendering PDF documents. Installation ------------ Installation is optional as long as the directory containing the pdftools package is included in the list of paths stored in the PYTHONPATH environment variable. To install the pdftools module, enter the following at the command line from within the directory unpacked from the archive: python setup.py install You may need to be the root user to install the package. Font Metrics ------------ pdftools will automatically import the PyQt4 package, if available, so that information about font metrics can be obtained if requested by the user. If the PyQt4 package is not installed, this information will not be available. If the PyQt4 package is available and you need to obtain font metrics, you must first ensure that a QApplication instance has been created. This can be achieved by adding the following lines of code to your application: from PyQt4.QtGui import QApplication app = QApplication([]) Please see the documentation for PyQt4 for more information about QApplication. The mechanism used to automatically import PyQt4 and use Qt classes may change in future versions of this package. License ------- Copyright (C) 2001-2008 by David Boddie This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. pdftools-0.37/setup.py0000755000076500001440000000061610145445323014031 0ustar davidusers#! /usr/bin/env python from distutils.core import setup import pdftools setup( name = "pdftools", description = "PDF document reading classes", author = "David Boddie", author_email = "david@boddie.org.uk", url = "http://www.boddie.org.uk/david/Projects/Python/pdftools", version = pdftools.__version__, packages = ["pdftools"] ) pdftools-0.37/PKG-INFO0000664000076500007650000000041311004732617013345 0ustar daviddavidMetadata-Version: 1.0 Name: pdftools Version: 0.37 Summary: PDF document reading classes Home-page: http://www.boddie.org.uk/david/Projects/Python/pdftools Author: David Boddie Author-email: david@boddie.org.uk License: UNKNOWN Description: UNKNOWN Platform: UNKNOWN