Skip to content

Commit

Permalink
Don’t remove attachments when adding internal anchors
Browse files Browse the repository at this point in the history
Fix #1733.
  • Loading branch information
liZe committed Oct 4, 2022
1 parent ad5222b commit 4185ad3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 15 additions & 0 deletions tests/test_pdf.py
Expand Up @@ -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='''
<title>Test document 2</title>
<meta charset="utf-8">
<link rel="attachment" href="data:,some data">
<h1 id="title">Title</h1>
<a href="#title">example</a>
''').write_pdf()
md5 = '<{}>'.format(hashlib.md5(b'some data').hexdigest()).encode()
assert md5 in pdf
assert b'EmbeddedFiles' in pdf


@assert_no_logs
Expand Down
5 changes: 4 additions & 1 deletion weasyprint/pdf/__init__.py
Expand Up @@ -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:
Expand Down

0 comments on commit 4185ad3

Please sign in to comment.