使用Pisa(Python)从HTML生成pdf的问题

时间:2011-01-17 16:40:14

标签: python django pdf-generation pisa

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

myview.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已生成,但我得到的只是一个空白页面。我错过了什么? 我检查了官方教程,但我无法弄明白。

0 个答案:

没有答案