Skip to content

Commit

Permalink
Change management of attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Feb 8, 2024
1 parent 54d8f72 commit 287819b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
3 changes: 1 addition & 2 deletions weasyprint/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ def __init__(self, title=None, authors=None, description=None,
#: Extracted from the ``<meta name=dcterms.modified>`` element in HTML
#: and written to the ``/ModDate`` info field in PDF.
self.modified = modified
#: File attachments, as a list of tuples of URL and a description or
#: :obj:`None`. (Defaults to the empty list.)
#: A list of :class:`attachments <Attachment>`, empty by default.
#: Extracted from the ``<link rel=attachment>`` elements in HTML
#: and written to the ``/EmbeddedFiles`` dictionary in PDF.
self.attachments = attachments or []
Expand Down
5 changes: 3 additions & 2 deletions weasyprint/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def read_text(package, resource):

import re

from . import CSS, css
from . import CSS, Attachment, css
from .css import get_child_text
from .css.counters import CounterStyle
from .formatting_structure import boxes
Expand Down Expand Up @@ -281,7 +281,8 @@ def get_html_metadata(html):
if url is None:
LOGGER.error('Missing href in <link rel="attachment">')
else:
attachments.append((url, attachment_title))
attachment = Attachment(url=url, description=attachment_title)
attachments.append(attachment)
return {
'title': title,
'description': description,
Expand Down
12 changes: 8 additions & 4 deletions weasyprint/pdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pydyf

from .. import VERSION
from .. import VERSION, Attachment
from ..html import W3C_DATE_RE
from ..logger import LOGGER, PROGRESS_LOGGER
from ..matrix import Matrix
Expand Down Expand Up @@ -245,11 +245,15 @@ def generate_pdf(document, target, zoom, **options):
pdf.info[key] = pydyf.String(value)

# Embedded files
attachments = metadata.attachments + (options['attachments'] or [])
attachments = metadata.attachments.copy()
if options['attachments']:
for attachment in options['attachments']:
if not isinstance(attachment, Attachment):
attachment = Attachment(attachment)
attachments.append(attachment)
pdf_attachments = []
for attachment in attachments:
pdf_attachment = write_pdf_attachment(
pdf, attachment, document.url_fetcher, compress)
pdf_attachment = write_pdf_attachment(pdf, attachment, compress)
if pdf_attachment is not None:
pdf_attachments.append(pdf_attachment)
if pdf_attachments:
Expand Down
13 changes: 3 additions & 10 deletions weasyprint/pdf/anchors.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,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, compress)
pdf, Attachment(annot_target), compress)
annot_file = annot_files[annot_target]
if annot_file is None:
continue
Expand Down Expand Up @@ -281,18 +281,11 @@ def add_annotations(links, matrix, document, pdf, page, annot_files, compress):
page['Annots'].append(annot.reference)


def write_pdf_attachment(pdf, attachment, url_fetcher, compress):
def write_pdf_attachment(pdf, attachment, 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
url = ''
if isinstance(attachment, tuple):
url, description = attachment
attachment = Attachment(
url=url, url_fetcher=url_fetcher, description=description)
elif not isinstance(attachment, Attachment):
attachment = Attachment(guess=attachment, url_fetcher=url_fetcher)

url = None
uncompressed_length = 0
stream = b''
try:
Expand Down

0 comments on commit 287819b

Please sign in to comment.