Skip to content

Commit

Permalink
pdf report
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederporto committed Dec 4, 2024
1 parent 17f5e30 commit 864047f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
33 changes: 17 additions & 16 deletions metrics/templates/metrics/wmf_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
{% load static %}
{% load i18n %}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% if pdf_title %}{{ pdf_title }}{% else %}PDF{% endif %}</title>
<style type="text/css">
<style>
@page {
size: A4 portrait;
margin: 3cm 2cm 2cm 3cm;
}

body {
font-weight: 200;
font-size: 14px;
font-size: 1em;
font-family: "Montserrat", sans-serif;
line-height: 1.5;
}

h1 {
Expand All @@ -24,9 +26,7 @@

a {
color: #007cae;
word-wrap: break-word;
word-break: break-word;
hyphens: auto;
overflow-wrap: break-word;
}
</style>
Expand All @@ -36,10 +36,10 @@
<div style="text-align:center; margin-bottom: 3em;">
<img src="http://localhost:8000/static/images/logo_cor.jpg" alt="Logo" width="200px" height="auto">
</div>
<div class="w3-container" style="background-color: var(--success-color); color:black;">
<div class="w3-container" style="color:black;">
<h1 style="text-align: center;">{{ project }}</h1>
<table repeat="1" class="table table-striped table-fixed table-sara metrics_table" style="border-spacing: 0; ">
<thead>
<table class="table table-striped table-fixed table-sara metrics_table" style="border-spacing: 0; ">
<thead style="display: table-header-group">
<tr>
<th>Metrics</th>
<th>Q1</th>
Expand Down Expand Up @@ -69,13 +69,14 @@ <h1 style="text-align: center;">{{ project }}</h1>
{% endfor %}
</tbody>
</table>
<pdf:nextpage/>
<h2>References</h2>
<ul style="max-width: 100%;">
{% for ref in references %}
{{ ref|safe }}
{% endfor %}
</ul>
<div style="page-break-before: always;">
<h2>References</h2>
<ul style="max-width: 100%;">
{% for ref in references %}
{{ ref|safe }}
{% endfor %}
</ul>
</div>
</div>
</div>
</body>
Expand Down
21 changes: 16 additions & 5 deletions metrics/utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
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)
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')
13 changes: 9 additions & 4 deletions metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -130,7 +131,7 @@ def replace(match):
else:
link = friendly = content

link = dewikify_url(link)
link = ur.quote(dewikify_url(link), safe=":/")
return f'<a target="_blank" href="{link}">{friendly}</a>'
elif substring.startswith('[') and substring.endswith(']'):
content = substring[1:-1]
Expand Down Expand Up @@ -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 = []
Expand All @@ -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


Expand Down

0 comments on commit 864047f

Please sign in to comment.