Hi all,
I'm trying to add to my app engine project functionality for exporting
as a PDF file. I'm trying to use the ReportLab library
http://www.reportlab.org/downloads.html and I'm also following the
tutorial in
http://www.devshed.com/c/a/Python/Python-for-PDF-Generation/
mixed with the example in
http://www.djangoproject.com/documentation/outputting_pdf/
But when I save the file I get the following error inside the PDF
document:
Traceback (most recent call last):
File "C:\Archivos de Programa\Google\google_appengine\google
\appengine\ext\webapp\__init__.py", line 499, in __call__
handler.get(*groups)
File "C:\
ecv.eforcers.com\ecv\src\handler\fin.py", line 40, in get
pdf.save()
File "C:\
ecv.eforcers.com\ecv\reportlab\pdfgen\canvas.py", line 877,
in save
self._doc.SaveToFile(self._filename, self)
File "C:\
ecv.eforcers.com\ecv\reportlab\pdfbase\pdfdoc.py", line
218, in SaveToFile
f = open(filename, "wb")
File "C:\Archivos de Programa\Google\google_appengine\google
\appengine\tools\dev_appserver.py", line 736, in __init__
raise IOError('invalid mode: %s' % mode)
IOError: invalid mode: wb
It looks like I'm violating the condition to write on the filesystem,
even if I try the http response as a file.
Here is my code...
import cgi
import wsgiref.handlers
import logging
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
import os
from google.appengine.ext.webapp import template
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import cm, mm, inch, pica
from django.http import HttpResponse
class FinHandler(webapp.RequestHandler):
def get(self, id_persona=None):
logging.debug("Entrando a generar el PDF para la persona %s",
id_persona)
# Create the HttpResponse object with the appropriate PDF headers.
self.response.headers.add_header("Content-Type", "application/
pdf")
self.response.headers.add_header("Content-Disposition",
"attachment; filename=cv.pdf")
# Create the PDF object, using the response object as its "file."
pdf = canvas.Canvas(self.response)
# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of
functionality.
pdf.setFont("Courier", 60)
pdf.setFillColorRGB(1, 0, 0)
pdf.drawCentredString(letter[0] / 2, inch * 6, "CLASSIFIED")
pdf.setFont("Courier", 30)
pdf.drawCentredString(letter[0] / 2, inch * 5, "For Your Eyes
Only")
# Close the PDF object cleanly, and we're done.
pdf.showPage()
pdf.save()
Please, if any of you have implemented this functionality with
ReportLab or a different library I would appreciate any help. Thanks
David C