diff --git a/metrics/templates/metrics/wmf_report.html b/metrics/templates/metrics/wmf_report.html index 8b6500f..ae725a9 100644 --- a/metrics/templates/metrics/wmf_report.html +++ b/metrics/templates/metrics/wmf_report.html @@ -2,11 +2,12 @@ {% load static %} {% load i18n %} - - + + + {% if pdf_title %}{{ pdf_title }}{% else %}PDF{% endif %} - @@ -36,10 +36,10 @@
Logo
-
+

{{ project }}

- - +
+ @@ -69,13 +69,14 @@

{{ project }}

{% endfor %}
Metrics Q1
- -

References

-
    - {% for ref in references %} - {{ ref|safe }} - {% endfor %} -
+
+

References

+
    + {% for ref in references %} + {{ ref|safe }} + {% endfor %} +
+
diff --git a/metrics/utils.py b/metrics/utils.py index 29df619..5bb9894 100644 --- a/metrics/utils.py +++ b/metrics/utils.py @@ -1,8 +1,12 @@ +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): @@ -10,8 +14,15 @@ def render_to_pdf(template_src, context_dict=None): context_dict = {} template = get_template(template_src) html = template.render(context_dict) - 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') + 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 diff --git a/metrics/views.py b/metrics/views.py index 8cddba2..b6faecd 100644 --- a/metrics/views.py +++ b/metrics/views.py @@ -68,7 +68,8 @@ def prepare_pdf(request, *args, **kwargs): main_results = get_results_for_timespan(timespan_array, Q(project=main_project), Q(), - True) + True, + "en") metrics = [] refs = [] @@ -130,7 +131,7 @@ def replace(match): else: link = friendly = content - link = dewikify_url(link) + link = ur.quote(dewikify_url(link), safe=":/") return f'{friendly}' elif substring.startswith('[') and substring.endswith(']'): content = substring[1:-1] @@ -324,7 +325,7 @@ def get_results_divided_by_trimester(buffer, area=None, with_goal=False): buffer.write(footer) -def get_results_for_timespan(timespan_array, metric_query=Q(), report_query=Q(), with_goal=False): +def get_results_for_timespan(timespan_array, metric_query=Q(), report_query=Q(), with_goal=False, lang="pt"): results = [] for metric in Metric.objects.filter(metric_query).order_by("activity_id", "id"): done_row = [] @@ -348,7 +349,11 @@ def get_results_for_timespan(timespan_array, metric_query=Q(), report_query=Q(), done_row.append(goal_value) else: done_row.append("?") - results.append({"activity": metric.activity.text, "metric": metric.text, "done": done_row}) + + if lang == "pt": + results.append({"activity": metric.activity.text, "metric": metric.text, "done": done_row}) + else: + results.append({"activity": metric.activity.text, "metric": metric.text_en, "done": done_row}) return results