Skip to content

perf: add early page count check to prevent expensive PDFMiner proces… #4048

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.18.6

### Enhancements
- **Optimize PDF processing with early page count check** Prevents expensive PDFMiner processing for documents that exceed page limits by checking page count before strategy execution when `pdf_hi_res_max_pages` is set.

### Features

### Fixes

## 0.18.5-dev0

### Enhancements
Expand Down
21 changes: 21 additions & 0 deletions unstructured/partition/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,27 @@ def partition_pdf_or_image(

validate_strategy(strategy, is_image)

# Early page count check for strategies that will use HI_RES
# This prevents expensive PDFMiner processing for documents that exceed page limits
pdf_hi_res_max_pages = kwargs.get("pdf_hi_res_max_pages")
if pdf_hi_res_max_pages is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please write a unit test to verify this behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I forgot to mark this as a draft. This is not ready for review and will have testing when complete

# Check if this strategy will result in HI_RES processing
will_use_hi_res = False

if strategy == PartitionStrategy.HI_RES:
will_use_hi_res = True
elif strategy == PartitionStrategy.AUTO:
# AUTO resolves to HI_RES in these cases:
extract_element = extract_images_in_pdf or bool(extract_image_block_types)
will_use_hi_res = is_image or infer_table_structure or extract_element

if will_use_hi_res:
check_pdf_hi_res_max_pages_exceeded(
filename=filename,
file=file,
pdf_hi_res_max_pages=pdf_hi_res_max_pages,
)

last_modified = get_last_modified_date(filename) if filename else None
pdfminer_config = PDFMinerConfig(
line_margin=pdfminer_line_margin,
Expand Down
Loading