-
-
Save george-silva/3228697 to your computer and use it in GitHub Desktop.
Helper para gerar relatório em pdf a partir de um html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template.loader import get_template | |
from django.template import Context | |
import cStringIO as StringIO | |
import cgi | |
import ho.pisa as pisa | |
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("UTF-8")), result) | |
if not pdf.err: | |
response = HttpResponse(result.getvalue(), mimetype='application/pdf') | |
response['Content-Disposition'] = 'attachment; filename=%s' % 'relatorio.pdf' | |
return response | |
return HttpResponse('We had some errors<pre>%s</pre>' % cgi.escape(html)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment