I was playing around with reportlab (a PDF-generating package for Python), and
figured out how to get PDFs to be generated and sent directly to the user's
browser using WebKit, without needing temporary files. In case this is of any
interest to anyone, here's the code I used. This is a bare-bones example, but
at least it demonstrates that it's possible to do this.
Reportlab can be found at http://www.reportlab.com/
###########################################
import reportlab.pdfgen.canvas
from reportlab.lib.units import inch
from cStringIO import StringIO
from Page import Page
class ExamplePDF(Page):
def writeHTML(self):
response = self.response()
request = self.request()
# Get the number from the request, if any
string = request.value('string', 'Hello, World!')
# Generate the PDF
buffer = StringIO()
c = reportlab.pdfgen.canvas.Canvas(buffer)
c.setFont('Helvetica', 12)
c.drawString(inch, inch*10, string)
c.showPage()
c.save()
pdf = buffer.getvalue()
# Set the response headers
response.setHeader('Content-type', 'application/pdf')
response.setHeader('Content-length', str(len(pdf)))
# Send it back to the user
self.write( pdf )
###########################################
--
- Geoff Talvola
Parlance Corporation
gtalvola@...
|