From 73a31345299a03abcfbcff916abca15f2fa64b0c Mon Sep 17 00:00:00 2001 From: pubpub-zz <4083478+pubpub-zz@users.noreply.github.com> Date: Tue, 3 Oct 2023 19:47:09 +0200 Subject: [PATCH 1/2] ROB: XYZ destination to cope with missing left and top param closes #2236 --- pypdf/generic/_data_structures.py | 4 ++++ tests/test_reader.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/pypdf/generic/_data_structures.py b/pypdf/generic/_data_structures.py index 88a17d85a..f14fda887 100644 --- a/pypdf/generic/_data_structures.py +++ b/pypdf/generic/_data_structures.py @@ -1490,6 +1490,10 @@ def __init__( if len(args) == 0: pass elif typ == "/XYZ": + if len(args) < 1: # left is missing : should never occur + args.append(NumberObject(0.0)) + if len(args) < 2: # top is missing + args.append(NumberObject(0.0)) if len(args) < 3: # zoom is missing args.append(NumberObject(0.0)) ( diff --git a/tests/test_reader.py b/tests/test_reader.py index 8afb45737..13b2ef780 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -1454,3 +1454,12 @@ def test_issue_140(): b = get_data_from_url(url, name=name) reader = PdfReader(BytesIO(b)) assert len(reader.pages) == 54 + + +@pytest.mark.enable_socket() +def test_xyz_with_missing_param(): + """Cf #2236""" + url = "https://github.com/py-pdf/pypdf/files/12795356/tt1.pdf" + name = "issue2236.pdf" + reader = PdfReader(BytesIO(get_data_from_url(url, name=name))) + reader.outline From d9d0d0fd24579ac891d144da97cc9a79d12d0501 Mon Sep 17 00:00:00 2001 From: pubpub-zz <4083478+pubpub-zz@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:07:41 +0200 Subject: [PATCH 2/2] coverage --- pypdf/generic/_data_structures.py | 6 +++--- tests/test_reader.py | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pypdf/generic/_data_structures.py b/pypdf/generic/_data_structures.py index f14fda887..2cd9b16fd 100644 --- a/pypdf/generic/_data_structures.py +++ b/pypdf/generic/_data_structures.py @@ -1487,9 +1487,7 @@ def __init__( self[NameObject("/Type")] = typ # from table 8.2 of the PDF 1.7 reference. - if len(args) == 0: - pass - elif typ == "/XYZ": + if typ == "/XYZ": if len(args) < 1: # left is missing : should never occur args.append(NumberObject(0.0)) if len(args) < 2: # top is missing @@ -1501,6 +1499,8 @@ def __init__( self[NameObject(TA.TOP)], self[NameObject("/Zoom")], ) = args + elif len(args) == 0: + pass elif typ == TF.FIT_R: ( self[NameObject(TA.LEFT)], diff --git a/tests/test_reader.py b/tests/test_reader.py index 13b2ef780..b252e48f9 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -1462,4 +1462,7 @@ def test_xyz_with_missing_param(): url = "https://github.com/py-pdf/pypdf/files/12795356/tt1.pdf" name = "issue2236.pdf" reader = PdfReader(BytesIO(get_data_from_url(url, name=name))) - reader.outline + assert reader.outline[0]["/Left"] == 820 + assert reader.outline[0]["/Top"] == 0 + assert reader.outline[1]["/Left"] == 0 + assert reader.outline[0]["/Top"] == 0