Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate compression of attachments #1867

Merged
merged 3 commits into from
Apr 26, 2023
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
2 changes: 1 addition & 1 deletion weasyprint/pdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def generate_pdf(document, target, zoom, **options):
pdf_attachments = []
for attachment in attachments:
pdf_attachment = write_pdf_attachment(
pdf, attachment, document.url_fetcher)
pdf, attachment, document.url_fetcher, compress)
if pdf_attachment is not None:
pdf_attachments.append(pdf_attachment)
if pdf_attachments:
Expand Down
14 changes: 4 additions & 10 deletions weasyprint/pdf/anchors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import hashlib
import io
import zlib
from os.path import basename
from urllib.parse import unquote, urlsplit

Expand Down Expand Up @@ -214,7 +213,7 @@ def add_annotations(links, matrix, document, pdf, page, annot_files, compress):
# above about multiple regions won't always be correct, because
# two links might have the same href, but different titles.
annot_files[annot_target] = write_pdf_attachment(
pdf, (annot_target, None), document.url_fetcher)
pdf, (annot_target, None), document.url_fetcher, compress)
annot_file = annot_files[annot_target]
if annot_file is None:
continue
Expand Down Expand Up @@ -242,7 +241,7 @@ def add_annotations(links, matrix, document, pdf, page, annot_files, compress):
page['Annots'].append(annot.reference)


def write_pdf_attachment(pdf, attachment, url_fetcher):
def write_pdf_attachment(pdf, attachment, url_fetcher, compress):
"""Write an attachment to the PDF stream."""
# Attachments from document links like <link> or <a> can only be URLs.
# They're passed in as tuples
Expand All @@ -261,23 +260,18 @@ def write_pdf_attachment(pdf, attachment, url_fetcher):
uncompressed_length = 0
stream = b''
md5 = hashlib.md5()
compress = zlib.compressobj()
for data in iter(lambda: source.read(4096), b''):
uncompressed_length += len(data)
md5.update(data)
compressed = compress.compress(data)
stream += compressed
compressed = compress.flush(zlib.Z_FINISH)
stream += compressed
stream += data
file_extra = pydyf.Dictionary({
'Type': '/EmbeddedFile',
'Filter': '/FlateDecode',
'Params': pydyf.Dictionary({
'CheckSum': f'<{md5.hexdigest()}>',
'Size': uncompressed_length,
})
})
file_stream = pydyf.Stream([stream], file_extra, compress)
file_stream = pydyf.Stream([stream], file_extra, compress=compress)
pdf.add_object(file_stream)

except URLFetchingError as exception:
Expand Down