Generate PDF documents with proper Chinese character support using reportlab. Automatically handles font registration and provides helper functions for creating Chinese PDFs without garbled text issues. Use when generating any PDF containing Chinese characters.
Standardmäßig ist der Prompt ausgewählt, der zuerst die Quelle prüft. Sie können zu einem direkten Befehl wechseln oder eine lokale Kopie herunterladen.
Quelldateien prüfen
Lesen Sie SKILL.md und alle von SkillsMP angezeigten Begleitdateien, bevor Sie sich für eine Installation entscheiden.
Mit Codex oder Claude installieren Kopieren Sie diesen Prompt, fügen Sie ihn in Codex, Claude oder einen anderen Assistant ein und lassen Sie die Skill-Seite prüfen und installieren.
Ein direkter Befehl überspringt den Prüf-Prompt. Prüfen Sie die Quelle, bevor Sie ihn ausführen.
Generate PDF documents with proper Chinese character support using reportlab. Automatically handles font registration and provides helper functions for creating Chinese PDFs without garbled text issues. Use when generating any PDF containing Chinese characters.
PDF Chinese Support
Overview
When using reportlab to generate PDFs, Chinese fonts are not supported by default, causing Chinese text to display as garbled characters (squares or question marks). This skill provides a complete solution to ensure Chinese displays correctly in all PDF elements.
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import Paragraph
# Step 1: Register Chinese font
pdfmetrics.registerFont(TTFont('ChineseFont', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'))
# Step 2: Create style with Chinese font
chinese_style = ParagraphStyle(
'ChineseStyle',
fontName='ChineseFont',
fontSize=12,
)
# Step 3: Wrap all Chinese text with Paragraph
text = Paragraph("你好,世界!", chinese_style)
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
# MUST do this before creating any PDF content
pdfmetrics.registerFont(TTFont('ChineseFont', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'))
2. Use Paragraph for All Text
from reportlab.platypus import Paragraph
from reportlab.lib.styles import ParagraphStyle
# Create Chinese style
style = ParagraphStyle('Chinese', fontName='ChineseFont', fontSize=12)
# Wrap ALL text with Paragraph - never use raw strings
title = Paragraph("中文标题", style)
content = Paragraph("中文内容", style)
3. Tables Must Use Paragraph Cells
from reportlab.platypus import Table
# Convert all cell values to Paragraph
table_data = []
for row in data:
table_row = [Paragraph(str(cell), chinese_style) for cell in row]
table_data.append(table_row)
table = Table(table_data)
table.setStyle(TableStyle([
('FONTNAME', (0, 0), (-1, -1), 'ChineseFont'), # All cells
]))
4. Clean HTML/Formatting
import re
defclean_text(text):
"""Remove HTML tags and normalize whitespace"""
text = re.sub(r'<[^>]+>', '', str(text))
text = text.replace('\n', ' ').replace('\r', ' ')
text = ' '.join(text.split())
return text
# Always clean text before creating Paragraph
cleaned = clean_text(raw_text)
para = Paragraph(cleaned, chinese_style)
Complete Example
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import cm
# 1. Register font
pdfmetrics.registerFont(TTFont('ChineseFont', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'))
# 2. Create document
doc = SimpleDocTemplate("output.pdf", pagesize=A4)
elements = []
# 3. Define styles
title_style = ParagraphStyle('Title', fontName='ChineseFont', fontSize=18, leading=24)
body_style = ParagraphStyle('Body', fontName='ChineseFont', fontSize=12, leading=20)
# 4. Add content
elements.append(Paragraph("报告标题", title_style))
elements.append(Spacer(1, 0.5*cm))
elements.append(Paragraph("这是报告正文内容。", body_style))
# 5. Create table with Chinese
data = [
[Paragraph('列1', body_style), Paragraph('列2', body_style)],
[Paragraph('数据1', body_style), Paragraph('数据2', body_style)],
]
table = Table(data)
table.setStyle(TableStyle([
('FONTNAME', (0, 0), (-1, -1), 'ChineseFont'),
('GRID', (0, 0), (-1, -1), 1, colors.black),
]))
elements.append(table)
# 6. Build PDF
doc.build(elements)
Troubleshooting
Issue: Chinese shows as squares or ???
Cause: Font not registered or not applied to text element.
Solution:
Check font exists: ls -la /usr/share/fonts/truetype/wqy/
Ensure pdfmetrics.registerFont() is called BEFORE creating any content
Verify all text uses Paragraph(text, chinese_style)
Cause: Font doesn't support all Unicode characters.
Solution: Try different font:
# Try Noto Sans CJK for better Unicode support
pdfmetrics.registerFont(TTFont('ChineseFont', '/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc'))
Helper Functions
The skill provides chinese_pdf_helper.py with these utilities:
register_chinese_font() - Auto-detect and register available Chinese font
create_chinese_style() - Create ParagraphStyle with Chinese font
clean_text() - Remove HTML tags and normalize text
create_paragraph() - Create Paragraph with automatic text cleaning
create_table_with_chinese() - Create Table with all cells as Paragraphs
generate_pdf() - High-level function to generate complete PDF
Resources
scripts/check_fonts.py - Check available Chinese fonts
scripts/generate_chinese_pdf.py - Basic example
scripts/chinese_pdf_helper.py - Complete helper module with all utilities