from django.shortcuts import render_to_response from django.http import HttpResponse from django.template import Template, Context from rlextra.rml2pdf import rml2pdf import cStringIO def getPDF(request): """Returns PDF as a binary stream.""" if 'q' in request.GET: name = request.GET['q'] t = Template(open('hello.rml').read()) c = Context({"name": name}) rml = t.render(c) buf = cStringIO.StringIO() #create the pdf rml2pdf.go(rml, outputFileName=buf) buf.reset() pdfData = buf.read() #send the response response = HttpResponse(mimetype='application/pdf') response.write(pdfData) response['Content-Disposition'] = 'attachment; filename=output.pdf' return response