How to enable RML Reports to be able to print chinese characters?
- Make sure you have a chinese fonts (Simsun, Simhei, WenQuanYiZhenHei)
- Embed your font into RML library and settings or we may say “register font that you want to use in RML”
- Change the fontName in your RML Report (such as: slow_moving_report.rml)
LET’S GET STARTED :
In this case I want to use Simsun font in my Slow moving report
(FYI : I do this in Ubuntu 10.04)
How to have a chinese fonts?
You can installed manually or via terminal
• If you want to install manually
- Go to C:\WINDOWS\Fonts in your windows OS
- Copy type font you want (.ttf / .ttc)
- Copy to your Ubuntu OS
- Double click on the font and install
• If you want to install via terminal, you can refer to this link : http://xgwang.wordpress.com/2007/11/25/set-up-chinese-fonts-using-microsofts-simsunttf-in-ubuntu-71-gutsy/
After you have a chinese font in your OS, now we can register your font to report lab
Note : your report lab must be version 2.0 above
You can refer to this link : http://forum.openerp.com/forum/topic1943.html
But I do it in slightly different way
First, create a folder called “fonts” in this path /usr/lib/python2.6/dist_packages/reportlab/
And put SIMSUN.TTC in it
Note : the location where reportlab looking for TTF-files can be found here : rl_config.py
(you can search the file in your file system)
Second, after that go to /usr/share/pyshared/reportlab/lib/styles.py
And put this code below def getSampleStyleSheet(): (this one id the original one)
def getSampleStyleSheet_JH():
"""Returns a chinese stylesheet object"""
stylesheet = StyleSheet1()
stylesheet.add(ParagraphStyle(name='Normal',
fontName='Simsun',
fontSize=10,
leading=12)
)
stylesheet.add(ParagraphStyle(name='BodyText',
parent=stylesheet['Normal'],
spaceBefore=6)
)
stylesheet.add(ParagraphStyle(name='Italic',
parent=stylesheet['BodyText'],
fontName = 'Simsun-Italic')
)
stylesheet.add(ParagraphStyle(name='Heading1',
parent=stylesheet['Normal'],
fontName = 'Simsun-Bold',
fontSize=18,
leading=22,
spaceAfter=6),
alias='h1')
stylesheet.add(ParagraphStyle(name='Title',
parent=stylesheet['Normal'],
fontName = 'Simsun-Bold',
fontSize=18,
leading=22,
alignment=TA_CENTER,
spaceAfter=6),
alias='title')
stylesheet.add(ParagraphStyle(name='Heading2',
parent=stylesheet['Normal'],
fontName = 'Simsun-Bold',
fontSize=14,
leading=18,
spaceBefore=12,
spaceAfter=6),
alias='h2')
stylesheet.add(ParagraphStyle(name='Heading3',
parent=stylesheet['Normal'],
fontName = 'Simsun-BoldItalic',
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='BoldItalic'),
alias='df')
stylesheet.add(ParagraphStyle(name='Code',
parent=stylesheet['Normal'],
fontName='Courier',
fontSize=8,
leading=8.8,
firstLineIndent=0,
leftIndent=36))
Third, go to /opt/openerp61/server/openerp/report/render/rml2pdf/init.py
And change into this :
from trml2pdf import parseString, parseNode
from reportlab.lib.fonts import addMapping
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.cidfonts import UnicodeCIDFont, findCMapFile
pdfmetrics.registerFont(TTFont('SimSun','SIMSUN.TTC')) # the font name you put here must exactly the same with the one you copy in “fonts” folder in step 1
for facename in ['SimSun']:
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
Forth, go to /opt/openerp61/server/openerp/report/render/rml2pdf/trml2pdf.py
• Change your encoding from “cp1252” to “UTF-8”
• Add following code:
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.styles import getSampleStyleSheet_JH, ParagraphStyle, StyleSheet1
• Replace all “getSampleStyleSheet” to “getSampleStyleSheet_JH”
Last step restart your openerp service TWICE
Note : if when you restart your opener service, it prompt out “start-stop-daemon: warning: failed to kill 5445: No such process”, you can commented the code in step 3 and find out what the error and uncommented again but don’t forget to restart your service again