Generating pdf from html using Pisa

528 views
Skip to first unread message

Satinderpal Singh

unread,
Feb 20, 2013, 2:05:19 PM2/20/13
to django...@googlegroups.com
I am trying to produce pdf from html using the following code, but
unable to do so, it gives an error that, global name 'results' is not
defined. Please tell me how to use this view.

def render_to_pdf(template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")),
result)
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype='application/pdf')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

def myview(request):
#Retrieve data or whatever you need
return render_to_pdf(
'report/pdf.html',
{
'pagesize':'A4',
'mylist': results,
}
)

in the report/pdf.html there is:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>My Title</title>
<style type="text/css">
@page {
size: {{ pagesize }};
margin: 1cm;
@frame footer {
-pdf-frame-content: footerContent;
bottom: 0cm;
margin-left: 9cm;
margin-right: 9cm;
height: 1cm;
}
}
</style>
</head>
{% block report %}
<body>
<div>
{% for item in mylist %}
{{item}}
{% endfor %}
</div>
<div id="footerContent">
{%block page_foot%}
Page <pdf:pagenumber>
<pdf:barcode>
{%endblock%}
</div>
</body>
</html>


--
Satinderpal Singh
http://satindergoraya.blogspot.in/
http://satindergoraya91.blogspot.in/

Bill Freeman

unread,
Feb 20, 2013, 2:19:39 PM2/20/13
to django...@googlegroups.com
The pisa pypi entry suggests that you switch to xml2pdf: https://pypi.python.org/pypi/xhtml2pdf


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



Adam Stein

unread,
Feb 20, 2013, 2:22:58 PM2/20/13
to django...@googlegroups.com
In myview(), you are setting "mylist" to results but I don't see results
defined in myview() anywhere.
--
Adam (ad...@csh.rit.edu)


Reply all
Reply to author
Forward
0 new messages