2013-10-17 14:05:27 +00:00
|
|
|
#!/usr/bin/env python
|
2013-10-10 10:54:55 +00:00
|
|
|
import sys
|
|
|
|
from psparser import LIT, KWD, STRICT
|
|
|
|
from pdftypes import PDFObjectNotFound
|
|
|
|
from pdftypes import resolve1
|
|
|
|
from pdftypes import int_value, float_value, num_value
|
|
|
|
from pdftypes import str_value, list_value, dict_value, stream_value
|
|
|
|
|
|
|
|
|
|
|
|
# some predefined literals and keywords.
|
|
|
|
LITERAL_PAGE = LIT('Page')
|
|
|
|
LITERAL_PAGES = LIT('Pages')
|
|
|
|
|
|
|
|
|
|
|
|
## PDFPage
|
|
|
|
##
|
|
|
|
class PDFPage(object):
|
|
|
|
|
|
|
|
"""An object that holds the information about a page.
|
|
|
|
|
|
|
|
A PDFPage object is merely a convenience class that has a set
|
|
|
|
of keys and values, which describe the properties of a page
|
|
|
|
and point to its contents.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
doc: a PDFDocument object.
|
|
|
|
pageid: any Python object that can uniquely identify the page.
|
|
|
|
attrs: a dictionary of page attributes.
|
|
|
|
contents: a list of PDFStream objects that represents the page content.
|
|
|
|
lastmod: the last modified time of the page.
|
|
|
|
resources: a list of resources used by the page.
|
|
|
|
mediabox: the physical size of the page.
|
|
|
|
cropbox: the crop rectangle of the page.
|
|
|
|
rotate: the page rotation (in degree).
|
|
|
|
annots: the page annotations.
|
|
|
|
beads: a chain that represents natural reading order.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, doc, pageid, attrs):
|
|
|
|
"""Initialize a page object.
|
|
|
|
|
|
|
|
doc: a PDFDocument object.
|
|
|
|
pageid: any Python object that can uniquely identify the page.
|
|
|
|
attrs: a dictionary of page attributes.
|
|
|
|
"""
|
|
|
|
self.doc = doc
|
|
|
|
self.pageid = pageid
|
|
|
|
self.attrs = dict_value(attrs)
|
|
|
|
self.lastmod = resolve1(self.attrs.get('LastModified'))
|
|
|
|
self.resources = resolve1(self.attrs['Resources'])
|
|
|
|
self.mediabox = resolve1(self.attrs['MediaBox'])
|
|
|
|
if 'CropBox' in self.attrs:
|
|
|
|
self.cropbox = resolve1(self.attrs['CropBox'])
|
|
|
|
else:
|
|
|
|
self.cropbox = self.mediabox
|
|
|
|
self.rotate = (self.attrs.get('Rotate', 0)+360) % 360
|
|
|
|
self.annots = self.attrs.get('Annots')
|
|
|
|
self.beads = self.attrs.get('B')
|
|
|
|
if 'Contents' in self.attrs:
|
|
|
|
contents = resolve1(self.attrs['Contents'])
|
|
|
|
else:
|
|
|
|
contents = []
|
|
|
|
if not isinstance(contents, list):
|
|
|
|
contents = [ contents ]
|
|
|
|
self.contents = contents
|
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<PDFPage: Resources=%r, MediaBox=%r>' % (self.resources, self.mediabox)
|
|
|
|
|
|
|
|
INHERITABLE_ATTRS = set(['Resources', 'MediaBox', 'CropBox', 'Rotate'])
|
|
|
|
@classmethod
|
|
|
|
def create_pages(klass, document, debug=0):
|
|
|
|
def search(obj, parent):
|
|
|
|
if isinstance(obj, int):
|
|
|
|
objid = obj
|
|
|
|
tree = dict_value(document.getobj(objid)).copy()
|
|
|
|
else:
|
|
|
|
objid = obj.objid
|
|
|
|
tree = dict_value(obj).copy()
|
|
|
|
for (k,v) in parent.iteritems():
|
|
|
|
if k in klass.INHERITABLE_ATTRS and k not in tree:
|
|
|
|
tree[k] = v
|
|
|
|
if tree.get('Type') is LITERAL_PAGES and 'Kids' in tree:
|
|
|
|
if 1 <= debug:
|
|
|
|
print >>sys.stderr, 'Pages: Kids=%r' % tree['Kids']
|
|
|
|
for c in list_value(tree['Kids']):
|
|
|
|
for x in search(c, tree):
|
|
|
|
yield x
|
|
|
|
elif tree.get('Type') is LITERAL_PAGE:
|
|
|
|
if 1 <= debug:
|
|
|
|
print >>sys.stderr, 'Page: %r' % tree
|
|
|
|
yield (objid, tree)
|
|
|
|
pages = False
|
|
|
|
if 'Pages' in document.catalog:
|
|
|
|
for (objid,tree) in search(document.catalog['Pages'], document.catalog):
|
|
|
|
yield klass(document, objid, tree)
|
|
|
|
pages = True
|
|
|
|
if not pages:
|
|
|
|
# fallback when /Pages is missing.
|
|
|
|
for xref in document.xrefs:
|
|
|
|
for objid in xref.get_objids():
|
|
|
|
try:
|
|
|
|
obj = document.getobj(objid)
|
|
|
|
if isinstance(obj, dict) and obj.get('Type') is LITERAL_PAGE:
|
|
|
|
yield klass(document, objid, obj)
|
|
|
|
except PDFObjectNotFound:
|
|
|
|
pass
|
|
|
|
return
|