Spaces:
Sleeping
Sleeping
Hussien Haider commited on
Commit Β·
ef91d8e
1
Parent(s): ad4494f
fix: embed actual ALPHA-LOGO.png as RGB JPEG in PDF (cached on disk)
Browse files
app/services/certificate_service.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""Certificate service: issuance, verification, PDF rendering."""
|
| 2 |
|
|
|
|
| 3 |
import io
|
| 4 |
import os
|
| 5 |
import random
|
|
@@ -110,6 +111,35 @@ def _ar(text: str) -> str:
|
|
| 110 |
return text
|
| 111 |
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# --------------------------------------------------------------------------- #
|
| 114 |
# Verifiability #
|
| 115 |
# --------------------------------------------------------------------------- #
|
|
@@ -254,34 +284,12 @@ def build_certificate_pdf(
|
|
| 254 |
c.setFillColor(GOLD)
|
| 255 |
c.rect(BAR - 1.5 * mm, 0, 1.5 * mm, H, fill=1, stroke=0)
|
| 256 |
|
| 257 |
-
# ββ Logo (
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
c.setStrokeColor(GOLD)
|
| 264 |
-
c.setLineWidth(1.0)
|
| 265 |
-
c.circle(cx_logo, cy_logo, r_logo, fill=0, stroke=1)
|
| 266 |
-
|
| 267 |
-
# inner circle (navy fill)
|
| 268 |
-
c.setFillColor(NAVY)
|
| 269 |
-
c.circle(cx_logo, cy_logo, r_logo - 0.8 * mm, fill=1, stroke=0)
|
| 270 |
-
|
| 271 |
-
# "A" letter (gold)
|
| 272 |
-
c.setFillColor(GOLD)
|
| 273 |
-
c.setFont(_font("bold"), 20)
|
| 274 |
-
c.drawCentredString(cx_logo, cy_logo - 7 * mm, "A")
|
| 275 |
-
|
| 276 |
-
# small shield accent below the A
|
| 277 |
-
sp = c.beginPath()
|
| 278 |
-
sp.moveTo(cx_logo, cy_logo - 9 * mm)
|
| 279 |
-
sp.lineTo(cx_logo + 3 * mm, cy_logo - 6 * mm)
|
| 280 |
-
sp.lineTo(cx_logo, cy_logo - 3 * mm)
|
| 281 |
-
sp.lineTo(cx_logo - 3 * mm, cy_logo - 6 * mm)
|
| 282 |
-
sp.close()
|
| 283 |
-
c.setFillColor(GOLD)
|
| 284 |
-
c.drawPath(sp, fill=1, stroke=0)
|
| 285 |
|
| 286 |
c.setFillColor(GOLD)
|
| 287 |
c.setFont(_font("bold"), 10)
|
|
|
|
| 1 |
"""Certificate service: issuance, verification, PDF rendering."""
|
| 2 |
|
| 3 |
+
import base64
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
import random
|
|
|
|
| 111 |
return text
|
| 112 |
|
| 113 |
|
| 114 |
+
# --------------------------------------------------------------------------- #
|
| 115 |
+
# Logo preparation (convert ALPHA-LOGO.png RGBA β RGB JPEG, cache on disk) #
|
| 116 |
+
# --------------------------------------------------------------------------- #
|
| 117 |
+
|
| 118 |
+
_LOGO_JPG_PATH: Optional[str] = None
|
| 119 |
+
|
| 120 |
+
def _prepare_logo():
|
| 121 |
+
global _LOGO_JPG_PATH
|
| 122 |
+
if _LOGO_JPG_PATH is not None:
|
| 123 |
+
return
|
| 124 |
+
try:
|
| 125 |
+
from PIL import Image as _PIL
|
| 126 |
+
from app.services.logo_data import LOGO_B64
|
| 127 |
+
raw = base64.b64decode(LOGO_B64)
|
| 128 |
+
pil = _PIL.open(io.BytesIO(raw))
|
| 129 |
+
if pil.mode != "RGB":
|
| 130 |
+
bg = _PIL.new("RGB", pil.size, (15, 27, 45))
|
| 131 |
+
if pil.mode == "RGBA":
|
| 132 |
+
bg.paste(pil, mask=pil.split()[3])
|
| 133 |
+
else:
|
| 134 |
+
bg.paste(pil)
|
| 135 |
+
pil = bg
|
| 136 |
+
path = os.path.join(tempfile.gettempdir(), "ca_alpha_logo.jpg")
|
| 137 |
+
pil.save(path, format="JPEG", quality=95)
|
| 138 |
+
_LOGO_JPG_PATH = path
|
| 139 |
+
except Exception:
|
| 140 |
+
_LOGO_JPG_PATH = ""
|
| 141 |
+
|
| 142 |
+
|
| 143 |
# --------------------------------------------------------------------------- #
|
| 144 |
# Verifiability #
|
| 145 |
# --------------------------------------------------------------------------- #
|
|
|
|
| 284 |
c.setFillColor(GOLD)
|
| 285 |
c.rect(BAR - 1.5 * mm, 0, 1.5 * mm, H, fill=1, stroke=0)
|
| 286 |
|
| 287 |
+
# ββ Logo: ALPHA-LOGO.png (RGB JPEG, cached on disk) ββ
|
| 288 |
+
_prepare_logo()
|
| 289 |
+
if _LOGO_JPG_PATH and os.path.exists(_LOGO_JPG_PATH):
|
| 290 |
+
logo_size = 22 * mm
|
| 291 |
+
c.drawImage(_LOGO_JPG_PATH, (BAR - logo_size) / 2, H - 36 * mm,
|
| 292 |
+
width=logo_size, height=logo_size, preserveAspectRatio=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
c.setFillColor(GOLD)
|
| 295 |
c.setFont(_font("bold"), 10)
|