This is the first time I’ve used ReportLab, I have tried to make simple pdf, but I get the following error, when I try and run the script.
class ReportLabTest (webapp.RequestHandler):
def get(self):
c = canvas.Canvas("hello.pdf")
c.translate(inch,inch)
c.setFont("Helvetica", 80)
c.setStrokeColorRGB(0.2,0.5,0.3)
c.setFillColorRGB(1,0,1)
c.rect(inch,inch,6*inch,9*inch, fill=1)
c.rotate(90)
c.setFillColorRGB(0,0,0.77)
c.drawString(3*inch, -3*inch, "Hello World")
c.showPage()
c.save()
self.write_response(c)
self.response.headers['Content-Type'] = 'application/pdf'
self.response.headers['Content-Disposition'] = 'filename=testpdf.pdf'
return
The error that I get is:
Traceback (most recent call last):
File "/home/ducos/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 710, in \__call__
handler.get(*groups)
File "/home/ducos/workspace/MedeticWS/www/tests.py", line 572, in get
c.save()
File "/home/ducos/workspace/MedeticWS/reportlab/pdfgen/canvas.py", line 1123, in save
self._doc.SaveToFile(self._filename, self)
File "/home/ducos/workspace/MedeticWS/reportlab/pdfbase/pdfdoc.py", line 234, in SaveToFile
f = open(filename, "wb")
File "/home/ducos/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 589, in __init__
raise IOError('invalid mode: %s' % mode)
IOError: invalid mode: wb
Thanks you for your help.
As per previous answer you can’t write to the file system. You can however provide a file like device as an argument instead of a filename. From the source of canvas
You may pass a file-like object to filename as an alternative to a string.So you can create a StringIO object pass it to Canvas, and then rather than calling save() which may close the device (I am not sure on this – see below). do showpage() if you haven’t already and the perform getvalue() on the StringIO object to for your response.write(). e.g.
Just checked, if a file like handle is provided then it doesn’t call
closeso asave()would be fine.