Create PDF files using rml and django templates

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from django.http import HttpResponse
from django.template import loader, Context
from trml2pdf import trml2pdf

def pdf_render_to_response(template, context, filename=None, prompt=False):
    response = HttpResponse(mimetype='application/pdf')
    if not filename:
        filename = template+'.pdf'
    cd = []
    if prompt:
        cd.append('attachment')
    cd.append('filename=%s' % filename)
    response['Content-Disposition'] = '; '.join(cd)
    tpl = loader.get_template(template)
    tc = {'filename': filename}
    tc.update(context)
    ctx = Context(tc)
    pdf = trml2pdf.parseString(tpl.render(ctx))
    response.write(pdf)
    return response

More like this

  1. RML2PDF with Django by meitham 3 years, 1 month ago
  2. Row-Level, URL-based permissions for FlatPages by bradmontgomery 3 years, 11 months ago
  3. Database file storage by powerfox 4 years, 3 months ago
  4. DRY template rendering decorator update by jakecr 3 years, 2 months ago
  5. Confirm alert if the user navigates away without saving changes by mrazzari 3 years, 10 months ago

Comments

prio (on July 17, 2007):

Hi,

You seem to be missing an import line:

from django.template import loader, Context

#

mporrato (on July 20, 2007):

Oops! I stripped too many lines from my original code. I just updated the snippet.

Thank you prio!

#

(Forgotten your password?)