Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #495: resolve params in PDFStream.get_filters #906

Merged
merged 7 commits into from
Jan 16, 2024

Conversation

EvaSDK
Copy link
Contributor

@EvaSDK EvaSDK commented Aug 5, 2023

Pull request

Some PDF documents use reference to store filter params. Resolve them to allow proper extraction of content.

Below is a minimum code raising the problem with the document I was working with (different from #495) but it has PII so I cannot share it. I found #495 and reproduced the problem with it which is what the PR is now using but I kept this in the long description because that's what lead me to fixing the issue.

I switched building _filters to list-comprehension just for consistency with the code I added.

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>)]

How Has This Been Tested?

Found a PDF exposing the problem in #495 and implemented unit test with it. I did not check if the PDF is freely reusable.

Checklist

  • I have read CONTRIBUTING.md.
  • I have added a concise human-readable description of the change to CHANGELOG.md.
  • I have tested that this fix is effective or that this feature works.
  • I have added docstrings to newly created methods and classes.
  • I have updated the README.md and the readthedocs documentation. Or verified that this is not necessary.

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>)]
```
@pavlokochkin
Copy link

worked for me, kudos @EvaSDK

@pietermarsman
Copy link
Member

@EvaSDK Thanks for figuring this out! Your time and effort is appreciated.

@pavlokochkin Thanks for the confirmation.

@pietermarsman pietermarsman added this pull request to the merge queue Jan 16, 2024
Merged via the queue into pdfminer:master with commit f428846 Jan 16, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants