[reportlab-users] IronPython and ReportLab

Marco Parenzan marco.parenzan at libero.it
Thu Aug 20 04:01:07 EDT 2009


Dear All,



I'm trying to use IronPython 2.0.2 with ReportLab PDF. I have downloaded it
(version 2.3) with FePy for unicodedata.py.

I used this code for testing (from Magnus Lie Hetland "Beginning Python"):



from reportlab.graphics.shapes import Drawing, String

from reportlab.graphics import renderPDF



d = Drawing(100, 100)

s = String(50, 50, "Hello World", textAlign='middle')



d.add(s)



renderPDF.drawToFile(d, "HelloWorld.pdf", "HW")



The execution fails:



Traceback (most recent call last):

File "renderPDF01.py", line 9, in renderPDF01.py

File "C:\marco.parenzan\Python\reportlab\graphics\renderPDF.py", line 268,
in drawToFile

File "C:\marco.parenzan\Python\reportlab\pdfgen\canvas.py", line 947, in
save

File "C:\marco.parenzan\Python\reportlab\pdfbase\pdfdoc.py", line 223, in
SaveToFile

File "C:\marco.parenzan\Python\reportlab\pdfbase\pdfdoc.py", line 245, in
GetPDFData

File "C:\marco.parenzan\Python\reportlab\pdfbase\pdfdoc.py", line 422, in
format

File "C:\marco.parenzan\Python\reportlab\pdfbase\pdfdoc.py", line 883, in
format

File "mscorlib", line unknown, in GetString

File "mscorlib", line unknown, in GetChars

File "mscorlib", line unknown, in Fallback

File "mscorlib", line unknown, in Throw

UnicodeDecodeError: ('unknown', u'\x93', 11, 12, '')



The failed row shows:



882: def format(self, document):

883: strings = map(str, self.strings) # final conversion, in case of
lazy objects

884: return string.join(strings, "")



I have discovered that the problem is in this string, that is the header for
the PDF file:



# Following Ken Lunde's advice and the PDF spec, this includes

# some high-order bytes. I chose the characters for Tokyo

# in Shift-JIS encoding, as these cannot be mistaken for

# any other encoding, and we'll be able to tell if something

# has run our PDF files through a dodgy Unicode conversion.

PDFHeader = (

"%PDF-1.3"+LINEEND+

"%\223\214\213\236 ReportLab Generated PDF document http://www.reportlab.com
<http://www.reportlab.com%22+LINEEND> "+LINEEND)



Which is appended at the beginning of str variable. The problem is in the
four characters: \223\214\213\236, which are badly converted into Unicode.
Accordingly to the comment (and to PDF documentation) the four bytes are not
fixed, but can be any number, better if >128 because of automatic detection
as binary, not text. If I convert them into full-code Unicode characters
\x00DF\x00D6\x00D5\x00EC, all is ok.



"%\x00DF\x00D6\x00D5\x00EC ReportLab Generated PDF document
http://www.reportlab.com"+LINEEND)"



Do you know why?



Uuhh, reportLab is very long to execute (Vista32, CoreDuo 2.5GHz,
4GbRAM):about 20 seconds.



Marco [dot] Parenzan [at] libero [dot] it







-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20090820/4a7f9fd7/attachment.html>


More information about the reportlab-users mailing list