[reportlab-users] encoding issue in utf8str() in pdfdoc.py

杨英超 yangyingchao at gmail.com
Tue Oct 26 09:19:42 EDT 2010


Hi, all:
I'm a Chinese programmer, using python 2.6 and reportlab 2.3.

When I tried to generate a pdf file whose path name included some Chinese characters, under Windows XP, some error occured:

Traceback (most recent call last):
File "D:\python\byj1018\main.py", line 1278, in MenuPrint
pdf.build(story)
File "C:\Python26\Lib\site-packages\reportlab\platypus\doctemplate.py", line 1110, in build
BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
File "C:\Python26\Lib\site-packages\reportlab\platypus\doctemplate.py", line 903, in build
self._endBuild()
File "C:\Python26\Lib\site-packages\reportlab\platypus\doctemplate.py", line 845, in _endBuild
if getattr(self,'_doSave',1): self.canv.save()
File "C:\Python26\Lib\site-packages\reportlab\pdfgen\canvas.py", line 1091, in save
self._doc.SaveToFile(self._filename, self)
File "C:\Python26\Lib\site-packages\reportlab\pdfbase\pdfdoc.py", line 234, in SaveToFile
f = open(filename, "wb")
IOError: [Errno 2] No such file or directory: 'D:\\python\\byj1018\\data\\IQC\xe8\xaf\x95\xe9\xaa\x8c3\\IQC\xe8\xaf\x95\xe9\xaa\x8c3.pdf'

But the same code went well under Linux.
I thought it was caused by the wrong encoding by the function utf8str() in pdfdoc.py. In that file, utf8 was always
used to encode unicode string. But Windows XP does not recognise the utf8 string by default. Then I modified that function a bit,
things went well.
So I suggest to get system default encoding from sys.stdin instead of the fixed utf8, such as:

import sys
def utf8str(x):
if isinstance(x,unicode):
return x.encode(sys.stdin.encoding)
else:
return str(x)

Best regards
Tubo
--
杨英超 <yangyingchao at gmail.com>


More information about the reportlab-users mailing list