Skip to content

Commit

Permalink
Fix #495: resolve params in PDFStream.get_filters
Browse files Browse the repository at this point in the history
Some PDF documents use reference to store filter params. Resolve them to
allow proper extraction of content.

```
In [1]: import pdfplumber

In [2]: doc = pdfplumber.open("bill.pdf")

In [3]: s = doc.images[0]['stream']

In [4]: s.get_data()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 s.get_data()

File ~/.local/share/virtualenvs/pdfreader/lib/python3.11/site-packages/pdfminer/pdftypes.py:396, in PDFStream.get_data(self)
    394 def get_data(self) -> bytes:
    395     if self.data is None:
--> 396         self.decode()
    397         assert self.data is not None
    398     return self.data

File ~/.local/share/virtualenvs/pdfreader/lib/python3.11/site-packages/pdfminer/pdftypes.py:373, in PDFStream.decode(self)
    371     raise PDFNotImplementedError("Unsupported filter: %r" % f)
    372 # apply predictors
--> 373 if params and "Predictor" in params:
    374     pred = int_value(params["Predictor"])
    375     if pred == 1:
    376         # no predictor

TypeError: argument of type 'PDFObjRef' is not iterable

In [5]: s.get_filters()
Out[5]: [(/'FlateDecode', <PDFObjRef:21>)]
```
  • Loading branch information
EvaSDK committed Aug 5, 2023
1 parent 04c88e8 commit c02236b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Color "convenience operators" now (per spec) also set color space ([#794](https://github.com/pdfminer/pdfminer.six/pull/794))
- `ValueError` when extracting images, due to breaking changes in Pillow ([#827](https://github.com/pdfminer/pdfminer.six/pull/827))
- Small typo's and issues in the documentation ([#828](https://github.com/pdfminer/pdfminer.six/pull/828))
- Resolve filter parameters ([#495](https://github.com/pdfminer/pdfminer.six/issues/495))

### Deprecated

Expand Down
16 changes: 10 additions & 6 deletions pdfminer/pdftypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,17 @@ def get_filters(self) -> List[Tuple[Any, Any]]:
if settings.STRICT and len(params) != len(filters):
raise PDFException("Parameters len filter mismatch")
# resolve filter if possible
_filters = []
for fltr in filters:
if hasattr(fltr, "resolve"):
fltr = fltr.resolve()[0]
_filters.append(fltr)
_filters = [
fltr.resolve()[0] if hasattr(fltr, "resolve") else fltr
for fltr in filters
]
# resolve params if possible
_params = [
param.resolve() if hasattr(param, "resolve") else param
for param in params
]
# return list solves https://github.com/pdfminer/pdfminer.six/issues/15
return list(zip(_filters, params))
return list(zip(_filters, _params))

def decode(self) -> None:
assert self.data is None and self.rawdata is not None, str(
Expand Down

0 comments on commit c02236b

Please sign in to comment.