fix: use correct Chinese font path on Linux Docker container
This commit is contained in:
@@ -12,13 +12,42 @@ from reportlab.pdfbase import pdfmetrics
|
|||||||
from reportlab.pdfbase.ttfonts import TTFont
|
from reportlab.pdfbase.ttfonts import TTFont
|
||||||
from reportlab.lib.enums import TA_CENTER, TA_LEFT
|
from reportlab.lib.enums import TA_CENTER, TA_LEFT
|
||||||
|
|
||||||
# 注册中文字体(包含常规和粗体)
|
# 注册中文字体
|
||||||
FONT_PATH = r"C:\Windows\Fonts\msyh.ttc"
|
# Windows 和 Linux 使用不同路径
|
||||||
FONT_BOLD_PATH = r"C:\Windows\Fonts\msyhbd.ttc"
|
if os.name == 'nt': # Windows
|
||||||
|
FONT_PATH = r"C:\Windows\Fonts\msyh.ttc"
|
||||||
|
FONT_BOLD_PATH = r"C:\Windows\Fonts\msyhbd.ttc"
|
||||||
|
else: # Linux (Docker)
|
||||||
|
# 直接尝试常见的中文字体路径
|
||||||
|
possible_paths = [
|
||||||
|
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
|
||||||
|
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
|
||||||
|
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
|
||||||
|
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
||||||
|
]
|
||||||
|
FONT_PATH = None
|
||||||
|
for p in possible_paths:
|
||||||
|
if os.path.exists(p):
|
||||||
|
FONT_PATH = p
|
||||||
|
break
|
||||||
|
if not FONT_PATH:
|
||||||
|
FONT_PATH = possible_paths[0] # 使用第一个作为默认值(会触发异常)
|
||||||
|
FONT_BOLD_PATH = FONT_PATH # 使用同一字体(不支持粗体分离)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pdfmetrics.registerFont(TTFont("Chinese", FONT_PATH))
|
pdfmetrics.registerFont(TTFont("Chinese", FONT_PATH))
|
||||||
pdfmetrics.registerFont(TTFont("Chinese-Bold", FONT_BOLD_PATH))
|
# 注册粗体(如果路径不同才注册,相同则复用)
|
||||||
|
if FONT_BOLD_PATH != FONT_PATH:
|
||||||
|
try:
|
||||||
|
pdfmetrics.registerFont(TTFont("Chinese-Bold", FONT_BOLD_PATH))
|
||||||
|
except:
|
||||||
|
pdfmetrics.registerFont(TTFont("Chinese-Bold", FONT_PATH))
|
||||||
|
else:
|
||||||
|
# 同一字体,注册为 Chinese-Bold 别名
|
||||||
|
try:
|
||||||
|
pdfmetrics.registerFont(TTFont("Chinese-Bold", FONT_PATH))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
CHINESE_FONT_OK = True
|
CHINESE_FONT_OK = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
CHINESE_FONT_OK = False
|
CHINESE_FONT_OK = False
|
||||||
|
|||||||
Reference in New Issue
Block a user