pdfreader 0.1.14__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pdfreader/__init__.py +8 -0
- pdfreader/buffer.py +188 -0
- pdfreader/codecs/__init__.py +13 -0
- pdfreader/codecs/agl.py +4289 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-0 +134 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-3 +151 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-4 +152 -0
- pdfreader/codecs/cmaps/Adobe-GB1-0 +109 -0
- pdfreader/codecs/cmaps/Adobe-GB1-2 +165 -0
- pdfreader/codecs/cmaps/Adobe-GB1-4 +195 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-1 +111 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-2 +113 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-4 +138 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-5 +157 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-0 +114 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-1 +149 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-2 +149 -0
- pdfreader/codecs/cmaps/Identity-H +337 -0
- pdfreader/codecs/cmaps/README.md +1 -0
- pdfreader/codecs/codec.py +137 -0
- pdfreader/codecs/decoder.py +195 -0
- pdfreader/codecs/differences.py +75 -0
- pdfreader/codecs/macroman.py +69 -0
- pdfreader/codecs/pdfdoc.py +78 -0
- pdfreader/codecs/standard.py +50 -0
- pdfreader/codecs/tests.py +22 -0
- pdfreader/codecs/winansi.py +72 -0
- pdfreader/codecs/zapfdingbatsgl.py +205 -0
- pdfreader/constants.py +128 -0
- pdfreader/document.py +169 -0
- pdfreader/exceptions.py +12 -0
- pdfreader/filters/__init__.py +20 -0
- pdfreader/filters/ascii85.py +38 -0
- pdfreader/filters/asciihex.py +54 -0
- pdfreader/filters/ccittfax.py +554 -0
- pdfreader/filters/crypt.py +5 -0
- pdfreader/filters/dct.py +5 -0
- pdfreader/filters/flate.py +32 -0
- pdfreader/filters/jbig2.py +5 -0
- pdfreader/filters/jpx.py +5 -0
- pdfreader/filters/lzw.py +304 -0
- pdfreader/filters/predictors.py +33 -0
- pdfreader/filters/runlength.py +55 -0
- pdfreader/filters/test_ccittf_fax.py +144 -0
- pdfreader/filters/tests.py +22 -0
- pdfreader/parsers/__init__.py +4 -0
- pdfreader/parsers/base.py +741 -0
- pdfreader/parsers/cmap.py +268 -0
- pdfreader/parsers/content.py +88 -0
- pdfreader/parsers/document.py +627 -0
- pdfreader/parsers/inlineimage.py +73 -0
- pdfreader/parsers/objstm.py +16 -0
- pdfreader/parsers/tests.py +24 -0
- pdfreader/pillow.py +113 -0
- pdfreader/registry.py +48 -0
- pdfreader/securityhandler.py +333 -0
- pdfreader/tests.py +18 -0
- pdfreader/types/__init__.py +7 -0
- pdfreader/types/cmap.py +395 -0
- pdfreader/types/constants.py +235 -0
- pdfreader/types/content.py +92 -0
- pdfreader/types/filestructure.py +52 -0
- pdfreader/types/native.py +198 -0
- pdfreader/types/objects.py +523 -0
- pdfreader/types/tests.py +20 -0
- pdfreader/types/xref.py +165 -0
- pdfreader/utils.py +57 -0
- pdfreader/viewer/__init__.py +5 -0
- pdfreader/viewer/canvas.py +61 -0
- pdfreader/viewer/graphicsstate.py +99 -0
- pdfreader/viewer/pdfviewer.py +331 -0
- pdfreader/viewer/resources.py +64 -0
- pdfreader/viewer/simple.py +233 -0
- pdfreader/viewer/tests.py +20 -0
- pdfreader-0.1.14.dist-info/LICENSE +21 -0
- pdfreader-0.1.14.dist-info/METADATA +121 -0
- pdfreader-0.1.14.dist-info/RECORD +79 -0
- pdfreader-0.1.14.dist-info/WHEEL +5 -0
- pdfreader-0.1.14.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
from ..pillow import PILImageMixin
|
|
2
|
+
from .native import Stream, Dictionary, Array
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class StartXRef(object):
|
|
6
|
+
""" startxref
|
|
7
|
+
123
|
|
8
|
+
|
|
9
|
+
Pseudo object. Can be between indirect objects if any incremental updates.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def __init__(self, offset):
|
|
13
|
+
self.offset = offset
|
|
14
|
+
|
|
15
|
+
def __repr__(self):
|
|
16
|
+
return "<StartXRef: {}>".format(self.offset)
|
|
17
|
+
|
|
18
|
+
def __eq__(self, other):
|
|
19
|
+
return self.offset == other.offset
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Trailer(object):
|
|
23
|
+
""" trailer
|
|
24
|
+
<<< ... .>>
|
|
25
|
+
endtrailer
|
|
26
|
+
|
|
27
|
+
Pseudo object. Can be between indirect objects if any incremental updates.
|
|
28
|
+
"""
|
|
29
|
+
def __init__(self, params):
|
|
30
|
+
self.params = params
|
|
31
|
+
|
|
32
|
+
def __repr__(self):
|
|
33
|
+
return "<Trailer: {}>".format(self.params)
|
|
34
|
+
|
|
35
|
+
def __eq__(self, other):
|
|
36
|
+
return self.params == other.params
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class StreamBasedObject(Stream):
|
|
40
|
+
""" Stream-based object.
|
|
41
|
+
Automatically resolves indirect references on attributes access """
|
|
42
|
+
|
|
43
|
+
def __init__(self, doc, stream):
|
|
44
|
+
super(StreamBasedObject, self).__init__(stream.dictionary, stream.stream)
|
|
45
|
+
self.doc = doc
|
|
46
|
+
self._cache = {}
|
|
47
|
+
|
|
48
|
+
@classmethod
|
|
49
|
+
def from_stream(cls, other):
|
|
50
|
+
obj = super(StreamBasedObject, StreamBasedObject).from_stream(other)
|
|
51
|
+
obj.doc = other.doc
|
|
52
|
+
|
|
53
|
+
def __getattr__(self, item):
|
|
54
|
+
if item in self._cache:
|
|
55
|
+
return self._cache[item]
|
|
56
|
+
obj = super(StreamBasedObject, self).__getattr__(item)
|
|
57
|
+
obj = self.doc.build(obj, lazy=True)
|
|
58
|
+
self._cache[item] = obj
|
|
59
|
+
return self._cache[item]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class ArrayBasedObject(Array):
|
|
63
|
+
""" Array-based object.
|
|
64
|
+
Automatically resolves indirect references on items access """
|
|
65
|
+
|
|
66
|
+
def __init__(self, doc, lst):
|
|
67
|
+
self.doc = doc
|
|
68
|
+
# ToDo: this must be lazier thing.
|
|
69
|
+
# Need to build objects only on access attempt only. For example: a.index, a[i], a[1:2] etc.
|
|
70
|
+
lst = [self.doc.build(obj, lazy=True) for obj in lst]
|
|
71
|
+
super(ArrayBasedObject, self).__init__(lst)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class DictBasedObject(Dictionary):
|
|
75
|
+
""" Dictionary-based object.
|
|
76
|
+
Automatically resolves indirect references on attributes/items access """
|
|
77
|
+
|
|
78
|
+
def __init__(self, doc, *args, **kwargs):
|
|
79
|
+
super(DictBasedObject, self).__init__(*args, **kwargs)
|
|
80
|
+
self.doc = doc
|
|
81
|
+
self._cache = {}
|
|
82
|
+
|
|
83
|
+
def __getattr__(self, item):
|
|
84
|
+
return self.get(item)
|
|
85
|
+
|
|
86
|
+
def __getitem__(self, item):
|
|
87
|
+
if item in self._cache:
|
|
88
|
+
return self._cache[item]
|
|
89
|
+
obj = super(DictBasedObject, self).__getitem__(item)
|
|
90
|
+
obj = self.doc.build(obj, lazy=True)
|
|
91
|
+
self._cache[item] = obj
|
|
92
|
+
return self._cache[item]
|
|
93
|
+
|
|
94
|
+
def __delitem__(self, item): # real signature unknown
|
|
95
|
+
""" Delete self[key]. """
|
|
96
|
+
del self._cache[item]
|
|
97
|
+
|
|
98
|
+
def get(self, item, default=None):
|
|
99
|
+
try:
|
|
100
|
+
val = self[item]
|
|
101
|
+
except KeyError:
|
|
102
|
+
val = default
|
|
103
|
+
return val
|
|
104
|
+
|
|
105
|
+
# override defaults to build Dictionary values before returning
|
|
106
|
+
|
|
107
|
+
def keys(self):
|
|
108
|
+
return [k for k in super(DictBasedObject, self).keys()]
|
|
109
|
+
|
|
110
|
+
def values(self):
|
|
111
|
+
return [self[k] for k in super(DictBasedObject, self).keys()]
|
|
112
|
+
|
|
113
|
+
def items(self):
|
|
114
|
+
return [(k, self[k]) for k in super(DictBasedObject, self).keys()]
|
|
115
|
+
|
|
116
|
+
def pop(self, i, **kwargs):
|
|
117
|
+
if i in self:
|
|
118
|
+
res = self[i]
|
|
119
|
+
_ = super(DictBasedObject, self).pop(i)
|
|
120
|
+
else:
|
|
121
|
+
res = super(DictBasedObject, self).pop(i, **kwargs)
|
|
122
|
+
return res
|
|
123
|
+
|
|
124
|
+
def popitem(self):
|
|
125
|
+
if not self:
|
|
126
|
+
raise KeyError()
|
|
127
|
+
k = next(iter(super(DictBasedObject, self).keys()))
|
|
128
|
+
return k, self.pop(k)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def obj_factory(doc, obj):
|
|
132
|
+
klass = None
|
|
133
|
+
if isinstance(obj, Stream):
|
|
134
|
+
if obj.Type == 'XObject':
|
|
135
|
+
klass = XOBJECTS.get(obj.Subtype, XObject)
|
|
136
|
+
else:
|
|
137
|
+
klass = STREAM_BASED_OBJECTS.get(obj.Type, StreamBasedObject)
|
|
138
|
+
elif isinstance(obj, Dictionary):
|
|
139
|
+
klass = DICT_OBJECTS.get(obj.get('Type'), DictBasedObject)
|
|
140
|
+
elif isinstance(obj, list):
|
|
141
|
+
klass = ArrayBasedObject
|
|
142
|
+
|
|
143
|
+
return klass(doc, obj) if klass else obj
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
class ObjectStream(StreamBasedObject):
|
|
147
|
+
""" Type = ObjStm
|
|
148
|
+
"""
|
|
149
|
+
pass
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class Catalog(DictBasedObject):
|
|
153
|
+
"""
|
|
154
|
+
Dictionary based object. (Type = Catalog)
|
|
155
|
+
See PDF 1.7 specification `sec. 7.7.2 - DocumentCatalog <https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=71>`_
|
|
156
|
+
"""
|
|
157
|
+
pass
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class PageTreeNode(DictBasedObject):
|
|
161
|
+
"""
|
|
162
|
+
Dictionary based object. (Type = Pages)
|
|
163
|
+
See PDF 1.7 specification `sec. 7.7.3.2 - Page Tree Nodes <https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=76>`_
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
def pages(self, node=None):
|
|
167
|
+
"""
|
|
168
|
+
Yields tree node pages one by one.
|
|
169
|
+
|
|
170
|
+
:return: :class:`~pdfreader.types.objects.Page` generator.
|
|
171
|
+
"""
|
|
172
|
+
if node is None:
|
|
173
|
+
node = self
|
|
174
|
+
for child in node.Kids:
|
|
175
|
+
if isinstance(child, Page):
|
|
176
|
+
yield child
|
|
177
|
+
else:
|
|
178
|
+
for page in self.pages(child):
|
|
179
|
+
yield page
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
class Page(DictBasedObject):
|
|
183
|
+
"""
|
|
184
|
+
Dictionary based Page object. (Type = Page)
|
|
185
|
+
See PDF 1.7 specification `sec. 7.7.3.3 - Page Objects <https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=77>`_
|
|
186
|
+
"""
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class XObject(StreamBasedObject):
|
|
190
|
+
"""
|
|
191
|
+
Stream based XObject object. (Type = XObject)
|
|
192
|
+
See PDF 1.7 specification `sec. 8.8 - External Objects <https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=201>`_
|
|
193
|
+
"""
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
class CMap(StreamBasedObject):
|
|
197
|
+
""" Type = CMap
|
|
198
|
+
"""
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class Metadata(StreamBasedObject):
|
|
202
|
+
""" Type = Metadata
|
|
203
|
+
Subtype = XML
|
|
204
|
+
"""
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
class Image(PILImageMixin, XObject):
|
|
208
|
+
"""
|
|
209
|
+
Stream based XObject object. (Type = XObject, Subtype = Image)
|
|
210
|
+
See PDF 1.7 specification `sec. 8.9 - Images <https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=203>`_
|
|
211
|
+
"""
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
class Form(XObject):
|
|
215
|
+
"""
|
|
216
|
+
Stream based XObject object. (Type = XObject, Subtype = Form)
|
|
217
|
+
See PDF 1.7 specification `sec. 8.10 - Form XObjects <https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=217>`_
|
|
218
|
+
"""
|
|
219
|
+
|
|
220
|
+
stream_content = XObject.filtered
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
class Group(XObject):
|
|
224
|
+
""" Type = XObject
|
|
225
|
+
Subtype = Group
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
class OCG(DictBasedObject):
|
|
230
|
+
""" Type = OCG
|
|
231
|
+
Optional Content Group
|
|
232
|
+
"""
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class OCMD(DictBasedObject):
|
|
236
|
+
""" Type = OCMD
|
|
237
|
+
Optional Content Membership Directory
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
class Font(DictBasedObject):
|
|
242
|
+
""" Type = Font
|
|
243
|
+
"""
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
class Encoding(DictBasedObject):
|
|
247
|
+
""" Type = Encoding
|
|
248
|
+
"""
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class CIDFont(DictBasedObject):
|
|
252
|
+
""" Type = CIDFont
|
|
253
|
+
"""
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class FontDescriptior(DictBasedObject):
|
|
257
|
+
""" Type = FontDescriptior
|
|
258
|
+
"""
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
class Halftone(DictBasedObject):
|
|
262
|
+
""" Type = Halftone
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
class Outlines(DictBasedObject):
|
|
267
|
+
""" Type = Outlines
|
|
268
|
+
"""
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
class Collection(DictBasedObject):
|
|
272
|
+
""" Type = Collection
|
|
273
|
+
"""
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
class CollectionField(DictBasedObject):
|
|
277
|
+
""" Type = CollectionField
|
|
278
|
+
"""
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
class CollectionSort(DictBasedObject):
|
|
282
|
+
""" Type = CollectionSort
|
|
283
|
+
"""
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
class CollectionSchema(DictBasedObject):
|
|
287
|
+
""" Type = CollectionSchema
|
|
288
|
+
"""
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
class PageLabel(DictBasedObject):
|
|
292
|
+
""" Type = PageLabel
|
|
293
|
+
"""
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
class Bread(DictBasedObject):
|
|
297
|
+
""" Type = Bread
|
|
298
|
+
"""
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
class Thread(DictBasedObject):
|
|
302
|
+
""" Type = Thread
|
|
303
|
+
"""
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class Trans(DictBasedObject):
|
|
307
|
+
""" Type = Trans
|
|
308
|
+
"""
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
class NavNode(DictBasedObject):
|
|
312
|
+
""" Type = NavNode
|
|
313
|
+
"""
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
class Annot(DictBasedObject):
|
|
317
|
+
""" Type = Annot
|
|
318
|
+
"""
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
class Border(DictBasedObject):
|
|
322
|
+
""" Type = Border
|
|
323
|
+
"""
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
class Action(DictBasedObject):
|
|
327
|
+
""" Type = Action
|
|
328
|
+
"""
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
class Sig(DictBasedObject):
|
|
332
|
+
""" Type = Sig
|
|
333
|
+
"""
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
class SigRef(DictBasedObject):
|
|
337
|
+
""" Type = SigRef
|
|
338
|
+
"""
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
class TransformParams(DictBasedObject):
|
|
342
|
+
""" Type = TransformParams
|
|
343
|
+
"""
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
class Requirement(DictBasedObject):
|
|
347
|
+
""" Type = Requirement
|
|
348
|
+
"""
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
class ReqHandler(DictBasedObject):
|
|
352
|
+
""" Type = ReqHandler
|
|
353
|
+
"""
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
class Rendition(DictBasedObject):
|
|
357
|
+
""" Type = Rendition
|
|
358
|
+
"""
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
class MediaCriteria(DictBasedObject):
|
|
362
|
+
""" Type = MediaCriteria
|
|
363
|
+
"""
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
class MinBitDepth(DictBasedObject):
|
|
367
|
+
""" Type = MinBitDepth
|
|
368
|
+
"""
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
class MinScreenSize(DictBasedObject):
|
|
372
|
+
""" Type = MinScreenSize
|
|
373
|
+
"""
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
class MediaClip(DictBasedObject):
|
|
377
|
+
""" Type = MediaClip
|
|
378
|
+
"""
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
class MediaPermissions(DictBasedObject):
|
|
382
|
+
""" Type = MediaPermissions
|
|
383
|
+
"""
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
class MediaDuration(DictBasedObject):
|
|
387
|
+
""" Type = MediaDuration
|
|
388
|
+
"""
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
class MediaScreenParams(DictBasedObject):
|
|
392
|
+
""" Type = MediaScreenParams
|
|
393
|
+
"""
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
class FWParams(DictBasedObject):
|
|
397
|
+
""" Type = FWParams
|
|
398
|
+
"""
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
class MediaOffset(DictBasedObject):
|
|
402
|
+
""" Type = MediaOffset
|
|
403
|
+
"""
|
|
404
|
+
|
|
405
|
+
class Timespan(DictBasedObject):
|
|
406
|
+
""" Type = Timespan
|
|
407
|
+
"""
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
class MediaPlayers(DictBasedObject):
|
|
411
|
+
""" Type = MediaPlayers
|
|
412
|
+
"""
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
class MediaPlayerInfo(DictBasedObject):
|
|
416
|
+
""" Type = MediaPlayerInfo
|
|
417
|
+
"""
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
class SoftwareIdentifier(DictBasedObject):
|
|
421
|
+
""" Type = SoftwareIdentifier
|
|
422
|
+
"""
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
class Sound(DictBasedObject):
|
|
426
|
+
""" Type = Sound
|
|
427
|
+
"""
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
class SlideShow(DictBasedObject):
|
|
431
|
+
""" Type = SlideShow
|
|
432
|
+
"""
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
class LastModified(DictBasedObject):
|
|
436
|
+
""" Type = LastModified
|
|
437
|
+
"""
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
class StructTreeRoot(DictBasedObject):
|
|
441
|
+
""" Type = StructTreeRoot
|
|
442
|
+
"""
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
class StructElem(DictBasedObject):
|
|
446
|
+
""" Type = StructElem
|
|
447
|
+
"""
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
class MCR(DictBasedObject):
|
|
451
|
+
""" Type = MCR
|
|
452
|
+
Marked Content Reference
|
|
453
|
+
"""
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
class OBJR(DictBasedObject):
|
|
457
|
+
""" Type = OBJR
|
|
458
|
+
Object Reference
|
|
459
|
+
"""
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
STREAM_BASED_OBJECTS = {
|
|
463
|
+
'ObjectStream': ObjectStream,
|
|
464
|
+
'XObject': XObject,
|
|
465
|
+
'CMap': CMap,
|
|
466
|
+
'Metadata': Metadata
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
XOBJECTS = {'Image': Image,
|
|
470
|
+
'Form': Form,
|
|
471
|
+
'Group': Group}
|
|
472
|
+
|
|
473
|
+
DICT_OBJECTS = {
|
|
474
|
+
'Catalog': Catalog,
|
|
475
|
+
'Pages': PageTreeNode,
|
|
476
|
+
'Page': Page,
|
|
477
|
+
'OCG': OCG,
|
|
478
|
+
'OCMD': OCMD,
|
|
479
|
+
'Font': Font,
|
|
480
|
+
'Encoding': Encoding,
|
|
481
|
+
'CIDFont': CIDFont,
|
|
482
|
+
'FontDescriptior': FontDescriptior,
|
|
483
|
+
'Halftone': Halftone,
|
|
484
|
+
'Outlines': Outlines,
|
|
485
|
+
'Collection': Collection,
|
|
486
|
+
'CollectionField': CollectionField,
|
|
487
|
+
'CollectionSort': CollectionSort,
|
|
488
|
+
'CollectionSchema': CollectionSchema,
|
|
489
|
+
'PageLabel': PageLabel,
|
|
490
|
+
'Bread': Bread,
|
|
491
|
+
'Thread': Thread,
|
|
492
|
+
'Trans': Trans,
|
|
493
|
+
'NavNode': NavNode,
|
|
494
|
+
'Annot': Annot,
|
|
495
|
+
'Border': Border,
|
|
496
|
+
'Action': Action,
|
|
497
|
+
'Sig': Sig,
|
|
498
|
+
'SigRef': SigRef,
|
|
499
|
+
'TransformParams': TransformParams,
|
|
500
|
+
'Requirement': Requirement,
|
|
501
|
+
'ReqHandler': ReqHandler,
|
|
502
|
+
'Rendition': Rendition,
|
|
503
|
+
'MediaCriteria': MediaCriteria,
|
|
504
|
+
'MinBitDepth': MinBitDepth,
|
|
505
|
+
'MinScreenSize': MinScreenSize,
|
|
506
|
+
'MediaClip': MediaClip,
|
|
507
|
+
'MediaPermissions': MediaPermissions,
|
|
508
|
+
'MediaDuration': MediaDuration,
|
|
509
|
+
'MediaScreenParams': MediaScreenParams,
|
|
510
|
+
'FWParams': FWParams,
|
|
511
|
+
'MediaOffset': MediaOffset,
|
|
512
|
+
'Timespan': Timespan,
|
|
513
|
+
'MediaPlayers': MediaPlayers,
|
|
514
|
+
'MediaPlayerInfo': MediaPlayerInfo,
|
|
515
|
+
'SoftwareIdentifier': SoftwareIdentifier,
|
|
516
|
+
'Sound': Sound,
|
|
517
|
+
'SlideShow': SlideShow,
|
|
518
|
+
'LastModified': LastModified,
|
|
519
|
+
'StructTreeRoot': StructTreeRoot,
|
|
520
|
+
'StructElem': StructElem,
|
|
521
|
+
'MCR': MCR,
|
|
522
|
+
'OBJR': OBJR
|
|
523
|
+
}
|
pdfreader/types/tests.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
import doctest
|
|
3
|
+
|
|
4
|
+
from . import cmap
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def suite():
|
|
8
|
+
suite = unittest.TestSuite()
|
|
9
|
+
suite.addTests(doctest.DocTestSuite(cmap))
|
|
10
|
+
return suite
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def load_tests(loader, tests, ignore):
|
|
14
|
+
tests.addTests(suite())
|
|
15
|
+
return tests
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
if __name__ == '__main__':
|
|
19
|
+
runner = unittest.TextTestRunner()
|
|
20
|
+
runner.run(suite())
|
pdfreader/types/xref.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
log = logging.getLogger(__name__)
|
|
3
|
+
|
|
4
|
+
from collections import OrderedDict
|
|
5
|
+
|
|
6
|
+
MAX_GEN = 65535
|
|
7
|
+
|
|
8
|
+
TYPE_FREE = 0
|
|
9
|
+
TYPE_IN_USE = 1
|
|
10
|
+
TYPE_COMPRESSED = 2
|
|
11
|
+
KNOWN_TYPES = (TYPE_COMPRESSED, TYPE_IN_USE, TYPE_FREE)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BaseXrefEntry(object):
|
|
15
|
+
|
|
16
|
+
def __init__(self, number, generation=0, typ=None):
|
|
17
|
+
assert isinstance(number, int) and number >= 0
|
|
18
|
+
assert isinstance(generation, int) and generation >= 0
|
|
19
|
+
|
|
20
|
+
if generation > MAX_GEN:
|
|
21
|
+
log.warning("Incorrect generation {} for entry {}".format(generation, number))
|
|
22
|
+
|
|
23
|
+
self.number = number
|
|
24
|
+
self.generation = generation
|
|
25
|
+
self.type = typ
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def is_in_use(self):
|
|
29
|
+
return self.type == TYPE_IN_USE
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def is_free(self):
|
|
33
|
+
return self.type == TYPE_FREE
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def is_compressed(self):
|
|
37
|
+
return self.type == TYPE_COMPRESSED
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def is_unknown(self):
|
|
41
|
+
return self.type not in KNOWN_TYPES
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class XRefEntry(BaseXrefEntry):
|
|
45
|
+
""" Regular Xref entry. Simple trailer table entry.
|
|
46
|
+
No compressed objects support
|
|
47
|
+
"""
|
|
48
|
+
def __init__(self, offset, number, generation, typ):
|
|
49
|
+
if not (isinstance(offset, int) and offset >=0):
|
|
50
|
+
raise AssertionError
|
|
51
|
+
self.offset = offset
|
|
52
|
+
if typ == 'n':
|
|
53
|
+
typ = TYPE_IN_USE
|
|
54
|
+
elif typ == 'f':
|
|
55
|
+
typ = TYPE_FREE
|
|
56
|
+
super(XRefEntry, self).__init__(number, generation, typ)
|
|
57
|
+
|
|
58
|
+
def __repr__(self):
|
|
59
|
+
return "<XRefEntry:number={s.number},generation={s.generation},offset={s.offset},type={s.type}>".format(s=self)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class CompressedObjEntry(BaseXrefEntry):
|
|
63
|
+
""" Entry in compressed objects
|
|
64
|
+
Generation is always 0 for compressed objects.
|
|
65
|
+
"""
|
|
66
|
+
def __init__(self, number, index):
|
|
67
|
+
if not (isinstance(index, int) and index >= 0):
|
|
68
|
+
raise AssertionError
|
|
69
|
+
self.index = index
|
|
70
|
+
super(CompressedObjEntry, self).__init__(number, typ=TYPE_COMPRESSED)
|
|
71
|
+
|
|
72
|
+
def __repr__(self):
|
|
73
|
+
return "<XRefEntry(compressed):number={s.number},generation={s.generation},index={s.index},type={s.type}>"\
|
|
74
|
+
.format(s=self)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class XRef(object):
|
|
78
|
+
""" xref - Cross reference entry """
|
|
79
|
+
|
|
80
|
+
def __init__(self):
|
|
81
|
+
# According to the specification free objexts must be implemented as a linked list
|
|
82
|
+
# But we don't care about that as we don't want to update PDFs. Just read.
|
|
83
|
+
self.free = OrderedDict()
|
|
84
|
+
self.in_use = OrderedDict()
|
|
85
|
+
self.compressed = OrderedDict()
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def __len__(self):
|
|
89
|
+
return len(self.free) + len(self.in_use) + len(self.compressed)
|
|
90
|
+
|
|
91
|
+
def add_entry(self, entry):
|
|
92
|
+
if entry.is_compressed:
|
|
93
|
+
self.compressed[entry.number] = entry
|
|
94
|
+
elif entry.is_free:
|
|
95
|
+
self.free[entry.number] = entry
|
|
96
|
+
elif entry.is_in_use:
|
|
97
|
+
self.in_use[entry.number] = entry
|
|
98
|
+
|
|
99
|
+
def merge(self, other):
|
|
100
|
+
for xre in other.free.values():
|
|
101
|
+
self.add_entry(xre)
|
|
102
|
+
for xre in other.in_use.values():
|
|
103
|
+
self.add_entry(xre)
|
|
104
|
+
for xre in other.free.values():
|
|
105
|
+
self.add_entry(xre)
|
|
106
|
+
for xre in other.compressed.values():
|
|
107
|
+
self.add_entry(xre)
|
|
108
|
+
|
|
109
|
+
def __repr__(self):
|
|
110
|
+
return "<XRef:free={},in_use={},compressed={}>".format(len(self.free), len(self.in_use), len(self.compressed))
|
|
111
|
+
|
|
112
|
+
@classmethod
|
|
113
|
+
def from_stream(cls, stream):
|
|
114
|
+
self = cls()
|
|
115
|
+
self.compressed = {}
|
|
116
|
+
self._stream = stream # Just for debugging, normally we don't need this.
|
|
117
|
+
|
|
118
|
+
if stream.get("Index"):
|
|
119
|
+
# ToDo: data/f8ben.pdf contains index of [226, 1, 250, 1, 299, 2, 302, 3]
|
|
120
|
+
# ToDo: and the only meaningfull in_use entry. PDF 1.7 says here must be just 2 values.
|
|
121
|
+
# ToDo: this must be interpreded some way. Need to investigate
|
|
122
|
+
first_object, n_entries = stream["Index"][0:2]
|
|
123
|
+
else:
|
|
124
|
+
first_object, n_entries = 0, stream["Size"]
|
|
125
|
+
colsizes = stream["W"]
|
|
126
|
+
colsoffsets = [0, colsizes[0], colsizes[0] + colsizes[1]]
|
|
127
|
+
rowsize = sum(colsizes)
|
|
128
|
+
data = stream.filtered
|
|
129
|
+
|
|
130
|
+
# walk rows
|
|
131
|
+
objnum = first_object
|
|
132
|
+
for i in range(0, len(data), rowsize):
|
|
133
|
+
row = data[i:i+rowsize]
|
|
134
|
+
cols, entry = [], None
|
|
135
|
+
# extract columns
|
|
136
|
+
for j in range(3):
|
|
137
|
+
if colsizes[j] == 0:
|
|
138
|
+
# if colsize is 0 it means it's just absent
|
|
139
|
+
# 0 - default
|
|
140
|
+
v = 0
|
|
141
|
+
else:
|
|
142
|
+
b = colsoffsets[j]
|
|
143
|
+
e = b + colsizes[j]
|
|
144
|
+
v = int.from_bytes(row[b:e], "big")
|
|
145
|
+
cols.append(v)
|
|
146
|
+
|
|
147
|
+
if cols[0] == 0:
|
|
148
|
+
# free entry
|
|
149
|
+
entry = XRefEntry(number=cols[1], generation=cols[2], offset=0, typ=TYPE_FREE)
|
|
150
|
+
elif cols[0] == 1:
|
|
151
|
+
# used entry
|
|
152
|
+
entry = XRefEntry(number=objnum, generation=cols[2], offset=cols[1], typ=TYPE_IN_USE)
|
|
153
|
+
elif cols[0] == 2:
|
|
154
|
+
# compressed object
|
|
155
|
+
entry = CompressedObjEntry(number=cols[1], index=cols[2])
|
|
156
|
+
else:
|
|
157
|
+
# PDF 1.5-1.7 defines any other reference types as references to null objects.
|
|
158
|
+
# so we just skip those
|
|
159
|
+
log.debug("Undefined xref col type (index 0): {}".format(cols))
|
|
160
|
+
|
|
161
|
+
if entry is not None:
|
|
162
|
+
self.add_entry(entry)
|
|
163
|
+
objnum += 1
|
|
164
|
+
|
|
165
|
+
return self
|