Skip to content

Commit

Permalink
BUG: Missing spaces in extract_text() method (#1328) (#2868)
Browse files Browse the repository at this point in the history
* BUG: Missing spaces in extract_text() method (#1328)

* Revert "BUG: Missing spaces in extract_text() method (#1328)"

This reverts commit 5400f5a.

* BUG: Missing spaces in extract_text() method (#1328)

* BUG: Missing spaces in extract_text() method (#1328) add test

* Revert "BUG: Missing spaces in extract_text() method (#1328)"

This reverts commit 5400f5a.

BUG: Missing spaces in extract_text() method (#1328)

BUG: Missing spaces in extract_text() method (#1328) add test

* BUG: Missing spaces in extract_text() method (#1328) Convert font size comparison to ratio

* Correction to new file URL.

Co-authored-by: Stefan <96178532+stefan6419846@users.noreply.github.com>

* BUG: Missing spaces in extract_text() method (#1328) calculation efficiency

* BUG: Missing spaces in extract_text() method (#1328) Simplify the assertion process

---------

Co-authored-by: Stefan <96178532+stefan6419846@users.noreply.github.com>
  • Loading branch information
ssjkamei and stefan6419846 authored Sep 24, 2024
1 parent d974d5c commit 635a7c1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pypdf/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,11 +1985,13 @@ def process_operation(operator: bytes, operands: List[Any]) -> None:
process_operation(b"TL", [-operands[1]])
process_operation(b"Td", operands)
elif operator == b"TJ":
# The space width may be smaller than the font width, so the width should be 95%.
_confirm_space_width = _space_width * 0.95
for op in operands[0]:
if isinstance(op, (str, bytes)):
process_operation(b"Tj", [op])
if isinstance(op, (int, float, NumberObject, FloatObject)) and (
(abs(float(op)) >= _space_width)
(abs(float(op)) >= _confirm_space_width)
and (len(text) > 0)
and (text[-1] != " ")
):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_text_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,14 @@ def test_layout_mode_warnings(mock_logger_warning):
mock_logger_warning.assert_called_with(
"Argument visitor_text is ignored in layout mode", "pypdf._page"
)


@pytest.mark.enable_socket()
def test_space_with_one_unit_smaller_than_font_width():
"""Tests for #1328"""
url = "https://github.com/py-pdf/pypdf/files/9498481/0004.pdf"
name = "iss1328.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
page = reader.pages[0]
extracted = page.extract_text()
assert "Reporting crude oil leak.\n" in extracted

0 comments on commit 635a7c1

Please sign in to comment.