From 90de878876c1ca62aaf3d82d21f2845c766c740f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Thu, 2 Sep 2021 10:34:03 -0300 Subject: [PATCH] =?UTF-8?q?Pequena=20corre=C3=A7=C3=A3o=20de=20bug=20na=20?= =?UTF-8?q?gera=C3=A7=C3=A3o=20de=20relat=C3=B3rios=20em=20PDF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/shortcuts.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sigi/shortcuts.py b/sigi/shortcuts.py index f5f0fff..d27986c 100644 --- a/sigi/shortcuts.py +++ b/sigi/shortcuts.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from cgi import escape +from datetime import datetime import os from django.conf import settings @@ -28,12 +29,15 @@ def render_to_pdf(template_src, context_dict): filename = template_src.replace('.html', '').replace('_pdf', '.pdf') 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, link_callback=fetch_resources) - if not pdf.err: - response = HttpResponse(result.getvalue(), content_type='application/pdf') - response['Content-Disposition'] = 'attachment; filename=' + filename - return response - return HttpResponse(_(u'We had some errors
%s
') % escape(html)) + + response = HttpResponse(content_type='application/pdf') + response['Content-Disposition'] = 'attachment; filename=' + filename + + pdf = pisa.CreatePDF(html, dest=response, + link_callback=fetch_resources) + + if pdf.err: + return HttpResponse(_(u'We had some errors
%s
') % escape(html)) + return response