From 40d0a006ab66665d710d9704878f4d27d8d86642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89der=20Porto?= Date: Wed, 4 Dec 2024 04:42:31 -0300 Subject: [PATCH] reverting --- metrics/templates/metrics/wmf_report.html | 35 ++++++++++++----------- metrics/utils.py | 22 ++++---------- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/metrics/templates/metrics/wmf_report.html b/metrics/templates/metrics/wmf_report.html index ae725a9..d668d0f 100644 --- a/metrics/templates/metrics/wmf_report.html +++ b/metrics/templates/metrics/wmf_report.html @@ -7,7 +7,7 @@ {% if pdf_title %}{{ pdf_title }}{% else %}PDF{% endif %} - @@ -53,13 +55,13 @@

{{ project }}

{% for metric in metrics %} - {{ metric.metric }} - {{ metric.q1|bool_yesno }} - {{ metric.q2|bool_yesno }} - {{ metric.q3|bool_yesno }} - {{ metric.q4|bool_yesno }} - {{ metric.total|bool_yesno }} - {{ metric.goal|bool_yesno }} + {{ metric.metric }} + {{ metric.q1|bool_yesno }} + {{ metric.q2|bool_yesno }} + {{ metric.q3|bool_yesno }} + {{ metric.q4|bool_yesno }} + {{ metric.total|bool_yesno }} + {{ metric.goal|bool_yesno }} {% if metric.refs_short %} @@ -69,14 +71,13 @@

{{ project }}

{% endfor %} -
-

References

- -
+ +

References

+ diff --git a/metrics/utils.py b/metrics/utils.py index 5bb9894..163c2ff 100644 --- a/metrics/utils.py +++ b/metrics/utils.py @@ -1,28 +1,16 @@ -import os from io import BytesIO from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa -import pdfkit -pdfkit.configuration(wkhtmltopdf='wkhtmltopdf/bin/wkhtmltopdf.exe') -import tempfile - def render_to_pdf(template_src, context_dict=None): if context_dict is None: context_dict = {} template = get_template(template_src) html = template.render(context_dict) - with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file: - pdfkit.from_string(html, temp_file.name, options={'page-size': 'A4', - 'margin-top': '3cm', - 'margin-bottom': '2cm', - 'margin-left': '3cm', - 'margin-right': '2cm', - 'minimum-font-size': 14}) - - with open(temp_file.name, "rb") as pdf_file: - result = pdf_file.read() - - return HttpResponse(result, content_type='application/pdf') \ No newline at end of file + result = BytesIO() + pdf = pisa.pisaDocument(BytesIO(html.encode("utf-8")), result) + if pdf.err: + return HttpResponse("Invalid PDF", status_code=400, content_type='text/plain') + return HttpResponse(result.getvalue(), content_type='application/pdf')