I'm developing a Google App Engine app using Python (webapp2) & Jinja2 and I'm trying to create a PDF file using the reportlab library.
Example:
from reportlab.pdfgen import canvas
class pdf(webapp2.RequestHandler):
def get(self):
x = 50
y = 750
c = canvas.Canvas("file.pdf")
c.drawString(x*5,y,"Output")
c.line(x,y-10,x*11,y-10)
c.save()
When I run the server I'm getting the following error:
raise IOError(errno.EROFS, 'Read-only file system', filename)
IOError: [Errno 30] Read-only file system: u'file.pdf'
I got it to work by using StringIO:
from reportlab.pdfgen import canvas
# Import String IO which is a
# module that reads and writes a string buffer
# cStringIO is a faster version of StringIO
from cStringIO import StringIO
class pdf(webapp2.RequestHandler):
def get(self):
pdfFile = StringIO()
x = 50
y = 750
c = canvas.Canvas(pdfFile)
c.drawString(x*5,y,"Output")
c.line(x,y-10,x*11,y-10)
c.save()
self.response.headers['content-type'] = 'application/pdf'
self.response.headers['Content-Disposition'] = 'attachment; filename=file.pdf'
self.response.out.write(pdfFile.getvalue())