2009-11-03 01:27:30 +00:00
|
|
|
import zlib
|
2016-05-20 19:12:05 +00:00
|
|
|
import logging
|
2014-06-26 09:12:39 +00:00
|
|
|
from .lzw import lzwdecode
|
|
|
|
from .ascii85 import ascii85decode
|
|
|
|
from .ascii85 import asciihexdecode
|
|
|
|
from .runlength import rldecode
|
|
|
|
from .ccitt import ccittfaxdecode
|
|
|
|
from .psparser import PSException
|
|
|
|
from .psparser import PSObject
|
|
|
|
from .psparser import LIT
|
2016-01-10 17:17:38 +00:00
|
|
|
from . import settings
|
2014-06-26 09:12:39 +00:00
|
|
|
from .utils import apply_png_predictor
|
|
|
|
from .utils import isnumber
|
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2016-05-20 19:12:05 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2009-11-07 00:55:18 +00:00
|
|
|
LITERAL_CRYPT = LIT('Crypt')
|
2010-01-30 07:30:01 +00:00
|
|
|
|
|
|
|
# Abbreviation of Filter names in PDF 4.8.6. "Inline Images"
|
2009-11-07 00:55:18 +00:00
|
|
|
LITERALS_FLATE_DECODE = (LIT('FlateDecode'), LIT('Fl'))
|
|
|
|
LITERALS_LZW_DECODE = (LIT('LZWDecode'), LIT('LZW'))
|
|
|
|
LITERALS_ASCII85_DECODE = (LIT('ASCII85Decode'), LIT('A85'))
|
|
|
|
LITERALS_ASCIIHEX_DECODE = (LIT('ASCIIHexDecode'), LIT('AHx'))
|
2009-12-24 11:51:43 +00:00
|
|
|
LITERALS_RUNLENGTH_DECODE = (LIT('RunLengthDecode'), LIT('RL'))
|
2010-01-30 07:30:01 +00:00
|
|
|
LITERALS_CCITTFAX_DECODE = (LIT('CCITTFaxDecode'), LIT('CCF'))
|
|
|
|
LITERALS_DCT_DECODE = (LIT('DCTDecode'), LIT('DCT'))
|
2019-10-22 15:37:06 +00:00
|
|
|
LITERALS_JBIG2_DECODE = (LIT('JBIG2Decode'),)
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2019-12-29 20:20:20 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
class PDFObject(PSObject):
|
|
|
|
pass
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2019-12-29 20:20:20 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
class PDFException(PSException):
|
|
|
|
pass
|
|
|
|
|
2019-12-29 20:20:20 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
class PDFTypeError(PDFException):
|
|
|
|
pass
|
|
|
|
|
2019-12-29 20:20:20 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
class PDFValueError(PDFException):
|
|
|
|
pass
|
|
|
|
|
2019-12-29 20:20:20 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
class PDFObjectNotFound(PDFException):
|
|
|
|
pass
|
|
|
|
|
2019-12-29 20:20:20 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
class PDFNotImplementedError(PDFException):
|
|
|
|
pass
|
2009-01-10 09:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PDFObjRef(PDFObject):
|
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def __init__(self, doc, objid, _):
|
|
|
|
if objid == 0:
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFValueError('PDF object id cannot be 0.')
|
|
|
|
self.doc = doc
|
|
|
|
self.objid = objid
|
|
|
|
return
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '<PDFObjRef:%d>' % (self.objid)
|
|
|
|
|
2013-10-09 13:45:54 +00:00
|
|
|
def resolve(self, default=None):
|
|
|
|
try:
|
|
|
|
return self.doc.getobj(self.objid)
|
|
|
|
except PDFObjectNotFound:
|
|
|
|
return default
|
2009-01-10 09:14:46 +00:00
|
|
|
|
|
|
|
|
2013-10-09 13:45:54 +00:00
|
|
|
def resolve1(x, default=None):
|
2011-02-27 04:09:12 +00:00
|
|
|
"""Resolves an object.
|
|
|
|
|
|
|
|
If this is an array or dictionary, it may still contains
|
|
|
|
some indirect objects inside.
|
|
|
|
"""
|
2009-10-24 04:41:59 +00:00
|
|
|
while isinstance(x, PDFObjRef):
|
2013-10-09 13:45:54 +00:00
|
|
|
x = x.resolve(default=default)
|
2009-10-24 04:41:59 +00:00
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2013-10-09 13:45:54 +00:00
|
|
|
def resolve_all(x, default=None):
|
2011-02-27 04:09:12 +00:00
|
|
|
"""Recursively resolves the given object and all the internals.
|
2013-11-07 07:14:53 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
Make sure there is no indirect reference within the nested object.
|
|
|
|
This procedure might be slow.
|
2011-02-27 04:09:12 +00:00
|
|
|
"""
|
2009-10-24 04:41:59 +00:00
|
|
|
while isinstance(x, PDFObjRef):
|
2013-10-09 13:45:54 +00:00
|
|
|
x = x.resolve(default=default)
|
2009-10-24 04:41:59 +00:00
|
|
|
if isinstance(x, list):
|
2013-11-07 08:35:04 +00:00
|
|
|
x = [resolve_all(v, default=default) for v in x]
|
2009-10-24 04:41:59 +00:00
|
|
|
elif isinstance(x, dict):
|
2020-01-04 15:47:07 +00:00
|
|
|
for (k, v) in x.items():
|
2013-10-09 13:45:54 +00:00
|
|
|
x[k] = resolve_all(v, default=default)
|
2009-10-24 04:41:59 +00:00
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def decipher_all(decipher, objid, genno, x):
|
2011-02-27 04:09:12 +00:00
|
|
|
"""Recursively deciphers the given object.
|
|
|
|
"""
|
2014-09-11 21:33:14 +00:00
|
|
|
if isinstance(x, bytes):
|
2009-10-24 04:41:59 +00:00
|
|
|
return decipher(objid, genno, x)
|
|
|
|
if isinstance(x, list):
|
2013-11-07 08:35:04 +00:00
|
|
|
x = [decipher_all(decipher, objid, genno, v) for v in x]
|
2009-10-24 04:41:59 +00:00
|
|
|
elif isinstance(x, dict):
|
2020-01-04 15:47:07 +00:00
|
|
|
for (k, v) in x.items():
|
2009-10-24 04:41:59 +00:00
|
|
|
x[k] = decipher_all(decipher, objid, genno, v)
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def int_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
|
|
|
if not isinstance(x, int):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('Integer required: %r' % x)
|
|
|
|
return 0
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def float_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
|
|
|
if not isinstance(x, float):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('Float required: %r' % x)
|
|
|
|
return 0.0
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def num_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
2014-03-28 08:50:59 +00:00
|
|
|
if not isnumber(x):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('Int or Float required: %r' % x)
|
|
|
|
return 0
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2020-01-07 20:59:13 +00:00
|
|
|
def uint_value(x, n_bits):
|
|
|
|
"""Resolve number and interpret it as a two's-complement unsigned number"""
|
|
|
|
x = int_value(x)
|
|
|
|
if x > 0:
|
|
|
|
return x
|
|
|
|
else:
|
|
|
|
return x + 2**n_bits
|
|
|
|
|
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def str_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
2020-01-04 15:47:07 +00:00
|
|
|
if not isinstance(x, bytes):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('String required: %r' % x)
|
|
|
|
return ''
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def list_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
2014-03-28 08:50:59 +00:00
|
|
|
if not isinstance(x, (list, tuple)):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('List required: %r' % x)
|
|
|
|
return []
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def dict_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
|
|
|
if not isinstance(x, dict):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2016-05-20 19:12:05 +00:00
|
|
|
log.error('PDFTypeError : Dict required: %r', x)
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('Dict required: %r' % x)
|
|
|
|
return {}
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def stream_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
|
|
|
if not isinstance(x, PDFStream):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('PDFStream required: %r' % x)
|
2017-04-18 16:20:31 +00:00
|
|
|
return PDFStream({}, b'')
|
2009-10-24 04:41:59 +00:00
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PDFStream(PDFObject):
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-01-30 07:30:01 +00:00
|
|
|
def __init__(self, attrs, rawdata, decipher=None):
|
2017-05-29 07:06:09 +00:00
|
|
|
assert isinstance(attrs, dict), str(type(attrs))
|
2010-01-30 07:30:01 +00:00
|
|
|
self.attrs = attrs
|
2009-10-24 04:41:59 +00:00
|
|
|
self.rawdata = rawdata
|
|
|
|
self.decipher = decipher
|
|
|
|
self.data = None
|
|
|
|
self.objid = None
|
|
|
|
self.genno = None
|
|
|
|
return
|
|
|
|
|
|
|
|
def set_objid(self, objid, genno):
|
|
|
|
self.objid = objid
|
|
|
|
self.genno = genno
|
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-02-15 14:50:19 +00:00
|
|
|
if self.data is None:
|
|
|
|
assert self.rawdata is not None
|
2019-12-29 20:20:20 +00:00
|
|
|
return '<PDFStream(%r): raw=%d, %r>' % \
|
|
|
|
(self.objid, len(self.rawdata), self.attrs)
|
2010-02-15 14:50:19 +00:00
|
|
|
else:
|
|
|
|
assert self.data is not None
|
2019-12-29 20:20:20 +00:00
|
|
|
return '<PDFStream(%r): len=%d, %r>' % \
|
|
|
|
(self.objid, len(self.data), self.attrs)
|
2010-01-30 07:30:01 +00:00
|
|
|
|
|
|
|
def __contains__(self, name):
|
|
|
|
return name in self.attrs
|
2013-11-07 07:14:53 +00:00
|
|
|
|
2010-01-30 07:30:01 +00:00
|
|
|
def __getitem__(self, name):
|
|
|
|
return self.attrs[name]
|
2013-11-07 07:14:53 +00:00
|
|
|
|
2010-01-30 07:30:01 +00:00
|
|
|
def get(self, name, default=None):
|
|
|
|
return self.attrs.get(name, default)
|
2013-11-07 07:14:53 +00:00
|
|
|
|
2010-02-07 15:11:24 +00:00
|
|
|
def get_any(self, names, default=None):
|
|
|
|
for name in names:
|
|
|
|
if name in self.attrs:
|
|
|
|
return self.attrs[name]
|
|
|
|
return default
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-04-10 11:05:02 +00:00
|
|
|
def get_filters(self):
|
|
|
|
filters = self.get_any(('F', 'Filter'))
|
2014-07-09 10:07:27 +00:00
|
|
|
params = self.get_any(('DP', 'DecodeParms', 'FDecodeParms'), {})
|
2013-11-07 08:35:04 +00:00
|
|
|
if not filters:
|
|
|
|
return []
|
2014-07-09 10:07:27 +00:00
|
|
|
if not isinstance(filters, list):
|
|
|
|
filters = [filters]
|
|
|
|
if not isinstance(params, list):
|
2016-09-23 12:57:28 +00:00
|
|
|
# Make sure the parameters list is the same as filters.
|
2016-09-23 12:31:31 +00:00
|
|
|
params = [params] * len(filters)
|
2016-09-24 10:03:22 +00:00
|
|
|
if settings.STRICT and len(params) != len(filters):
|
2016-09-23 12:57:28 +00:00
|
|
|
raise PDFException("Parameters len filter mismatch")
|
2017-06-02 11:35:12 +00:00
|
|
|
# resolve filter if possible
|
|
|
|
_filters = []
|
|
|
|
for fltr in filters:
|
|
|
|
if hasattr(fltr, 'resolve'):
|
|
|
|
fltr = fltr.resolve()[0]
|
|
|
|
_filters.append(fltr)
|
2019-12-29 20:20:20 +00:00
|
|
|
# return list solves https://github.com/pdfminer/pdfminer.six/issues/15
|
|
|
|
return list(zip(_filters, params))
|
2010-04-10 11:05:02 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def decode(self):
|
2019-12-29 20:20:20 +00:00
|
|
|
assert self.data is None \
|
|
|
|
and self.rawdata is not None, str((self.data, self.rawdata))
|
2009-10-24 04:41:59 +00:00
|
|
|
data = self.rawdata
|
|
|
|
if self.decipher:
|
|
|
|
# Handle encryption
|
2014-05-19 14:27:43 +00:00
|
|
|
data = self.decipher(self.objid, self.genno, data, self.attrs)
|
2010-04-10 11:05:02 +00:00
|
|
|
filters = self.get_filters()
|
2010-02-07 15:11:24 +00:00
|
|
|
if not filters:
|
2010-02-15 14:50:19 +00:00
|
|
|
self.data = data
|
|
|
|
self.rawdata = None
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
2019-12-29 20:20:20 +00:00
|
|
|
for (f, params) in filters:
|
2009-10-24 04:41:59 +00:00
|
|
|
if f in LITERALS_FLATE_DECODE:
|
|
|
|
# will get errors if the document is encrypted.
|
2010-10-17 05:15:18 +00:00
|
|
|
try:
|
|
|
|
data = zlib.decompress(data)
|
2014-06-16 09:50:07 +00:00
|
|
|
except zlib.error as e:
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2020-01-04 15:47:07 +00:00
|
|
|
error_msg = 'Invalid zlib bytes: {!r}, {!r}'\
|
|
|
|
.format(e, data)
|
2019-12-29 20:20:20 +00:00
|
|
|
raise PDFException(error_msg)
|
2014-06-30 10:05:56 +00:00
|
|
|
data = b''
|
2009-10-24 04:41:59 +00:00
|
|
|
elif f in LITERALS_LZW_DECODE:
|
2009-11-04 11:28:32 +00:00
|
|
|
data = lzwdecode(data)
|
2009-10-24 04:41:59 +00:00
|
|
|
elif f in LITERALS_ASCII85_DECODE:
|
2009-11-04 11:28:32 +00:00
|
|
|
data = ascii85decode(data)
|
2009-10-24 04:41:59 +00:00
|
|
|
elif f in LITERALS_ASCIIHEX_DECODE:
|
2009-11-04 11:28:32 +00:00
|
|
|
data = asciihexdecode(data)
|
2009-12-24 11:51:43 +00:00
|
|
|
elif f in LITERALS_RUNLENGTH_DECODE:
|
|
|
|
data = rldecode(data)
|
2010-02-13 15:00:43 +00:00
|
|
|
elif f in LITERALS_CCITTFAX_DECODE:
|
2011-07-18 11:07:00 +00:00
|
|
|
data = ccittfaxdecode(data, params)
|
2014-05-14 05:25:30 +00:00
|
|
|
elif f in LITERALS_DCT_DECODE:
|
2019-12-29 20:20:20 +00:00
|
|
|
# This is probably a JPG stream
|
|
|
|
# it does not need to be decoded twice.
|
2014-05-14 05:25:30 +00:00
|
|
|
# Just return the stream to the user.
|
|
|
|
pass
|
2019-10-22 15:37:06 +00:00
|
|
|
elif f in LITERALS_JBIG2_DECODE:
|
2019-12-29 20:20:20 +00:00
|
|
|
pass
|
2009-10-24 04:41:59 +00:00
|
|
|
elif f == LITERAL_CRYPT:
|
2009-12-24 11:51:43 +00:00
|
|
|
# not yet..
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFNotImplementedError('/Crypt filter is unsupported')
|
|
|
|
else:
|
|
|
|
raise PDFNotImplementedError('Unsupported filter: %r' % f)
|
|
|
|
# apply predictors
|
Update pdftypes.py (#61)
Fix errors with:
File "/app/python/lib/python3.5/site-packages/pdfminer/pdfinterp.py", line 850, in process_page
self.render_contents(page.resources, page.contents, ctm=ctm)
File "/app/python/lib/python3.5/site-packages/pdfminer/pdfinterp.py", line 860, in render_contents
self.init_resources(resources)
File "/app/python/lib/python3.5/site-packages/pdfminer/pdfinterp.py", line 360, in init_resources
self.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec)
File "/app/python/lib/python3.5/site-packages/pdfminer/pdfinterp.py", line 210, in get_font
font = self.get_font(None, subspec)
File "/app/python/lib/python3.5/site-packages/pdfminer/pdfinterp.py", line 201, in get_font
font = PDFCIDFont(self, spec)
File "/app/python/lib/python3.5/site-packages/pdfminer/pdffont.py", line 667, in __init__
BytesIO(self.fontfile.get_data()))
File "/app/python/lib/python3.5/site-packages/pdfminer/pdftypes.py", line 297, in get_data
self.decode()
File "/app/python/lib/python3.5/site-packages/pdfminer/pdftypes.py", line 278, in decode
if 'Predictor' in params:
TypeError: argument of type 'NoneType' is not iterable
2017-05-29 06:55:02 +00:00
|
|
|
if params and 'Predictor' in params:
|
2009-10-24 04:41:59 +00:00
|
|
|
pred = int_value(params['Predictor'])
|
2011-06-06 15:31:03 +00:00
|
|
|
if pred == 1:
|
|
|
|
# no predictor
|
|
|
|
pass
|
|
|
|
elif 10 <= pred:
|
|
|
|
# PNG predictor
|
|
|
|
colors = int_value(params.get('Colors', 1))
|
|
|
|
columns = int_value(params.get('Columns', 1))
|
2019-12-29 20:20:20 +00:00
|
|
|
raw_bits_per_component = params.get('BitsPerComponent', 8)
|
|
|
|
bitspercomponent = int_value(raw_bits_per_component)
|
|
|
|
data = apply_png_predictor(pred, colors, columns,
|
|
|
|
bitspercomponent, data)
|
2011-06-06 15:31:03 +00:00
|
|
|
else:
|
2019-12-29 20:20:20 +00:00
|
|
|
error_msg = 'Unsupported predictor: %r' % pred
|
|
|
|
raise PDFNotImplementedError(error_msg)
|
2009-10-24 04:41:59 +00:00
|
|
|
self.data = data
|
|
|
|
self.rawdata = None
|
|
|
|
return
|
|
|
|
|
|
|
|
def get_data(self):
|
2009-11-06 15:10:29 +00:00
|
|
|
if self.data is None:
|
2009-10-24 04:41:59 +00:00
|
|
|
self.decode()
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
def get_rawdata(self):
|
|
|
|
return self.rawdata
|