Skip to content

Commit 0f1c491

Browse files
committed
logo
1 parent 0d09683 commit 0f1c491

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

src/iosanita/contenttypes/browser/export_view.py

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,73 @@
88
from zExceptions import NotFound
99
from zope.interface import implementer
1010
from zope.publisher.interfaces import IPublishTraverse
11-
11+
from plone.memoize import forever
1212
import csv
1313
import importlib.resources
1414
import logging
1515
import re
16-
16+
from PIL import Image
17+
import base64
18+
import imghdr
1719

1820
logger = logging.getLogger(__name__)
1921

2022
fontools_logger = logging.getLogger("fontTools.subset")
2123
fontools_logger.setLevel(logging.WARNING)
2224

2325

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+
2478
class IExportViewTraverser(IPublishTraverse):
2579
"""
2680
Marker interface for Download views
@@ -193,6 +247,9 @@ def pdf_styles(self):
193247

194248
def pdf_title(self):
195249
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()}"
196253
return context.Title()
197254

198255
def pdf_description(self):
@@ -213,10 +270,10 @@ def pdf_cell_format(self, column, value):
213270
return {"type": "str", "value": value.split("T")[0]}
214271
return {"type": "str", "value": str(value)}
215272

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())
220277
return None
221278

222279
def pdf_last_update(self):

src/iosanita/contenttypes/browser/static/export_pdf.css

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ body {
3434
}
3535
header {
3636
position: running(header);
37-
width: 20em;
37+
width: 100%;
38+
display: flex;
39+
align-items: center;
3840
}
3941

4042
/* header .site_data {
@@ -60,7 +62,11 @@ a {
6062
color: #235295
6163
}
6264
h1 {
63-
font-weight: 700;
65+
font-weight: 500;
66+
font-size: 18px;
67+
}
68+
#logo {
69+
padding-right: 1em;
6470
}
6571

6672
/* p.description {

src/iosanita/contenttypes/browser/templates/export_pdf.pt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
</head>
1515
<body>
1616
<header class="header"
17-
tal:define="title context/pdf_title|nothing; logob64 context/pdf_logob64|nothing"
17+
tal:define="title context/pdf_title|nothing; logo context/pdf_logo|nothing"
1818
tal:condition="title">
19-
<img id="logo" tal:condition="logob64" tal:attributes="src logob64"/>
19+
<div id="logo" taal:condition="logo" tal:content="structure logo"/>
2020
<h1 class="title">${title}</h1>
2121
</header>
2222

0 commit comments

Comments
 (0)