Skip to content

Commit 2a9aa87

Browse files
committed
v4.0.1
Signed-off-by: TIANHE <TIANHE@GMAIL.COM>
1 parent 234224a commit 2a9aa87

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

backend_api_python/app/routes/ai_chat.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,28 +2372,43 @@ def save_local_chat_message():
23722372
return jsonify({"code": 0, "msg": str(e), "data": None}), 500
23732373

23742374

2375-
def _register_report_pdf_font() -> str:
2375+
def _has_cjk_text(value: Any) -> bool:
2376+
text = _plain_text(value)
2377+
return bool(re.search(r"[\u2e80-\u9fff\uac00-\ud7af\u3040-\u30ff]", text))
2378+
2379+
2380+
def _register_report_pdf_font(prefer_cjk: bool = False) -> str:
23762381
from pathlib import Path
23772382

23782383
from reportlab.pdfbase import pdfmetrics
23792384
from reportlab.pdfbase.ttfonts import TTFont
23802385

23812386
candidates = [
2382-
"C:/Windows/Fonts/msyh.ttc",
2383-
"C:/Windows/Fonts/msyh.ttf",
2384-
"C:/Windows/Fonts/simsun.ttc",
2385-
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
2386-
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
2387-
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
2388-
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
2387+
("C:/Windows/Fonts/msyh.ttc", True),
2388+
("C:/Windows/Fonts/msyh.ttf", True),
2389+
("C:/Windows/Fonts/simsun.ttc", True),
2390+
("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", True),
2391+
("/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc", True),
2392+
("/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", True),
2393+
("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", False),
23892394
]
2390-
for path in candidates:
2395+
for path, is_cjk in candidates:
2396+
if prefer_cjk and not is_cjk:
2397+
continue
23912398
try:
23922399
if Path(path).exists():
23932400
pdfmetrics.registerFont(TTFont("QuantDingerSans", path))
23942401
return "QuantDingerSans"
23952402
except Exception as e:
23962403
logger.debug(f"Failed to register PDF font {path}: {e}")
2404+
if prefer_cjk:
2405+
try:
2406+
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
2407+
2408+
pdfmetrics.registerFont(UnicodeCIDFont("STSong-Light"))
2409+
return "STSong-Light"
2410+
except Exception as e:
2411+
logger.debug(f"Failed to register built-in CJK PDF font: {e}")
23972412
return "Helvetica"
23982413

23992414

@@ -2406,7 +2421,8 @@ def _build_ai_report_pdf(report: dict, target: dict | None = None, language: str
24062421

24072422
target = target or {}
24082423
zh = str(language or "").lower().startswith("zh")
2409-
font_name = _register_report_pdf_font()
2424+
prefer_cjk = zh or _has_cjk_text(report) or _has_cjk_text(target)
2425+
font_name = _register_report_pdf_font(prefer_cjk=prefer_cjk)
24102426
title_font = font_name
24112427
width, height = A4
24122428
margin = 18 * mm

0 commit comments

Comments
 (0)