Skip to content

Commit

Permalink
Merge pull request #1867 from timoramsauer/doublecompression
Browse files Browse the repository at this point in the history
Remove duplicate compression of attachments
  • Loading branch information
liZe authored Apr 26, 2023
2 parents 0ff8692 + 1db2d6d commit c140c7b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
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

0 comments on commit c140c7b

Please sign in to comment.