.memstack/skills/pdf-chinese-support/SKILL.md
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.
npx skillsauth add s1366560/agi-demos pdf-chinese-supportInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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 scripts.chinese_pdf_helper import ChinesePDFHelper
# Create helper instance
helper = ChinesePDFHelper()
# Generate PDF with Chinese content
helper.generate_pdf(
output_path="report.pdf",
title="中文报告标题",
sections=[
{"heading": "第一节", "content": "这是第一节的内容。"},
{"heading": "第二节", "content": "这是第二节的内容,包含表格。"},
],
table_data=[
["姓名", "职位", "部门"],
["张三", "经理", "销售部"],
["李四", "工程师", "技术部"],
]
)
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)
| Font Name | Path | Priority |
|-----------|------|----------|
| WenQuanYi Zen Hei | /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc | 1 |
| WenQuanYi Micro Hei | /usr/share/fonts/truetype/wqy/wqy-microhei.ttc | 2 |
| Noto Sans CJK | /usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc | 3 |
| Droid Sans Fallback | /usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf | 4 |
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'))
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)
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
]))
import re
def clean_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)
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)
Cause: Font not registered or not applied to text element.
Solution:
ls -la /usr/share/fonts/truetype/wqy/pdfmetrics.registerFont() is called BEFORE creating any contentParagraph(text, chinese_style)Solution:
# Install Chinese fonts
sudo apt-get update
sudo apt-get install -y fonts-wqy-zenhei fonts-wqy-microhei
fc-cache -fv
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'))
The skill provides chinese_pdf_helper.py with these utilities:
register_chinese_font() - Auto-detect and register available Chinese fontcreate_chinese_style() - Create ParagraphStyle with Chinese fontclean_text() - Remove HTML tags and normalize textcreate_paragraph() - Create Paragraph with automatic text cleaningcreate_table_with_chinese() - Create Table with all cells as Paragraphsgenerate_pdf() - High-level function to generate complete PDFscripts/check_fonts.py - Check available Chinese fontsscripts/generate_chinese_pdf.py - Basic examplescripts/chinese_pdf_helper.py - Complete helper module with all utilitiestools
Sandbox MCP Server 是一个隔离的代码执行环境,提供完整的文件系统操作、命令执行、 代码分析、测试运行和远程桌面能力。当你需要执行代码、操作文件、运行测试、 分析代码结构、或需要图形界面操作时使用此技能。支持 Python、Node.js、Java 等多语言环境。
tools
Replace with description of the skill and when Claude should use it.
development
Generate high-quality images using ModelScope's Z-Image API. Use this skill when the user wants to generate images using the specific Z-Image model or ModelScope API they provided. Trigger words: 'Zimage', 'ModelScope', 'generate zimage'.
tools
No-code automation democratizes workflow building. Zapier and Make (formerly Integromat) let non-developers automate business processes without writing code. But no-code doesn't mean no-complexity ...