mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-09-11 20:47:56 +03:00
Added missing PDF/a-3 attributes to Embedded File
SubType, CreationDate and ModDate were added
This commit is contained in:
parent
576e43baa3
commit
e9edc43f64
@ -6,6 +6,8 @@ importing sub-modules.
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
import datetime
|
||||
import os
|
||||
from pathlib import Path
|
||||
from urllib.parse import urljoin
|
||||
|
||||
@ -267,11 +269,36 @@ class Attachment:
|
||||
"""
|
||||
def __init__(self, guess=None, filename=None, url=None, file_obj=None,
|
||||
string=None, base_url=None, url_fetcher=default_url_fetcher,
|
||||
description=None):
|
||||
description=None, created=None, modified=None,
|
||||
af_relationship="Source"):
|
||||
self.source = _select_source(
|
||||
guess, filename, url, file_obj, string, base_url=base_url,
|
||||
url_fetcher=url_fetcher)
|
||||
self.description = description
|
||||
self.af_relationship = af_relationship
|
||||
|
||||
def epoch_to_pdf(epoch):
|
||||
dt_object = datetime.datetime.fromtimestamp(epoch)
|
||||
return datetime_to_pdf(dt_object)
|
||||
|
||||
def datetime_to_pdf(dt_object):
|
||||
return dt_object.strftime("D:%Y%m%d%H%M%SZ")
|
||||
|
||||
if created:
|
||||
self.created = created
|
||||
else:
|
||||
if filename:
|
||||
self.created = epoch_to_pdf(os.path.getctime(filename))
|
||||
else:
|
||||
self.created = datetime_to_pdf(datetime.datetime.now())
|
||||
|
||||
if modified:
|
||||
self.modified = modified
|
||||
else:
|
||||
if filename:
|
||||
self.modified = epoch_to_pdf(os.path.getmtime(filename))
|
||||
else:
|
||||
self.modified = datetime_to_pdf(datetime.datetime.now())
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
import hashlib
|
||||
import io
|
||||
import mimetypes
|
||||
import zlib
|
||||
from os.path import basename
|
||||
from urllib.parse import unquote, urlsplit
|
||||
@ -270,12 +271,19 @@ def write_pdf_attachment(pdf, attachment, url_fetcher):
|
||||
stream += compressed
|
||||
compressed = compress.flush(zlib.Z_FINISH)
|
||||
stream += compressed
|
||||
mime_type, _ = mimetypes.guess_type(url, strict=False)
|
||||
if not mime_type:
|
||||
mime_type = 'application/octet-stream'
|
||||
mime_type = '/' + mime_type.replace('/', '#2f')
|
||||
file_extra = pydyf.Dictionary({
|
||||
'Type': '/EmbeddedFile',
|
||||
'Filter': '/FlateDecode',
|
||||
"Subtype": mime_type,
|
||||
'Params': pydyf.Dictionary({
|
||||
'CheckSum': f'<{md5.hexdigest()}>',
|
||||
'Size': uncompressed_length,
|
||||
'CreationDate': attachment.created,
|
||||
'ModDate': attachment.modified,
|
||||
})
|
||||
})
|
||||
file_stream = pydyf.Stream([stream], file_extra)
|
||||
@ -296,10 +304,14 @@ def write_pdf_attachment(pdf, attachment, url_fetcher):
|
||||
'Type': '/Filespec',
|
||||
'F': pydyf.String(),
|
||||
'UF': pydyf.String(filename),
|
||||
"AFRelationship": "/Data",
|
||||
'EF': pydyf.Dictionary({'F': file_stream.reference}),
|
||||
'Desc': pydyf.String(attachment.description or ''),
|
||||
})
|
||||
pdf.add_object(attachment)
|
||||
if "AF" not in pdf.catalog:
|
||||
pdf.catalog["AF"] = pydyf.Array()
|
||||
pdf.catalog["AF"].append(attachment.reference)
|
||||
return attachment
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user