Skip to content

fix(pdf): drop clipped and off-page text - #89

Merged
asionesjia merged 2 commits into
mainfrom
fix/pdf-clip-hidden-text
Aug 27, 2026
Merged

asionesjia merged 2 commits into
mainfrom
fix/pdf-clip-hidden-text

Conversation

@asionesjia

@asionesjia asionesjia commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Confirmed #79. The three cited one-page crops keep the next page's text in the content stream. Viewers and pdftotext -layout hide it with crop/clip. mdgate read the whole stream.

On 01030000000027.pdf that leaked 6.Conclusion, the following essay, and 49. On 01030000000028.pdf and 01030000000029.pdf each file mixed in the adjacent page, so the two markdown outputs overlapped.

CropBox equals MediaBox on these files. The leftover sits behind nested re W n clips (and, on the right-hand crop, at negative x). The extractor ignored both.

Changes

  • Start each page with CropBox, else MediaBox, including boxes inherited from /Pages.
  • Intersect that region with W / W* clip rects, saved and restored with q / Q.
  • Skip text, strokes, rects, and annotations that do not overlap the current clip.
  • Clip form XObject text to the form BBox.

Test plan

  • bunx vitest run packages/pdf/test (images included after bun run build)
  • bun run lint
  • Pre-commit bun test (557 pass)
  • Rechecked the issue's bench pages: 027 no longer emits Conclusion/49; 028 keeps 312 and drops 313/sections 5-6; 029 keeps 313/sections 5-6 and drops the previous page

Note

Medium Risk
Changes core content-stream interpretation and can drop previously extracted text if clip heuristics (axis-aligned path bounds) disagree with a viewer; scope is limited to the PDF package with targeted tests.

Overview
Fixes PDF-to-markdown leaking text that viewers hide via CropBox/MediaBox and content-stream clip paths (issue #79: adjacent-page content in cropped one-page exports).

The extractor now tracks a visible clip per page: CropBox (else MediaBox, including inheritance from /Pages), intersected with W / W* path bounding boxes and saved/restored on q / Q. Text runs, stroke lines, filled rects, form XObject content (intersected with BBox), and annotation items are omitted when they do not overlap the active clip; annotation appearances are skipped when /Rect is off-page, while in-page widget labels still render.

Adds packages/pdf/test/clip.test.ts covering MediaBox/CropBox, nested clips, form BBox, and annotation edge cases.

Reviewed by Cursor Bugbot for commit bb53d1e. Bugbot is set up for automated code reviews on this repo. Configure here.

Honor CropBox/MediaBox and W/W* clips so leftover next-page content
in the stream is not emitted as markdown.

fixes #79
@asionesjia asionesjia added bug Something isn't working pkg:pdf @mdgate/pdf labels Aug 27, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
mdgate-demo bb53d1e Aug 27 2026, 07:20 AM

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Appearance clip mixes coordinate spaces
    • On-page appearances are kept by filtering on the annotation Rect in page space, and form streams are extracted without intersecting the form-space BBox with the page crop.

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit cc80126. Configure here.

Comment thread packages/pdf/src/pdf.ts

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale comment

Left a non-blocking comment. Cursor Bugbot finished as skipped and reported 1 unresolved issue, so this is not approved. Human review is needed; no reviewers were assigned because no eligible non-author reviewer is available.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

Appearance forms stay in form space. Intersecting their BBox with the
page crop dropped on-page widgets and leaked off-page ones. Filter by
the annotation Rect instead, then extract the form without a page clip.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approved. Cursor Bugbot passed on the current head with the prior finding resolved and no new issues, so no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

@asionesjia
asionesjia merged commit 4fa4faf into main Aug 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working pkg:pdf @mdgate/pdf

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants