Hussien Haider commited on
Commit
4a95521
Β·
1 Parent(s): 5393837

fix: move logo prep to import time to ensure it runs

Browse files
Files changed (1) hide show
  1. app/services/certificate_service.py +18 -23
app/services/certificate_service.py CHANGED
@@ -102,32 +102,28 @@ def _ar(text: str) -> str:
102
 
103
 
104
  # --------------------------------------------------------------------------- #
105
- # Logo preparation (convert ALPHA-LOGO.png RGBA β†’ RGB JPEG, cache on disk) #
106
  # --------------------------------------------------------------------------- #
107
 
108
  _LOGO_JPG_PATH: Optional[str] = None
109
 
110
- def _prepare_logo():
111
- global _LOGO_JPG_PATH
112
- if _LOGO_JPG_PATH is not None:
113
- return
114
- try:
115
- from PIL import Image as _PIL
116
- from app.services.logo_data import LOGO_B64
117
- raw = base64.b64decode(LOGO_B64)
118
- pil = _PIL.open(io.BytesIO(raw))
119
- if pil.mode != "RGB":
120
- bg = _PIL.new("RGB", pil.size, (15, 27, 45))
121
- if pil.mode == "RGBA":
122
- bg.paste(pil, mask=pil.split()[3])
123
- else:
124
- bg.paste(pil)
125
- pil = bg
126
- path = os.path.join(tempfile.gettempdir(), "ca_alpha_logo.jpg")
127
- pil.save(path, format="JPEG", quality=95)
128
- _LOGO_JPG_PATH = path
129
- except Exception:
130
- _LOGO_JPG_PATH = ""
131
 
132
 
133
  # --------------------------------------------------------------------------- #
@@ -287,7 +283,6 @@ def build_certificate_pdf(
287
  c.rect(0, H - i * 8*mm, BAR, 8*mm, fill=1, stroke=0)
288
 
289
  # ── Logo: ALPHA-LOGO.png ──
290
- _prepare_logo()
291
  if _LOGO_JPG_PATH and os.path.exists(_LOGO_JPG_PATH):
292
  logo_size = 20 * mm
293
  logo_y = H - 34 * mm
 
102
 
103
 
104
  # --------------------------------------------------------------------------- #
105
+ # Logo preparation (convert ALPHA-LOGO.png RGBA β†’ RGB JPEG, at import time) #
106
  # --------------------------------------------------------------------------- #
107
 
108
  _LOGO_JPG_PATH: Optional[str] = None
109
 
110
+ try:
111
+ from PIL import Image as _PIL
112
+ from app.services.logo_data import LOGO_B64
113
+ _raw = base64.b64decode(LOGO_B64)
114
+ _pil = _PIL.open(io.BytesIO(_raw))
115
+ if _pil.mode != "RGB":
116
+ _bg = _PIL.new("RGB", _pil.size, (15, 27, 45))
117
+ if _pil.mode == "RGBA":
118
+ _bg.paste(_pil, mask=_pil.split()[3])
119
+ else:
120
+ _bg.paste(_pil)
121
+ _pil = _bg
122
+ _path = os.path.join(tempfile.gettempdir(), "ca_alpha_logo.jpg")
123
+ _pil.save(_path, format="JPEG", quality=95)
124
+ _LOGO_JPG_PATH = _path
125
+ except Exception:
126
+ _LOGO_JPG_PATH = ""
 
 
 
 
127
 
128
 
129
  # --------------------------------------------------------------------------- #
 
283
  c.rect(0, H - i * 8*mm, BAR, 8*mm, fill=1, stroke=0)
284
 
285
  # ── Logo: ALPHA-LOGO.png ──
 
286
  if _LOGO_JPG_PATH and os.path.exists(_LOGO_JPG_PATH):
287
  logo_size = 20 * mm
288
  logo_y = H - 34 * mm