[reportlab-users] Fwd: Reportlab and IronPython

Tony Meyer tony at spamexperts.com
Thu Sep 16 00:28:18 EDT 2010


This doesn't have anything to do with IronClad or the Python script
encoding, or the string being written - the error occurs with simply
this code:


> from reportlab.pdfgen import canvas

> c = canvas.Canvas('test.pdf')

> c.save()


This is a str/unicode issue. FWIW, if ReportLab is patched up to work
(at least this much) under Python 3, there's a related error:


>>> c.save()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "reportlab/pdfgen/canvas.py", line 1091, in save
self._doc.SaveToFile(self._filename, self)
File "reportlab/pdfbase/pdfdoc.py", line 236, in SaveToFile
f.write(self.GetPDFData(canvas))
TypeError: 'str' does not support the buffer interface

Tim's message (http://two.pairlist.net/pipermail/reportlab-users/2009-August/008699.html)
from the original thread is a reasonably clear explanation of the
problem. The patch in that thread should still work, I suspect.

The problem is that in the PDFFile class in pdfdoc.py, the format()
method converts everything to str. In IronPython, I believe this is
the same as saying unicode(x, "ascii"). However, the PDF data always
includes (IIUC as a marker that the file is binary, within a PDF
comment) a \x93 character, so that fails.

Changing the format() method from:

def format(self, document):
strings = map(str, self.strings) # final conversion, in case
of lazy objects
return string.join(strings, "")

To:

def format(self, document):
# final conversion, in case of lazy objects
strings = []
for s in self.strings:
if not isinstance(s, types.StringTypes):
strings.append(str(s))
else:
strings.append(s)
return "".join(strings)

Also removes the problem.

Cheers,
Tony


More information about the reportlab-users mailing list