-
Notifications
You must be signed in to change notification settings - Fork 17
fix: Return error for invalid PDFs #277
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4de9a67
fix: return appropriate error for failing pdf, before sending the req…
mpolomdeepsense d9c3b4a
test: pdf check tests before request
mpolomdeepsense d094ac9
fix: add new line at the end of a file
mpolomdeepsense bd7f658
docs: fix check_pdf function docs
mpolomdeepsense b861538
fix: pylint errors and warnings
mpolomdeepsense 4689fc1
chore: update changelog
mpolomdeepsense 2a15f55
test: before_request split pdf hook unit tests
mpolomdeepsense 2a39907
Merge branch 'main' into fix/return-422-for-failing-pdfs
mpolomdeepsense File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
import io | ||
|
||
import pytest | ||
from pypdf import PdfReader | ||
|
||
from unstructured_client._hooks.custom.pdf_utils import check_pdf, PDFValidationError | ||
from _test_unstructured_client.unit_utils import sample_docs_path | ||
|
||
|
||
def _open_pdf(pdf_path: str) -> PdfReader: | ||
with open(pdf_path, "rb") as f: | ||
pdf_content = f.read() | ||
return PdfReader(io.BytesIO(pdf_content)) | ||
|
||
|
||
def test_check_pdf_with_valid_pdf(): | ||
pdf_path = sample_docs_path("list-item-example-1.pdf") | ||
pdf = _open_pdf(pdf_path) | ||
|
||
result = check_pdf(pdf) | ||
assert isinstance(result, PdfReader) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("pdf_name", "expected_error_message"), | ||
[ | ||
( | ||
"failing-encrypted.pdf", | ||
"File is encrypted. Please decrypt it with password.", | ||
), | ||
( | ||
"failing-missing-root.pdf", | ||
"File does not appear to be a valid PDF. Error: Cannot find Root object in pdf", | ||
), | ||
( | ||
"failing-missing-pages.pdf", | ||
"File does not appear to be a valid PDF. Error: Invalid object in /Pages", | ||
), | ||
], | ||
) | ||
def test_check_pdf_raises_pdf_validation_error( | ||
pdf_name: str, expected_error_message: str | ||
): | ||
"""Test that we get a PDFValidationError with the correct error message for invalid PDF files.""" | ||
pdf_path = sample_docs_path(pdf_name) | ||
pdf = _open_pdf(pdf_path) | ||
|
||
with pytest.raises(PDFValidationError) as exc_info: | ||
check_pdf(pdf) | ||
|
||
assert exc_info.value.message == expected_error_message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we profiled it for memory/time for large pdfs (like ~1k pages)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not, but maybe we could just check for the first page? I think it will result with the same error. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested. This does not slow down execution and doesn't affect memory usage, even with big files (tested on 200, 1000 and 10000 pages pdfs).
Results for 10k pages pdf:
mem_profiler_results.txt