开发者

Problems generating pdf from HTML with Pisa (Python)

开发者 https://www.devze.com 2023-02-05 16:16 出处:网络
In my web app, writen in django, I met the need to convert an html template in a pdf document. I decided to use Pisa and this is the incriminated code:

In my web app, writen in django, I met the need to convert an html template in a pdf document. I decided to use Pisa and this is the incriminated code:

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开发者_StackOverflow/pdf')
    return response

The PDF is generated but all I get is a blank page. Did I miss something? I checked the official tutorials but I can't figure it out.

0

精彩评论

暂无评论...
验证码 换一张
取 消