[Answer]-Change filename in Django Pisa

1👍

You’re very close. You just need to set the Content-Disposition of your response:

def write_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html = template.render(context)
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(
        html.encode("UTF-8")), result, encoding='UTF-8')

    if not pdf.err:
        response = http.HttpResponse(result.getvalue(),
                                 mimetype='application/pdf')

        response['Content-Disposition'] = 'attachment; filename=whatever.pdf'

        return response
    return http.HttpResponse('Gremlins ate your pdf! %s' % cgi.escape(html))

Leave a comment