bug/trim-large-pages: hi_res page trimming keeps the wrong slice when the MediaBox does not start at y=0
まだ誰も着手していません。
評価
調査の方向性
unstructured_client/_hooks/custom/split_pdf_hook.pyを読み、SplitPdfHook._trim_large_pagesとMediaBoxの調整方法に注目してください。issueで提案されているように、MediaBoxの原点がゼロ以外の場合を対象とするパラメータ化ユニットテストを追加してください。既存のテストでは現在、このメソッドをモックしています。各原点で上部の4000ptが保持され、原点がゼロから始まるボックスでは既存の動作が変わらないことが完了条件です。
索引モデルが issue の本文から書いたものです。
説明
Describe the bug
With strategy="hi_res" and PDF splitting, SplitPdfHook._trim_large_pages trims any page taller than MAX_PAGE_LENGTH (4000pt) to its top 4000pt. It does that by setting mediabox.top = mediabox.height and then bottom = top - 4000, which assumes the MediaBox starts at y=0. When the MediaBox has a non-zero bottom edge, the window it keeps is shifted down by that offset:
- MediaBox
[0, 1000, 612, 6000]becomes[0, 1000, 612, 5000]: the top 1000pt of the page are dropped and the bottom part is kept instead. - MediaBox
[0, 5000, 612, 10000]becomes[0, 1000, 612, 5000], which doesn't overlap the page at all, so that chunk goes to the API as a blank page.
To Reproduce
On main (c1b153f, unstructured-client 0.47.0, pypdf 6.21.4):
import io, sys
from pypdf import PdfReader, PdfWriter
from pypdf.generic import RectangleObject
from unstructured_client._hooks.custom.split_pdf_hook import SplitPdfHook, MAX_PAGE_LENGTH
origin = float(sys.argv[1]) if len(sys.argv) > 1 else 1000.0
w = PdfWriter()
page = w.add_blank_page(width=612, height=5000)
page.mediabox = RectangleObject([0, origin, 612, origin + 5000]) # 5000pt tall, bottom edge at y=origin
buf = io.BytesIO(); w.write(buf); buf.seek(0)
before = PdfReader(buf).pages[0].mediabox
after = SplitPdfHook()._trim_large_pages(PdfReader(buf), {"strategy": "hi_res"}).pages[0].mediabox
print("original:", [float(x) for x in before])
print("trimmed: ", [float(x) for x in after])
print("expected:", [0.0, float(before.top) - MAX_PAGE_LENGTH, 612.0, float(before.top)])
origin 0 trimmed [0, 1000, 612, 5000] expected [0, 1000, 612, 5000]
origin 1000 trimmed [0, 1000, 612, 5000] expected [0, 2000, 612, 6000]
origin 5000 trimmed [0, 1000, 612, 5000] expected [0, 6000, 612, 10000]
Expected behavior
Keep the top 4000pt of the page wherever the MediaBox sits. Dropping the top assignment and only moving the bottom edge does that:
if page.mediabox.height >= max_page_length:
page.mediabox.bottom = page.mediabox.top - max_page_length
With that change all three cases above match the expected box, and it is the same result as today for MediaBoxes that start at 0. _trim_large_pages has no direct test right now (the unit tests patch it out); I'd suggest a parametrized one over the MediaBox origin like the script above.
Additional context
This only runs when the hook actually splits (hi_res, more pages than one split or a page range). I found it reading the hook, not from a customer file, so I don't know how common offset MediaBoxes are in practice; they do show up in cropped or imposed PDFs. I saw CONTRIBUTING asks for issues rather than PRs, so filing it here.
- 主要言語
- Python
- スター
- 120
- フォーク
- 22
- 平均マージ
- 2日 3時間
- マージ済み PR(30日)
- 6
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Unstructured-IO/unstructured-python-client のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Unstructured-IO/unstructured-python-client#362 · コメント 1 件 ·
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
Unstructured-IO/unstructured-python-client の issue をすべて見る
似ている issue
-
bump patch
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
autowarefoundation/autoware_carla_scenario#1321 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 78/100
QuantConnect/Documentation#2838 ·
メンテナーはふだん 1 日以内に返信
-
get_all_subclasses must deduplicate by class identity対応中かも @Gambit-Checkmate が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 15 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
awslabs/route53-dynamic-dns-with-lambda#67 · コメント 1 件 ·