Entry 3362

Creating a PDF in memory from within a web application

   

Submitted by anonymous on March 15, 2010 at 4:07 p.m.
Language: Python. Code size: 706 bytes.

from django.http import HttpResponse
from rlextra.rml2pdf import rml2pdf
import cStringIO

def getPDF(request):
    """Returns PDF as a binary stream."""

    # Use your favourite templating language here to create the RML string.
    # The generated document might depend on the web request parameters,
    # database lookups and so on - we'll leave that up to you.
    rml = getRML(request)  

    buf = cStringIO.StringIO()

    rml2pdf.go(rml, outputFileName=buf)
    buf.reset()
    pdfData = buf.read()

    response = HttpResponse(mimetype='application/pdf')
    response.write(pdfData)
    response['Content-Disposition'] = 'attachment; filename=output.pdf'
    return response

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).