Skip to content

Commit

Permalink
Attempt to handle decompression error on some broken PDF files
Browse files Browse the repository at this point in the history
from times to times we go through files where no text is detected, while readers
like evince reads the pdf nicely. After digging it occured this is because the
PDF includes some badly compressed data. This may be fixed by uncompressing byte
per byte and ignoring the error on the last check bytes (arbitrarily found to be
the 3 last).

This has been largely inspired by py-pdf/pypdf#422
and the test file has been taken from there, so credits to @zegrep.
  • Loading branch information
Sylvain Thénault committed Sep 20, 2021
1 parent 106b13b commit c92e3a9
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pdfminer/pdftypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def stream_value(x):


def decompress_corrupted(data):
d = zlib.decompressobj(zlib.MAX_WBITS | 32)
"""Called on some data that can't be properly decoded because of CRC checksum
error. Attempt to decode it skipping the CRC.
"""
d = zlib.decompressobj()
f = io.BytesIO(data)
result_str = b''
buffer = f.read(1)
Expand All @@ -198,6 +201,7 @@ def decompress_corrupted(data):
buffer = f.read(1)
i += 1
except zlib.error:
# Let the error propagates if we're not yet in the CRC checksum
if i < len(data) - 3:
raise
return result_str
Expand Down

0 comments on commit c92e3a9

Please sign in to comment.