Enabling Arabic Support in OpenERP Reports:: Fixing ReportLab To enable RTL language support, we need to modify some of its code, and build two supporting libraries, namely FriBiDi2 and PyFribidi2. :: Building Dependencies First, remove older libraries and install libraries necessary for building the new sources. sudo apt-get purge libfribidi0 python-pyfribidi && sudo apt-get install autoconf gcc
Second, download FriBiDi2 extract it, open a shell in the extracted directory and execute the following command: ./configure && make && sudo make install Next, download PyFribidi2 extract it, open a shell in the extracted directory and execute the following command:
sed -i -r 's/\bfribidi2(\b|_)/fribidi\1/g' configure.in configure setup.py [ `python -d 'import sys; print sys.version[:]'` == '3.0' ] && sed -r -i 's/\bsite-packages\b/dist-packages/g' configure configure.in ./configure && make install
Finally, verify the installation by opening a shell, changing to the home directory, starting the Python interpreter and importing pyfribidi2: cd :: Modifying the code Three files need to be modified in ReportLab's code. The default installation of ReportLab in Ubuntu with Python 2.6 places it in the directory ”/usr/lib/python2.6/dist-packages/reportlab/”. We will need to modify the files there using an editor that is run in super user mode (e.g. `sudo vim`). I will refer to the files below using the relative path in Report Lab's director. rl_config.py We need to modify the search path that ReportLab uses to search for fonts to include the default path on Ubuntu. In the variable T1SearchPath, add the following value as the last element in the tuple: '/usr/share/fonts/truetype/type1', Do the same for TTFSearchPath, adding the following value as the last element in the tuple : '/usr/share/fonts/truetype/type1', And do the same for CMapSearchPath, adding the following value as the last element in the tuple: '/usr/share/fonts/cmap',
pdfgen/textobject.py We need to import PyFribidi2 and call it before text is output to the PDF. Add the following lines after the import statements: # try to import pyfribidi try: import pyfribidi2 as pyfribidi log2vis = pyfribidi.log2vis DIR_ON = pyfribidi.ON DIR_LTR = pyfribidi.LTR DIR_RTL = pyfribidi.RTL except: import warnings warnings.warn('pyfribidi is not installed - RTL not supported') log2vis = None Add a parameter to specify direction in PDFTextObject.init, so that the method declaration becomes as follows: def __init__(self, canvas, x=0,y=0, direction = 'LTR'): self.direction = direction
Finally, add a call to PyFribidi2 at the top of PDFTextObject._formatText , so that it becomes like this: def _formatText(self, text): "Generates PDF text output operator(s)" # Use pyfribidi to write the text in the correct visual order. directions = { 'LTR': DIR_LTR, 'RTL': DIR_RTL } text = log2vis(text, directions.get(self.direction, DIR_OFF))
platypus/paragraph.py Right before the comment ”#now the font for the rest of the paragraph”, add the following lines: # set the paragraph direction if self.style.wordWrap == 'RTL': tx.direction = 'RTL'
:: Fixing OpenERP The fixes below have been adapted from the ReportsUnicode wiki on OpenObject's website. However, the code in the wiki does not work correctly, so I have fixed a few things in it (and will hopefully fix the wiki soon). Only two files need to be changed: ”[bin/]report/render/rml2pdf/init.py” and ”[bin/]report/render/rml2pdf/trml2pdf.py”. report/render/rml2pdf/__init__.py Replace the code in this file by the following code: import os, stat from tools.misc import debug from reportlab import rl_config from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase import ttfonts from reportlab.lib.fonts import addMapping def rl_isreg(filename, dirname): try: st = os.stat(os.path.join(dirname, filename)) except OSError, reason: if reason.errno == 2: return False raise return stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode) for dirname in rl_config.TTFSearchPath: for root, dirs, files in os.walk(dirname): #@UnusedVariable for file in [x for x in files if x.upper().endswith('.ttf') and rl_isreg(x, root) ]: filename = os.path.join(root, file) try: face = ttfonts.TTFontFace(filename) face.extractInfo(1) pdfmetrics.registerFont(ttfonts.TTFont(face.name, filename, asciiReadable=1)) addMapping(face.familyName, face.bold, face.italic, face.name) except: pass from trml2pdf import parseString, parseNode report/render/rml2pdf/trml2pdf.py Right before the parseNode function, add the following function. It replaces a number of standard PDF fonts with alternatives that support Unicode characters. I haven't tried all of them, but Helvetica –> DejaVuSans works for me. def changeFonts(data): fontmap = { 'Times-Roman': 'DejaVuSerif', 'Times-BoldItalic': 'DejaVuSerif-BoldItalic', 'Times-Bold': 'DejaVuSerif-Bold', 'Times-Italic': 'DejaVuSerif-Italic', 'Helvetica': 'DejaVuSans', 'Helvetica-BoldItalic': 'DejaVuSans-BoldOblique', 'Helvetica-Bold': 'DejaVuSans-Bold', 'Helvetica-Italic': 'DejaVuSans-Oblique', 'Courier': 'DejaVuSansMono', 'Courier-Bold': 'DejaVuSansMono-Bold', 'Courier-BoldItalic': 'DejaVuSansMono-BoldOblique', 'Courier-Italic': 'DejaVuSansMono-Oblique', 'Helvetica-ExtraLight': 'DejaVuSans-ExtraLight', 'TimesCondensed-Roman': 'DejaVuSerifCondensed', 'TimesCondensed-BoldItalic': 'DejaVuSerifCondensed-BoldItalic', 'TimesCondensed-Bold': 'DejaVuSerifCondensed-Bold', 'TimesCondensed-Italic': 'DejaVuSerifCondensed-Italic', 'HelveticaCondensed': 'DejaVuSansCondensed', 'HelveticaCondensed-BoldItalic': 'DejaVuSansCondensed-BoldOblique', 'HelveticaCondensed-Bold': 'DejaVuSansCondensed-Bold', 'HelveticaCondensed-Italic': 'DejaVuSansCondensed-Oblique', } for old, new in fontmap.iteritems(): data = data.replace('"'+old+'"', '"'+new+'"') return data
That should be all. OpenERP should be all set and ready for creating Arabic reports.The following is the partner label report:
|









