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

Henning von Bargen henning.vonbargen at arcor.de
Tue Oct 26 14:43:58 EDT 2010



> 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

> --

Probably not a good idea.
Instead, you better remove the 2 calls to utf8str in saveToFile
entirely and use the filename as is.
Then, when you specify the filename argument, use a unicode
object instead of a byte-string (the built-in open works fine with unicode).


More information about the reportlab-users mailing list