1

在我用 django 编写的网络应用程序中,我遇到了将 html 模板转换为 pdf 文档的需求。我决定使用比萨,这是有罪的代码:

我的视图.py:

#view.py
...
import ho.pisa as pisa
import cStringIO as StringIO #pdf
...
@login_required 
def dettaglio_bolla(request, id=None, tipo=None):
        bolla = Bolla.objects.get(id = id)
    cliente = Cliente.objects.get(id = bolla.destinatario.id)
    allegati = Allegato.objects.filter(bolla = bolla)
    paginator = Paginator(allegati, 2)
    try:
        pagina = int(request.GET.get('pagina','1'))
    except ValueError:
        pagina = 1
    try:
        pagina_allegati = paginator.page(pagina)
    except (EmptyPage, InvalidPage):
        pagina_allegati = paginator.page(paginator.num_pages)
    response =  render_to_response("dettaglio_bolla.html",{'bolla':bolla,'cliente':cliente,'allegati': pagina_allegati})
    if tipo == ".pdf":
        result = StringIO.StringIO()
        pisa.CreatePDF(unicode(response.content,encoding="utf-8"), result)
        return HttpResponse(result.getvalue(), mimetype='application/pdf')
    return response

PDF 已生成,但我得到的只是一个空白页。我错过了什么?我查看了官方教程,但我无法弄清楚。

4

0 回答 0