8
8
from zExceptions import NotFound
9
9
from zope .interface import implementer
10
10
from zope .publisher .interfaces import IPublishTraverse
11
-
11
+ from plone . memoize import forever
12
12
import csv
13
13
import importlib .resources
14
14
import logging
15
15
import re
16
-
16
+ from PIL import Image
17
+ import base64
18
+ import imghdr
17
19
18
20
logger = logging .getLogger (__name__ )
19
21
20
22
fontools_logger = logging .getLogger ("fontTools.subset" )
21
23
fontools_logger .setLevel (logging .WARNING )
22
24
23
25
26
+ # @forever.memoize
27
+ def image_to_html (input_string ):
28
+ """
29
+ Convert image data to a base64 string formatted for HTML.
30
+
31
+ Args:
32
+ - input_string: The string containing the filename and base64 encoded image data.
33
+
34
+ Returns:
35
+ - HTML.
36
+ """
37
+
38
+ if not input_string :
39
+ return ""
40
+
41
+ # Split the input string to extract the filename and base64 data
42
+ parts = input_string .split (";" )
43
+ datab64 = parts [1 ].split (":" )[1 ]
44
+
45
+ # Decode the image data from base64
46
+ image_data = base64 .b64decode (datab64 )
47
+
48
+ if image_data [:5 ] == b"<?xml" :
49
+ # https://github.com/Kozea/WeasyPrint/issues/75
50
+ # anche se il ticket risulta chiuso gli svg non risultano correttamente gestiti
51
+ # return image_data
52
+ # return f'<img src="data:image/svg+xml;charset=utf-8;base64,{datab64}">'
53
+ # XXX: se non si va decode/encode il b64 non risulta corretto (!)
54
+ return f'<img src="data:image/svg+xml;charset=utf-8;base64,{ base64 .b64encode (image_data ).decode ()} ">'
55
+
56
+ # Guess the image format
57
+ image_format = imghdr .what (None , image_data )
58
+
59
+ if not image_format :
60
+ # raise ValueError("Unable to determine image format")
61
+ logger .warning ("site logo, unable to determine image format" )
62
+ return ""
63
+
64
+ # Open the image from the decoded data
65
+ img = Image .open (BytesIO (image_data ))
66
+
67
+ # Create a buffer to hold the image data
68
+ buffered = BytesIO ()
69
+ img .save (buffered , format = image_format )
70
+
71
+ # Encode the image data to base64
72
+ img_base64 = base64 .b64encode (buffered .getvalue ()).decode ("utf-8" )
73
+
74
+ # Format the base64 string for HTML
75
+ return f'<img class="logo" src="data:{ image_format } ;base64,{ img_base64 } ">'
76
+
77
+
24
78
class IExportViewTraverser (IPublishTraverse ):
25
79
"""
26
80
Marker interface for Download views
@@ -193,6 +247,9 @@ def pdf_styles(self):
193
247
194
248
def pdf_title (self ):
195
249
context = self .context .context
250
+ site_title = api .portal .get_registry_record ("plone.site_title" )
251
+ if site_title :
252
+ return f"{ site_title } : { context .Title ()} "
196
253
return context .Title ()
197
254
198
255
def pdf_description (self ):
@@ -213,10 +270,10 @@ def pdf_cell_format(self, column, value):
213
270
return {"type" : "str" , "value" : value .split ("T" )[0 ]}
214
271
return {"type" : "str" , "value" : str (value )}
215
272
216
- def pdf_logob64 (self ):
217
- """
218
- TODO
219
- """
273
+ def pdf_logo (self ):
274
+ site_logo = api . portal . get_registry_record ( "plone.site_logo" )
275
+ if site_logo :
276
+ return image_to_html ( site_logo . decode ())
220
277
return None
221
278
222
279
def pdf_last_update (self ):
0 commit comments