Skip to content

PyPdfiumDocumentBackend wrongly merges text bonding boxes #1546

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

Closed
pedrolourencoribeiro opened this issue May 7, 2025 · 1 comment · Fixed by #1549
Closed

PyPdfiumDocumentBackend wrongly merges text bonding boxes #1546

pedrolourencoribeiro opened this issue May 7, 2025 · 1 comment · Fixed by #1549
Labels
bug Something isn't working

Comments

@pedrolourencoribeiro
Copy link
Contributor

Bug

PyPdfium2 produces very fragmented cells, with sub-word level boundaries, in many PDFs.
There is some logic to try to merge them

merged_text = "".join(cell.text for cell in group)
  merged_bbox = BoundingBox(
      l=min(cell.rect.to_bounding_box().l for cell in group),
      t=min(cell.rect.to_bounding_box().t for cell in group),
      r=max(cell.rect.to_bounding_box().r for cell in group),
      b=max(cell.rect.to_bounding_box().b for cell in group),
  )

While this works for most cases sometimes the bounding boxes overlap and we end up having duplicated characters. We could instead get the text_bounded based on the new computed BoundingBox

merged_bbox = BoundingBox(
    l=min(cell.rect.to_bounding_box().l for cell in group),
    t=min(cell.rect.to_bounding_box().t for cell in group),
    r=max(cell.rect.to_bounding_box().r for cell in group),
    b=max(cell.rect.to_bounding_box().b for cell in group),
)

bbox = merged_bbox.to_bottom_left_origin(page_size.height)
merged_text = self.text_page.get_text_bounded(*bbox.as_tuple())

loans-leases-with-images.pdf

...

Steps to reproduce

Parse the pdf provided (page 3 for example) and look for offffering

One example in the following PDF is
BBox1: off
BBox2: ffering
that leads to: offffering
...

Docling version

2.31
...

Python version

3.12
...

@pedrolourencoribeiro pedrolourencoribeiro added the bug Something isn't working label May 7, 2025
@pedrolourencoribeiro
Copy link
Contributor Author

#1549
I've created a PR with the potential fix not sure on how to proceed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant