Thank you knerz and vcc on Tiny ERP's official forum to provide us this solution.
感谢Tiny ERP官方网站上的vcc和knerz为我们提供了下面的解决方案
1. 获取中文字体文件 (本例中使用 arialuni.ttf (全称: "Arial Unicode MS"))
2. 复制至 tiny/bin/reportlab/fonts/
它的这个说明是针对reportlab1.x的(如果我记得没错的话),现在我们用的是reportlab2.x,这样你可以把字体文件拷贝到reportlab的目录下,这个目录是在python的安装目录下的site-package>reportlab>font目录。当然你将字体文件放在tiny/bin/reportlab/fonts/下也没有问题,只要用 ln 命令做一个链接到上面所述的目录下就可以了
3. 编辑 tiny/report/render/rml2pdf/trml2pdf.py, 插入以下代码:
encoding = 'UTF-8'
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle, StyleSheet1
def getZhSampleStyleSheet():
"""Returns a chinese stylesheet object"""
stylesheet = StyleSheet1()
stylesheet.add(ParagraphStyle(name='Normal',
fontName='Arial Unicode MS',
fontSize=10,
leading=12)
)
stylesheet.add(ParagraphStyle(name='BodyText',
parent=stylesheet['Normal'],
spaceBefore=6)
)
stylesheet.add(ParagraphStyle(name='Italic',
parent=stylesheet['BodyText'],
fontName = 'Arial Unicode MS')
)
stylesheet.add(ParagraphStyle(name='Heading1',
parent=stylesheet['Normal'],
fontName = 'Arial Unicode MS',
fontSize=18,
leading=22,
spaceAfter=6),
alias='h1')
stylesheet.add(ParagraphStyle(name='Title',
parent=stylesheet['Normal'],
fontName = 'Arial Unicode MS',
fontSize=18,
leading=22,
alignment=TA_CENTER,
spaceAfter=6),
alias='title')
stylesheet.add(ParagraphStyle(name='Heading2',
parent=stylesheet['Normal'],
fontName = 'Arial Unicode MS',
fontSize=14,
leading=18,
spaceBefore=12,
spaceAfter=6),
alias='h2')
stylesheet.add(ParagraphStyle(name='Heading3',
parent=stylesheet['Normal'],
fontName = 'Arial Unicode MS',
fontSize=12,
leading=14,
spaceBefore=12,
spaceAfter=6),
alias='h3')
stylesheet.add(ParagraphStyle(name='Bullet',
parent=stylesheet['Normal'],
firstLineIndent=0,
spaceBefore=3),
alias='bu')
stylesheet.add(ParagraphStyle(name='Definition',
parent=stylesheet['Normal'],
firstLineIndent=0,
leftIndent=36,
bulletIndent=0,
spaceBefore=6,
bulletFontName='Arial Unicode MS'),
alias='df')
stylesheet.add(ParagraphStyle(name='Code',
parent=stylesheet['Normal'],
fontName='Arial Unicode MS',
fontSize=8,
leading=8.8,
firstLineIndent=0,
leftIndent=36))
return stylesheet
4. 在同样的文件中搜索 reportlab.lib.styles.getSampleStyleSheet() 用 getZhSampleStyleSheet() 替换(共六处)
5. 编辑 tiny/report/render/rml2pdf/__init__.py, 插入以下代码:
from reportlab.lib.fonts import addMapping
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.cidfonts import CIDFont, findCMapFile
from reportlab.lib.fonts import addMapping
pdfmetrics.registerFont(TTFont('Arial Unicode MS','arialuni.ttf'))
for facename in ['Arial Unicode MS']:
addMapping(facename, 0, 0, facename) #normal
addMapping(facename, 0, 1, facename) #italic
addMapping(facename, 1, 0, facename) #bold
addMapping(facename, 1, 1, facename) #italic and bold
6.回到tinyerp的安装目录。使用以下命令使所有的报表使用Arial Unicode MS 字体:
find ./bin/addons -name "*.rml" | xargs grep -l 'Times-Roman' | xargs sed -i 's/Times-Roman/Arial Unicode MS/g';
find ./bin/addons -name "*.rml" | xargs grep -l 'Times-BoldItalic' | xargs sed -i 's/Times-BoldItalic/Arial Unicode MS/g';
find ./bin/addons -name "*.rml" | xargs grep -l 'Times-Bold' | xargs sed -i 's/Times-Bold/Arial Unicode MS/g';
find ./bin/addons -name "*.rml" | xargs grep -l 'Times-Italic' | xargs sed -i 's/Times-Italic/Arial Unicode MS/g';
find ./bin/addons -name "*.rml" | xargs grep -l 'Helvetica-BoldOblique' | xargs sed -i 's/Helvetica-BoldOblique/Arial Unicode MS/g';
find ./bin/addons -name "*.xsl" | xargs grep -l 'Helvetica-BoldOblique' | xargs sed -i 's/Helvetica-BoldOblique/Arial Unicode MS/g';
find ./bin/addons -name "*.rml" | xargs grep -l 'Helvetica-Bold' | xargs sed -i 's/Helvetica-Bold/Arial Unicode MS/g';
find ./bin/addons -name "*.xsl" | xargs grep -l 'Helvetica-Bold' | xargs sed -i 's/Helvetica-Bold/Arial Unicode MS/g';
find ./bin/addons -name "*.xsl" | xargs grep -l 'Helvetica-Oblique' | xargs sed -i 's/Helvetica-Oblique/Arial Unicode MS/g';
find ./bin/addons -name "*.rml" | xargs grep -l 'Helvetica' | xargs sed -i 's/Helvetica/Arial Unicode MS/g';
find ./bin/addons -name "*.xsl" | xargs grep -l 'Helvetica' | xargs sed -i 's/Helvetica/Arial Unicode MS/g';
find ./bin/addons -name "*.rml" | xargs grep -l 'Courier-Bold' | xargs sed -i 's/Courier-Bold/Arial Unicode MS/g';
find ./bin/addons -name "*.xsl" | xargs grep -l 'Courier' | xargs sed -i 's/Courier/Arial Unicode MS/g';
find ./bin/addons -name "*.rml" | xargs grep -l 'Courier' | xargs sed -i 's/Courier/Arial Unicode MS/g'
搞定!