This commit is contained in:
MarkLo 2025-11-25 02:50:00 +08:00
parent e3a3d09cff
commit acaac87ef3
1 changed files with 28 additions and 9 deletions

View File

@ -22,15 +22,30 @@ class PDFGenerator:
def __init__(self): def __init__(self):
"""Initialize PDF generator with Chinese font support""" """Initialize PDF generator with Chinese font support"""
# Try to register Chinese fonts (fallback to default if not available) # Register Chinese fonts using reportlab's built-in CID fonts
# These fonts support Chinese characters without requiring external font files
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
try: try:
# Try common Chinese font paths on different systems # Register Chinese font (Traditional Chinese support)
# macOS: /System/Library/Fonts/PingFang.ttc # 'STSong-Light' is a built-in CID font that supports Chinese characters
# Linux: /usr/share/fonts/truetype/ pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))
# For now, we'll use built-in fonts and handle Chinese with Unicode self.chinese_font = 'STSong-Light'
pass except Exception as e:
except Exception: # If CID font registration fails, try alternative method
pass try:
# Try STHeiti for better Traditional Chinese support
pdfmetrics.registerFont(UnicodeCIDFont('STHeiti-Light'))
self.chinese_font = 'STHeiti-Light'
except Exception:
# Last resort: use MSung for Traditional Chinese
try:
pdfmetrics.registerFont(UnicodeCIDFont('MSung-Light'))
self.chinese_font = 'MSung-Light'
except Exception:
# Fallback to basic font
self.chinese_font = 'Helvetica'
print("Warning: Could not register Chinese font, Chinese characters may not display correctly")
def generate_analyst_report_pdf( def generate_analyst_report_pdf(
self, self,
@ -69,10 +84,11 @@ class PDFGenerator:
# Define styles # Define styles
styles = getSampleStyleSheet() styles = getSampleStyleSheet()
# Custom styles with better Chinese support # Custom styles with Chinese font support
title_style = ParagraphStyle( title_style = ParagraphStyle(
'CustomTitle', 'CustomTitle',
parent=styles['Heading1'], parent=styles['Heading1'],
fontName=self.chinese_font,
fontSize=24, fontSize=24,
textColor=HexColor('#1a1a1a'), textColor=HexColor('#1a1a1a'),
spaceAfter=30, spaceAfter=30,
@ -82,6 +98,7 @@ class PDFGenerator:
subtitle_style = ParagraphStyle( subtitle_style = ParagraphStyle(
'CustomSubtitle', 'CustomSubtitle',
parent=styles['Normal'], parent=styles['Normal'],
fontName=self.chinese_font,
fontSize=12, fontSize=12,
textColor=HexColor('#666666'), textColor=HexColor('#666666'),
spaceAfter=20, spaceAfter=20,
@ -91,6 +108,7 @@ class PDFGenerator:
heading_style = ParagraphStyle( heading_style = ParagraphStyle(
'CustomHeading', 'CustomHeading',
parent=styles['Heading2'], parent=styles['Heading2'],
fontName=self.chinese_font,
fontSize=16, fontSize=16,
textColor=HexColor('#2c3e50'), textColor=HexColor('#2c3e50'),
spaceAfter=12, spaceAfter=12,
@ -100,6 +118,7 @@ class PDFGenerator:
body_style = ParagraphStyle( body_style = ParagraphStyle(
'CustomBody', 'CustomBody',
parent=styles['Normal'], parent=styles['Normal'],
fontName=self.chinese_font,
fontSize=10, fontSize=10,
leading=14, leading=14,
textColor=HexColor('#333333'), textColor=HexColor('#333333'),