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,92 @@
|
|
|
1
|
+
from ..pillow import PILImageMixin
|
|
2
|
+
from ..types.native import apply_filter_multi
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class StreamContent(object):
|
|
6
|
+
|
|
7
|
+
@property
|
|
8
|
+
def as_source_code(self):
|
|
9
|
+
""" Shall return a string containing meaningful PDF source for the object """
|
|
10
|
+
raise NotImplementedError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class InlineImage(PILImageMixin, StreamContent):
|
|
14
|
+
""" BI/ID/EI operators content.
|
|
15
|
+
|
|
16
|
+
Inline image looks like a stream-based object but really it is not.
|
|
17
|
+
We just follow Stream interface to have an option to interact with InlineImage
|
|
18
|
+
the same way as with XObject/Image
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
#: key-value image properties
|
|
22
|
+
dictionary = None
|
|
23
|
+
#: bytes, encoded image stream
|
|
24
|
+
data = None
|
|
25
|
+
|
|
26
|
+
def __init__(self, entries, data):
|
|
27
|
+
self.dictionary = entries
|
|
28
|
+
self.data = data
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def Filter(self):
|
|
32
|
+
return self.dictionary.get('Filter') or self.dictionary.get('F')
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def Width(self):
|
|
36
|
+
return self.dictionary.get('Width') or self.dictionary.get('W')
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def Height(self):
|
|
40
|
+
return self.dictionary.get('Height') or self.dictionary.get('H')
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def ColorSpace(self):
|
|
44
|
+
return self.dictionary.get('ColorSpace') or self.dictionary.get('CS')
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def BitsPerComponent(self):
|
|
48
|
+
return self.dictionary.get('BitsPerComponent') or self.dictionary.get('BPC')
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def Decode(self):
|
|
52
|
+
return self.dictionary.get('Decode') or self.dictionary.get('D')
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def DecodeParms(self):
|
|
56
|
+
return self.dictionary.get('DecodeParms') or self.dictionary.get('DP')
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def Intent(self):
|
|
60
|
+
return self.dictionary.get('Intent')
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def Interpolate(self):
|
|
64
|
+
return self.dictionary.get('Interpolate') or self.dictionary.get('I')
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def ImageMask(self):
|
|
68
|
+
return self.dictionary.get('ImageMask') or self.dictionary.get('IM')
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def filtered(self):
|
|
72
|
+
""" :return: bytes, decoded image stream as it defined by image properties """
|
|
73
|
+
return apply_filter_multi(self.Filter, self.data, self.DecodeParms)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class Operator(StreamContent):
|
|
77
|
+
""" Page content stream operator. For example: */F01 12 Tf*
|
|
78
|
+
|
|
79
|
+
"""
|
|
80
|
+
#: operator name
|
|
81
|
+
name = None
|
|
82
|
+
#: list of operands
|
|
83
|
+
args = None
|
|
84
|
+
|
|
85
|
+
def __init__(self, name, args):
|
|
86
|
+
self.name = name
|
|
87
|
+
self.args = args
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
if __name__ == "__main__":
|
|
91
|
+
import doctest
|
|
92
|
+
doctest.testmod()
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
|
|
2
|
+
class PDFHeader(object):
|
|
3
|
+
""" %PDF1.3 """
|
|
4
|
+
def __init__(self, version, offset=None):
|
|
5
|
+
self.version = version
|
|
6
|
+
self.major, self.minor = self.version.split(".")
|
|
7
|
+
self.offset = offset
|
|
8
|
+
|
|
9
|
+
def __repr__(self):
|
|
10
|
+
return "<PDF Header:v={self.version} (major={self.major}, minor={self.minor}), offset={self.offset})>"\
|
|
11
|
+
.format(self=self)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PDFTrailer(object):
|
|
15
|
+
"""
|
|
16
|
+
trailer
|
|
17
|
+
<< /Size 22 .... >>
|
|
18
|
+
startxref
|
|
19
|
+
16354
|
|
20
|
+
%%EOF
|
|
21
|
+
|
|
22
|
+
or a trailer from a stream
|
|
23
|
+
"""
|
|
24
|
+
def __init__(self, xrefs, **kwargs):
|
|
25
|
+
self.xrefs = xrefs
|
|
26
|
+
self.params = kwargs
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def root(self):
|
|
30
|
+
""" Root """
|
|
31
|
+
return self.params["Root"]
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def encrypt(self):
|
|
35
|
+
""" Document encryptio dictionary if exists """
|
|
36
|
+
return self.params.get("Encrypt")
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def info(self):
|
|
40
|
+
""" Document info dictionary if exists """
|
|
41
|
+
return self.params.get("Info")
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def id(self):
|
|
45
|
+
""" Array of two strings containing a file identifier if exists """
|
|
46
|
+
return self.params.get("ID") or self.params.get("Id")
|
|
47
|
+
|
|
48
|
+
def __repr__(self):
|
|
49
|
+
return "<PDF Trailer:xrefs={self.xrefs},root={self.root},info={self.info}>".format(self=self)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
log = logging.getLogger(__name__)
|
|
3
|
+
|
|
4
|
+
from decimal import Decimal
|
|
5
|
+
|
|
6
|
+
from ..constants import DEFAULT_ENCODING
|
|
7
|
+
from ..filters import apply_filter
|
|
8
|
+
from ..utils import cached_property
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
null = None
|
|
12
|
+
Boolean = bool
|
|
13
|
+
Integer = int
|
|
14
|
+
Real = Decimal
|
|
15
|
+
Array = list
|
|
16
|
+
Dictionary = dict
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class String(bytes):
|
|
20
|
+
""" A string object shall consist of a series of zero or more bytes """
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Name(str):
|
|
24
|
+
""" Name type: /SomeName """
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class HexString(str):
|
|
28
|
+
""" Hexadecimal string: <AF20FA> """
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def as_int(self):
|
|
32
|
+
return int(self, 16)
|
|
33
|
+
|
|
34
|
+
def to_string(self):
|
|
35
|
+
return self.to_bytes().decode(DEFAULT_ENCODING)
|
|
36
|
+
|
|
37
|
+
def to_bytes(self):
|
|
38
|
+
return bytes.fromhex(self)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def apply_filter_multi(filters, binary, params):
|
|
42
|
+
if not filters:
|
|
43
|
+
return binary
|
|
44
|
+
|
|
45
|
+
if isinstance(filters, Array):
|
|
46
|
+
farr = filters
|
|
47
|
+
elif isinstance(filters, Name):
|
|
48
|
+
farr = Array()
|
|
49
|
+
farr.append(filters)
|
|
50
|
+
else:
|
|
51
|
+
raise TypeError("Incorrect filter type: {}".format(filters))
|
|
52
|
+
|
|
53
|
+
filters_applied = []
|
|
54
|
+
for fname in farr:
|
|
55
|
+
try:
|
|
56
|
+
binary = apply_filter(fname, binary, params)
|
|
57
|
+
filters_applied.append(fname)
|
|
58
|
+
except NotImplementedError:
|
|
59
|
+
log.exception("Partially decoded. Filters applied: {}".format(filters_applied))
|
|
60
|
+
raise
|
|
61
|
+
|
|
62
|
+
return binary
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class Stream(object):
|
|
66
|
+
""" binary stream: dictionary and binary data
|
|
67
|
+
common keys:
|
|
68
|
+
Length - integer (required)
|
|
69
|
+
Filter - name or array
|
|
70
|
+
DecodeParms - dict or array
|
|
71
|
+
F - file specification
|
|
72
|
+
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
def __init__(self, info_dict, binary_stream):
|
|
76
|
+
if not isinstance(info_dict, Dictionary):
|
|
77
|
+
raise AssertionError
|
|
78
|
+
if not isinstance(binary_stream, bytes):
|
|
79
|
+
raise AssertionError
|
|
80
|
+
|
|
81
|
+
if "Length" not in info_dict:
|
|
82
|
+
raise KeyError("Missing stream length")
|
|
83
|
+
|
|
84
|
+
if info_dict["Length"] != len(binary_stream):
|
|
85
|
+
log.warning("Inconsistent stream: defined length {}, real length {}"
|
|
86
|
+
.format(info_dict["Length"], len(binary_stream)))
|
|
87
|
+
|
|
88
|
+
self.dictionary = info_dict
|
|
89
|
+
self.stream = binary_stream
|
|
90
|
+
|
|
91
|
+
def __getitem__(self, item):
|
|
92
|
+
return self.dictionary.__getitem__(item)
|
|
93
|
+
|
|
94
|
+
def get(self, item, default=None):
|
|
95
|
+
return self.dictionary.get(item, default)
|
|
96
|
+
|
|
97
|
+
def __len__(self):
|
|
98
|
+
return len(self.stream)
|
|
99
|
+
|
|
100
|
+
def __repr__(self):
|
|
101
|
+
data = self.stream
|
|
102
|
+
if len(data) > 25:
|
|
103
|
+
data = (self.stream[:25] + b' ...')
|
|
104
|
+
return "<Stream:len={},data={}>".format(self.dictionary["Length"], repr(data))
|
|
105
|
+
|
|
106
|
+
def type(self):
|
|
107
|
+
return self.get('Type')
|
|
108
|
+
|
|
109
|
+
@cached_property
|
|
110
|
+
def filtered(self):
|
|
111
|
+
""" :return: bytes, decoded image stream as it defined by image properties """
|
|
112
|
+
return apply_filter_multi(self.get('Filter'),
|
|
113
|
+
self.stream,
|
|
114
|
+
self.dictionary.get("DecodeParms"))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def __eq__(self, other):
|
|
118
|
+
return self.dictionary == other.dictionary and self.stream == other.stream
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def from_stream(cls, other):
|
|
122
|
+
return cls(other.dictionary, other.stream)
|
|
123
|
+
|
|
124
|
+
def __getattr__(self, item):
|
|
125
|
+
return self.dictionary.get(item)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class Comment(str):
|
|
129
|
+
""" % Some PDF Comment """
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class IndirectReference(object):
|
|
133
|
+
def __init__(self, number, generation):
|
|
134
|
+
""" 10 0 R """
|
|
135
|
+
if not isinstance(number, int):
|
|
136
|
+
raise AssertionError
|
|
137
|
+
if not (isinstance(generation, int) and generation >= 0):
|
|
138
|
+
raise AssertionError
|
|
139
|
+
|
|
140
|
+
self.num = number
|
|
141
|
+
self.gen = generation
|
|
142
|
+
|
|
143
|
+
def __repr__(self):
|
|
144
|
+
return "<IndirectReference:n={self.num},g={self.gen}>".format(self=self)
|
|
145
|
+
|
|
146
|
+
def __eq__(self, other):
|
|
147
|
+
return self.num == other.num and self.gen == other.gen
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class IndirectObject(object):
|
|
151
|
+
""" 10 0 obj
|
|
152
|
+
....
|
|
153
|
+
endobj
|
|
154
|
+
"""
|
|
155
|
+
def __init__(self, number, generation, value):
|
|
156
|
+
if not isinstance(number, int):
|
|
157
|
+
raise AssertionError
|
|
158
|
+
if not (isinstance(generation, int) and generation >= 0):
|
|
159
|
+
raise AssertionError
|
|
160
|
+
if not isinstance(value,
|
|
161
|
+
(type(null), Boolean, Integer, Real, Array, Dictionary, String, Name, HexString, Stream,
|
|
162
|
+
IndirectReference)):
|
|
163
|
+
raise AssertionError
|
|
164
|
+
self.num = number
|
|
165
|
+
self.gen = generation
|
|
166
|
+
self.val = value
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def id(self):
|
|
170
|
+
return self.num, self.gen
|
|
171
|
+
|
|
172
|
+
def __repr__(self):
|
|
173
|
+
return "<IndirectObject:n={self.num},g={self.gen},v={val}>".format(self=self, val=repr(self.val))
|
|
174
|
+
|
|
175
|
+
def __eq__(self, other):
|
|
176
|
+
return self.num == other.num and self.gen == other.gen
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
PDF_TYPES = (type(null), IndirectReference, IndirectObject, Comment, Stream, Dictionary, Integer, Real, Boolean, Array,
|
|
180
|
+
String, HexString, Name)
|
|
181
|
+
|
|
182
|
+
ATOMIC_TYPES = (Integer, Real, Boolean, String, HexString, Name, type(null))
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
is_atomic = lambda obj: isinstance(obj, (ATOMIC_TYPES))
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class Token(str):
|
|
189
|
+
""" That's not a PDF type itself. We used it to reflect other than PDF types tokens.
|
|
190
|
+
For example: * CMap - def, findresource, begin
|
|
191
|
+
* Content stream operators
|
|
192
|
+
"""
|
|
193
|
+
pass
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
if __name__ == "__main__":
|
|
197
|
+
import doctest
|
|
198
|
+
doctest.testmod()
|