1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
# A part of pdfrw (https://github.com/pmaupin/pdfrw)
# Copyright (C) 2006-2015 Patrick Maupin, Austin, Texas
# Copyright (C) 2012-2015 Nerijus Mika
# MIT license -- See LICENSE.txt for details
'''
The PdfReader class reads an entire PDF file into memory and
parses the top-level container objects. (It does not parse
into streams.) The object subclasses PdfDict, and the
document pages are stored in a list in the pages attribute
of the object.
'''
import gc
import binascii
import collections
import itertools
from .errors import PdfParseError, log
from .tokens import PdfTokens
from .objects import PdfDict, PdfArray, PdfName, PdfObject, PdfIndirect
from .uncompress import uncompress
from .py23_diffs import convert_load, iteritems
class PdfReader(PdfDict):
def findindirect(self, objnum, gennum, PdfIndirect=PdfIndirect, int=int):
''' Return a previously loaded indirect object, or create
a placeholder for it.
'''
key = int(objnum), int(gennum)
result = self.indirect_objects.get(key)
if result is None:
self.indirect_objects[key] = result = PdfIndirect(key)
self.deferred_objects.add(key)
result._loader = self.loadindirect
return result
def readarray(self, source, PdfArray=PdfArray):
''' Found a [ token. Parse the tokens after that.
'''
specialget = self.special.get
result = []
pop = result.pop
append = result.append
for value in source:
if value in ']R':
if value == ']':
break
generation = pop()
value = self.findindirect(pop(), generation)
else:
func = specialget(value)
if func is not None:
value = func(source)
append(value)
return PdfArray(result)
def readdict(self, source, PdfDict=PdfDict):
''' Found a << token. Parse the tokens after that.
'''
specialget = self.special.get
result = PdfDict()
next = source.next
tok = next()
while tok != '>>':
if not tok.startswith('/'):
source.error('Expected PDF /name object')
tok = next()
continue
key = tok
value = next()
func = specialget(value)
if func is not None:
value = func(source)
tok = next()
else:
tok = next()
if value.isdigit() and tok.isdigit():
tok2 = next()
if tok2 != 'R':
source.error('Expected "R" following two integers')
tok = tok2
continue
value = self.findindirect(value, tok)
tok = next()
result[key] = value
return result
def empty_obj(self, source, PdfObject=PdfObject):
''' Some silly git put an empty object in the
file. Back up so the caller sees the endobj.
'''
source.floc = source.tokstart
def badtoken(self, source):
''' Didn't see that coming.
'''
source.exception('Unexpected delimiter')
def findstream(self, obj, tok, source, len=len):
''' Figure out if there is a content stream
following an object, and return the start
pointer to the content stream if so.
(We can't read it yet, because we might not
know how long it is, because Length might
be an indirect object.)
'''
fdata = source.fdata
startstream = source.tokstart + len(tok)
gotcr = fdata[startstream] == '\r'
startstream += gotcr
gotlf = fdata[startstream] == '\n'
startstream += gotlf
if not gotlf:
if not gotcr:
source.error(r'stream keyword not followed by \n')
else:
source.warning(r"stream keyword terminated "
r"by \r without \n")
return startstream
def readstream(self, obj, startstream, source, exact_required=False,
streamending='endstream endobj'.split(), int=int):
fdata = source.fdata
length = int(obj.Length)
source.floc = target_endstream = startstream + length
endit = source.multiple(2)
obj._stream = fdata[startstream:target_endstream]
if endit == streamending:
return
if exact_required:
source.exception('Expected endstream endobj')
# The length attribute does not match the distance between the
# stream and endstream keywords.
# TODO: Extract maxstream from dictionary of object offsets
# and use rfind instead of find.
maxstream = len(fdata) - 20
endstream = fdata.find('endstream', startstream, maxstream)
source.floc = startstream
room = endstream - startstream
if endstream < 0:
source.error('Could not find endstream')
return
if (length == room + 1 and
fdata[startstream - 2:startstream] == '\r\n'):
source.warning(r"stream keyword terminated by \r without \n")
obj._stream = fdata[startstream - 1:target_endstream - 1]
return
source.floc = endstream
if length > room:
source.error('stream /Length attribute (%d) appears to '
'be too big (size %d) -- adjusting',
length, room)
obj.stream = fdata[startstream:endstream]
return
if fdata[target_endstream:endstream].rstrip():
source.error('stream /Length attribute (%d) appears to '
'be too small (size %d) -- adjusting',
length, room)
obj.stream = fdata[startstream:endstream]
return
endobj = fdata.find('endobj', endstream, maxstream)
if endobj < 0:
source.error('Could not find endobj after endstream')
return
if fdata[endstream:endobj].rstrip() != 'endstream':
source.error('Unexpected data between endstream and endobj')
return
source.error('Illegal endstream/endobj combination')
def loadindirect(self, key, PdfDict=PdfDict,
isinstance=isinstance):
result = self.indirect_objects.get(key)
if not isinstance(result, PdfIndirect):
return result
source = self.source
offset = int(self.source.obj_offsets.get(key, '0'))
if not offset:
source.warning("Did not find PDF object %s", key)
return None
# Read the object header and validate it
objnum, gennum = key
source.floc = offset
objid = source.multiple(3)
ok = len(objid) == 3
ok = ok and objid[0].isdigit() and int(objid[0]) == objnum
ok = ok and objid[1].isdigit() and int(objid[1]) == gennum
ok = ok and objid[2] == 'obj'
if not ok:
source.floc = offset
source.next()
objheader = '%d %d obj' % (objnum, gennum)
fdata = source.fdata
offset2 = (fdata.find('\n' + objheader) + 1 or
fdata.find('\r' + objheader) + 1)
if (not offset2 or
fdata.find(fdata[offset2 - 1] + objheader, offset2) > 0):
source.warning("Expected indirect object '%s'", objheader)
return None
source.warning("Indirect object %s found at incorrect "
"offset %d (expected offset %d)",
objheader, offset2, offset)
source.floc = offset2 + len(objheader)
# Read the object, and call special code if it starts
# an array or dictionary
obj = source.next()
func = self.special.get(obj)
if func is not None:
obj = func(source)
self.indirect_objects[key] = obj
self.deferred_objects.remove(key)
# Mark the object as indirect, and
# just return it if it is a simple object.
obj.indirect = key
tok = source.next()
if tok == 'endobj':
return obj
# Should be a stream. Either that or it's broken.
isdict = isinstance(obj, PdfDict)
if isdict and tok == 'stream':
self.readstream(obj, self.findstream(obj, tok, source), source)
return obj
# Houston, we have a problem, but let's see if it
# is easily fixable. Leaving out a space before endobj
# is apparently an easy mistake to make on generation
# (Because it won't be noticed unless you are specifically
# generating an indirect object that doesn't end with any
# sort of delimiter.) It is so common that things like
# okular just handle it.
if isinstance(obj, PdfObject) and obj.endswith('endobj'):
source.error('No space or delimiter before endobj')
obj = PdfObject(obj[:-6])
else:
source.error("Expected 'endobj'%s token",
isdict and " or 'stream'" or '')
obj = PdfObject('')
obj.indirect = key
self.indirect_objects[key] = obj
return obj
def read_all(self):
deferred = self.deferred_objects
prev = set()
while 1:
new = deferred - prev
if not new:
break
prev |= deferred
for key in new:
self.loadindirect(key)
def uncompress(self):
self.read_all()
uncompress(self.indirect_objects.values())
def load_stream_objects(self, object_streams):
# read object streams
objs = []
for num in object_streams:
obj = self.findindirect(num, 0).real_value()
assert obj.Type == '/ObjStm'
objs.append(obj)
# read objects from stream
if objs:
uncompress(objs)
for obj in objs:
objsource = PdfTokens(obj.stream, 0, False)
snext = objsource.next
offsets = {}
firstoffset = int(obj.First)
num = snext()
while num.isdigit():
offset = int(snext())
offsets[int(num)] = firstoffset + offset
num = snext()
for num, offset in iteritems(offsets):
# Read the object, and call special code if it starts
# an array or dictionary
objsource.floc = offset
sobj = snext()
func = self.special.get(sobj)
if func is not None:
sobj = func(objsource)
key = (num, 0)
self.indirect_objects[key] = sobj
if key in self.deferred_objects:
self.deferred_objects.remove(key)
# Mark the object as indirect, and
# add it to the list of streams if it starts a stream
sobj.indirect = key
def findxref(self, fdata):
''' Find the cross reference section at the end of a file
'''
startloc = fdata.rfind('startxref')
if startloc < 0:
raise PdfParseError('Did not find "startxref" at end of file')
source = PdfTokens(fdata, startloc, False, self.verbose)
tok = source.next()
assert tok == 'startxref' # (We just checked this...)
tableloc = source.next_default()
if not tableloc.isdigit():
source.exception('Expected table location')
if source.next_default().rstrip().lstrip('%') != 'EOF':
source.exception('Expected %%EOF')
return startloc, PdfTokens(fdata, int(tableloc), True, self.verbose)
def parse_xref_stream(self, source, int=int, range=range,
enumerate=enumerate, islice=itertools.islice,
defaultdict=collections.defaultdict,
hexlify=binascii.hexlify):
''' Parse (one of) the cross-reference file section(s)
'''
def readint(s, lengths):
lengths = itertools.cycle(lengths)
offset = 0
for length in itertools.cycle(lengths):
next = offset + length
yield int(hexlify(s[offset:next]), 16) if length else None
offset = next
setdefault = source.obj_offsets.setdefault
next = source.next
# check for xref stream object
objid = source.multiple(3)
ok = len(objid) == 3
ok = ok and objid[0].isdigit()
ok = ok and objid[1] == 'obj'
ok = ok and objid[2] == '<<'
if not ok:
source.exception('Expected xref stream start')
obj = self.readdict(source)
if obj.Type != PdfName.XRef:
source.exception('Expected dict type of /XRef')
tok = next()
self.readstream(obj, self.findstream(obj, tok, source), source, True)
if not uncompress([obj], True):
source.exception('Could not decompress Xref stream')
num_pairs = obj.Index or PdfArray(['0', obj.Size])
num_pairs = [int(x) for x in num_pairs]
num_pairs = zip(num_pairs[0::2], num_pairs[1::2])
entry_sizes = [int(x) for x in obj.W]
if len(entry_sizes) != 3:
source.exception('Invalid entry size')
object_streams = defaultdict(list)
get = readint(obj.stream, entry_sizes)
for objnum, size in num_pairs:
for cnt in range(size):
xtype, p1, p2 = islice(get, 3)
if xtype in (1, None):
if p1:
setdefault((objnum, p2 or 0), p1)
elif xtype == 2:
object_streams[p1].append((objnum, p2))
objnum += 1
obj.private.object_streams = object_streams
return obj
def parse_xref_table(self, source, int=int, range=range):
''' Parse (one of) the cross-reference file section(s)
'''
setdefault = source.obj_offsets.setdefault
next = source.next
# plain xref table
start = source.floc
try:
while 1:
tok = next()
if tok == 'trailer':
return
startobj = int(tok)
for objnum in range(startobj, startobj + int(next())):
offset = int(next())
generation = int(next())
inuse = next()
if inuse == 'n':
if offset != 0:
setdefault((objnum, generation), offset)
elif inuse != 'f':
raise ValueError
except:
pass
try:
# Table formatted incorrectly.
# See if we can figure it out anyway.
end = source.fdata.rindex('trailer', start)
table = source.fdata[start:end].splitlines()
for line in table:
tokens = line.split()
if len(tokens) == 2:
objnum = int(tokens[0])
elif len(tokens) == 3:
offset, generation, inuse = (int(tokens[0]),
int(tokens[1]), tokens[2])
if offset != 0 and inuse == 'n':
setdefault((objnum, generation), offset)
objnum += 1
elif tokens:
log.error('Invalid line in xref table: %s' %
repr(line))
raise ValueError
log.warning('Badly formatted xref table')
source.floc = end
next()
except:
source.floc = start
source.exception('Invalid table format')
def parsexref(self, source):
''' Parse (one of) the cross-reference file section(s)
'''
next = source.next
tok = next()
if tok.isdigit():
return self.parse_xref_stream(source), True
elif tok == 'xref':
self.parse_xref_table(source)
tok = next()
if tok != '<<':
source.exception('Expected "<<" starting catalog')
return self.readdict(source), False
else:
source.exception('Expected "xref" keyword or xref stream object')
def readpages(self, node):
pagename = PdfName.Page
pagesname = PdfName.Pages
catalogname = PdfName.Catalog
typename = PdfName.Type
kidname = PdfName.Kids
# PDFs can have arbitrarily nested Pages/Page
# dictionary structures.
def readnode(node):
nodetype = node[typename]
if nodetype == pagename:
yield node
elif nodetype == pagesname:
for node in node[kidname]:
for node in readnode(node):
yield node
elif nodetype == catalogname:
for node in readnode(node[pagesname]):
yield node
else:
log.error('Expected /Page or /Pages dictionary, got %s' %
repr(node))
try:
return list(readnode(node))
except (AttributeError, TypeError) as s:
log.error('Invalid page tree: %s' % s)
return []
def __init__(self, fname=None, fdata=None, decompress=False,
disable_gc=True, verbose=True):
self.private.verbose = verbose
# Runs a lot faster with GC off.
disable_gc = disable_gc and gc.isenabled()
if disable_gc:
gc.disable()
try:
if fname is not None:
assert fdata is None
# Allow reading preexisting streams like pyPdf
if hasattr(fname, 'read'):
fdata = fname.read()
else:
try:
f = open(fname, 'rb')
fdata = f.read()
f.close()
except IOError:
raise PdfParseError('Could not read PDF file %s' %
fname)
fdata = convert_load(fdata)
assert fdata is not None
if not fdata.startswith('%PDF-'):
startloc = fdata.find('%PDF-')
if startloc >= 0:
log.warning('PDF header not at beginning of file')
else:
lines = fdata.lstrip().splitlines()
if not lines:
raise PdfParseError('Empty PDF file!')
raise PdfParseError('Invalid PDF header: %s' %
repr(lines[0]))
self.private.version = fdata[5:8]
endloc = fdata.rfind('%EOF')
if endloc < 0:
raise PdfParseError('EOF mark not found: %s' %
repr(fdata[-20:]))
endloc += 6
junk = fdata[endloc:]
fdata = fdata[:endloc]
if junk.rstrip('\00').strip():
log.warning('Extra data at end of file')
private = self.private
private.indirect_objects = {}
private.deferred_objects = set()
private.special = {'<<': self.readdict,
'[': self.readarray,
'endobj': self.empty_obj,
}
for tok in r'\ ( ) < > { } ] >> %'.split():
self.special[tok] = self.badtoken
startloc, source = self.findxref(fdata)
private.source = source
# Find all the xref tables/streams, and
# then deal with them backwards.
xref_list = []
while 1:
source.obj_offsets = {}
trailer, is_stream = self.parsexref(source)
prev = trailer.Prev
if prev is None:
token = source.next()
if token != 'startxref' and not xref_list:
source.warning('Expected "startxref" '
'at end of xref table')
break
xref_list.append((source.obj_offsets, trailer, is_stream))
source.floc = int(prev)
if is_stream:
self.load_stream_objects(trailer.object_streams)
while xref_list:
later_offsets, later_trailer, is_stream = xref_list.pop()
source.obj_offsets.update(later_offsets)
if is_stream:
trailer.update(later_trailer)
self.load_stream_objects(later_trailer.object_streams)
else:
trailer = later_trailer
trailer.Prev = None
if (trailer.Version and
float(trailer.Version) > float(self.version)):
self.private.version = trailer.Version
if is_stream:
self.Root = trailer.Root
self.Info = trailer.Info
self.ID = trailer.ID
self.Size = trailer.Size
self.Encrypt = trailer.Encrypt
else:
self.update(trailer)
# self.read_all_indirect(source)
private.pages = self.readpages(self.Root)
if decompress:
self.uncompress()
# For compatibility with pyPdf
private.numPages = len(self.pages)
finally:
if disable_gc:
gc.enable()
# For compatibility with pyPdf
def getPage(self, pagenum):
return self.pages[pagenum]
|