0

我现在有这个 PDFview 类,我想在任何表单模板中使用它,这样我就可以在发送数据之前查看该表单数据的预览。

PDFView 类(视图):

def get(self, request, **kwargs):
    template = get_template("pim/pdfview.html")
    ctx = {}
    ctx["test"] = Employee.objects.all()

    context = Context(ctx)
    html  = template.render(context)
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        response = http.HttpResponse(result.getvalue(), mimetype='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="leavereport.pdf"'
        return response
    return http.HttpResponse('We had some errors<pre>%s</pre>' % cgi.escape(html)) 
4

0 回答 0