From 4185ad3409accd6eac57c85f14c4f5bd70ed2e05 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Tue, 4 Oct 2022 11:16:33 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20remove=20attachments=20when=20a?= =?UTF-8?q?dding=20internal=20anchors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #1733. --- tests/test_pdf.py | 15 +++++++++++++++ weasyprint/pdf/__init__.py | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/test_pdf.py b/tests/test_pdf.py index 6e93040c6d..e3067b6fe9 100644 --- a/tests/test_pdf.py +++ b/tests/test_pdf.py @@ -620,6 +620,21 @@ def test_attachments_data(): ''').write_pdf() md5 = '<{}>'.format(hashlib.md5(b'some data').hexdigest()).encode() assert md5 in pdf + assert b'EmbeddedFiles' in pdf + + +@assert_no_logs +def test_attachments_data_with_anchor(): + pdf = FakeHTML(string=''' + Test document 2 + + +

Title

+ example + ''').write_pdf() + md5 = '<{}>'.format(hashlib.md5(b'some data').hexdigest()).encode() + assert md5 in pdf + assert b'EmbeddedFiles' in pdf @assert_no_logs diff --git a/weasyprint/pdf/__init__.py b/weasyprint/pdf/__init__.py index 4c396dcbe2..34ae7e8aa7 100644 --- a/weasyprint/pdf/__init__.py +++ b/weasyprint/pdf/__init__.py @@ -468,7 +468,10 @@ def generate_pdf(document, target, zoom, attachments, optimize_size, name_array.append(pydyf.String(anchor[0])) name_array.append(anchor[1]) dests = pydyf.Dictionary({'Names': name_array}) - pdf.catalog['Names'] = pydyf.Dictionary({'Dests': dests}) + if 'Names' in pdf.catalog: + pdf.catalog['Names']['Dests'] = dests + else: + pdf.catalog['Names'] = pydyf.Dictionary({'Dests': dests}) # Apply PDF variants functions if variant: