PyPdfiumDocumentBackend: a comma run that descends below its row stays a separate cell ("$250 , 500,000")
Bug
With PyPdfiumDocumentBackend, a punctuation run that a PDF draws as its own text object stays a separate cell when it hangs below the baseline of its row, so the page text gets spaces that aren't in the PDF: $250 , 500,000, ACME HOLDINGS PLC , as Parent.
#5161 (2.148.0) joins a punctuation run whose box lies inside the row (bbox.t >= row_top and bbox.b <= row_bottom in group_rows). But a comma descends below the baseline. If the run before it has no descenders (digits, capitals), the comma's bottom edge is below row_bottom, so the rule doesn't fire. In a 350-page credit agreement converted with 2.150.0, this still splits dollar amounts and party names, even though pdfium's own text for those pages is clean.
Steps to reproduce
import ctypes, tempfile
from pathlib import Path
import pypdfium2 as pdfium
import pypdfium2.raw as pdfium_c
from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend
from docling.datamodel.base_models import InputFormat
from docling.datamodel.document import InputDocument
def runs_pdf(path, lines):
"""One text object per run; each run starts where the previous one ends."""
doc = pdfium.PdfDocument.new()
page = doc.new_page(612, 792)
for y, runs in lines:
x = 72.0
for run in runs:
obj = pdfium_c.FPDFPageObj_NewTextObj(doc.raw, b"Times-Roman", ctypes.c_float(12))
buf = ctypes.create_string_buffer((run + "\x00").encode("utf-16-le"))
pdfium_c.FPDFText_SetText(obj, ctypes.cast(buf, ctypes.POINTER(pdfium_c.FPDF_WCHAR)))
pdfium_c.FPDFPageObj_Transform(obj, 1, 0, 0, 1, x, y)
l, b, r, t = (ctypes.c_float() for _ in range(4))
pdfium_c.FPDFPageObj_GetBounds(obj, l, b, r, t)
pdfium_c.FPDFPage_InsertObject(page.raw, obj)
x = r.value
pdfium_c.FPDFPage_GenerateContent(page.raw)
doc.save(str(path))
path = Path(tempfile.mkdtemp()) / "runs.pdf"
runs_pdf(path, [
(700, ["the sum of $250", ",", "500,000 and 32.5%"]),
(680, ["ACME HOLDINGS PLC", ",", " as Parent"]),
])
print(repr(pdfium.PdfDocument(str(path))[0].get_textpage().get_text_range()))
doc = InputDocument(path_or_stream=path, format=InputFormat.PDF, backend=PyPdfiumDocumentBackend)
print([c.text for c in doc._backend.load_page(0).get_text_cells()])
pdfium's text:
'the sum of $250,500,000 and 32.5%\r\nACME HOLDINGS PLC, as Parent'
Cells on 2.150.0:
['the sum of $250', ',', '500,000 and 32.5%', 'ACME HOLDINGS PLC', ', ', ' as Parent']
Expected: ['the sum of $250,500,000 and 32.5%', 'ACME HOLDINGS PLC, as Parent'].
Testing whether the punctuation box overlaps the row vertically, rather than whether it lies inside it, would join these. Another approach is to build a line's cells from pdfium's character stream (FPDFText_GetUnicode per character, FPDFText_GetLooseCharBox for spacing) instead of from get_rect + text_in_rect. That reads each character once and never splits a run off its line. It is what we use as a workaround, and on that document it gives zero split amounts and zero spaces before punctuation.
Related, for the same document
PageAssembleModel.sanitize_text drops a line-end hyphen whenever both sides are word characters. That removes the hyphen from compounds that happen to wrap at it: "Non-" / "Guarantor" becomes "NonGuarantor", and the same happens to "Co-Documentation", "Bail-In" and "Dollar-denominated". pdfium marks every line-end hyphen the same way (U+FFFE in get_text_range), so the backend can't tell a wrap hyphen from a compound either. What worked for us was to let the document decide: keep the hyphen if the document spells the word hyphenated elsewhere more often than joined. Happy to open a separate issue for this if that's preferred.
Docling version
2.150.0 (also 2.118.0–2.145.0, where the shared-edge character is read twice as well: ', R', fixed by 2.148.0)
Python version
3.12, pypdfium2 5.15.2, macOS 15 (arm64)
Classification labels added: bug, priority:medium, pdf.
These labels are advisory. Maintainers can correct them. The workflow preserves these corrections.
Hi Docling maintainers!
I'm interested in contributing to issue #5353 regarding punctuation runs being separated from their corresponding PDF text rows in PyPdfiumDocumentBackend.
I have experience with Python, PDF processing, and RAG-based document Q&A systems. I'd like to investigate the row-grouping logic and develop a robust fix with regression tests.
Before I start, could you please confirm whether this issue is available for contribution and whether there are any preferred implementation approaches?
Thank you!