Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions PyPDF2/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,9 +1133,11 @@ def _extract_text(
if "/Font" in resources_dict:
for f in cast(DictionaryObject, resources_dict["/Font"]):
cmaps[f] = build_char_map(f, space_width, obj)
cmap: Tuple[
Union[str, Dict[int, str]], Dict[str, str], str
] # (encoding,CMAP,font_name)
cmap: Tuple[Union[str, Dict[int, str]], Dict[str, str], str] = (
"charmap",
{},
"NotInitialized",
) # (encoding,CMAP,font_name)
try:
content = (
obj[content_key].get_object() if isinstance(content_key, str) else obj
Expand Down Expand Up @@ -1211,10 +1213,28 @@ def process_operation(operator: bytes, operands: List) -> None:
# table 4.7, page 219
# cm_matrix calculation is a reserved for the moment
elif operator == b"q":
cm_stack.append(cm_matrix)
cm_stack.append(
(
cm_matrix,
cmap,
font_size,
char_scale,
space_scale,
_space_width,
TL,
)
)
elif operator == b"Q":
try:
cm_matrix = cm_stack.pop()
(
cm_matrix,
cmap,
font_size,
char_scale,
space_scale,
_space_width,
TL,
) = cm_stack.pop()
except Exception:
cm_matrix = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
elif operator == b"cm":
Expand Down
9 changes: 9 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ def test_extract_text_single_quote_op():
page.extract_text()


def test_iss_1142():
# check fix for problem of context save/restore (q/Q)
url = "https://github.com/py-pdf/PyPDF2/files/9150656/ST.2019.PDF"
name = "st2019.pdf"
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
txt = reader.pages[3].extract_text()
assert txt.find("有限公司郑州分公司") > 0


@pytest.mark.parametrize(
("url", "name"),
[
Expand Down