Easy PDF generation using PISA HTML2PDF

299 views
Skip to first unread message

dirk.h...@gmail.com

unread,
Mar 22, 2008, 4:50:11 AM3/22/08
to Django users
Hi,

I'm the author of a pure Python module that is able to generate PDF
form HTML and CSS. You find more informations about it and a sample
integration in Django here:

http://www.htmltopdf.org

I think this could be interesting to be mentioned in your
documentation:

http://www.djangoproject.com/documentation/outputting_pdf/

As I do not use Django often, the sample may be not very complete. A
user of my tool send me this portion of code that should help you
integration pisa into your work:

#! /usr/bin/python
# -*- encoding: utf-8 -*-
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
import cStringIO as StringIO
from sx.pisa3 import pisaDocument

def html_to_pdf(content):
'''
Convert the html content to PDF
'''
result = StringIO.StringIO()
html = StringIO.StringIO(content.encode("ISO-8859-1"))
pdf = pisaDocument(html, result)
if not pdf.err:
return result.getvalue()
return pdf

def render_to_pdf(template_src, context_dict):
'''
Render the template with a context. Then send the response in pdf
format.
'''
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
return HttpResponse(html_to_pdf(html), 'application/pdf',
200,'application/pdf')

Hope this informations are helpful for you.

Regards,
Dirk

dirk.h...@gmail.com

unread,
Mar 23, 2008, 2:37:32 PM3/23/08
to Django users
Hi,

I updated pisa which now includes a complete Django example.

http://pypi.python.org/pypi/pisa/3.0.17

Enjoy it.
Dirk

coulix

unread,
Mar 23, 2008, 5:25:57 PM3/23/08
to Django users
Thanks !
Easier than reportlab :)
Greg

On Mar 23, 7:37 pm, "dirk.holtw...@gmail.com"

shabda

unread,
Mar 24, 2008, 7:00:31 AM3/24/08
to Django users
I have used html2pdf in the past and it is awfully easy! In one of my
apps, I had a requirement that each page be also available as pdf.
so

def view_func(self):
..normal processing
if request.GET.has_key('pdf'):
#Get the pdf template
html = StringIO.StringIO(template.render(Context(payload)))
result = StringIO.StringIO()
pdf = pisa.CreatePDF(html, result)
return HttpResponse(result.getvalue(), mimetype='application/
pdf')
Reply all
Reply to author
Forward
0 new messages