Skip to content

Commit

Permalink
reverting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederporto committed Dec 4, 2024
1 parent 6e0e4ab commit 40d0a00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
35 changes: 18 additions & 17 deletions metrics/templates/metrics/wmf_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<head>
<meta charset="UTF-8">
<title>{% if pdf_title %}{{ pdf_title }}{% else %}PDF{% endif %}</title>
<style>
<style type="text/css">
@page {
size: A4 portrait;
margin: 3cm 2cm 2cm 3cm;
}

body {
font-weight: 200;
font-size: 1em;
font-size: 14px;
font-family: "Montserrat", sans-serif;
line-height: 1.5;
}
Expand All @@ -26,7 +26,9 @@

a {
color: #007cae;
word-wrap: break-word;
word-break: break-word;
hyphens: auto;
overflow-wrap: break-word;
}
</style>
Expand All @@ -53,13 +55,13 @@ <h1 style="text-align: center;">{{ project }}</h1>
<tbody>
{% for metric in metrics %}
<tr>
<td style="padding:0.25em; border:1px solid black;" {% if metric.refs_short %}rowspan="2"{% endif %}>{{ metric.metric }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black;width: 11%">{{ metric.q1|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black;width: 11%">{{ metric.q2|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black;width: 11%">{{ metric.q3|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black;width: 11%">{{ metric.q4|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black;width: 11%">{{ metric.total|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black;width: 11%">{{ metric.goal|bool_yesno }}</td>
<td style="padding:0.25em; border:1px solid black; width:34%" {% if metric.refs_short %}rowspan="2"{% endif %}>{{ metric.metric }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black; width: 11%">{{ metric.q1|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black; width: 11%">{{ metric.q2|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black; width: 11%">{{ metric.q3|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black; width: 11%">{{ metric.q4|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black; width: 11%">{{ metric.total|bool_yesno }}</td>
<td style="text-align:center; padding:0.25em; border:1px solid black; width: 11%">{{ metric.goal|bool_yesno }}</td>
</tr>
{% if metric.refs_short %}
<tr>
Expand All @@ -69,14 +71,13 @@ <h1 style="text-align: center;">{{ project }}</h1>
{% endfor %}
</tbody>
</table>
<div style="page-break-before: always;">
<h2>References</h2>
<ul style="max-width: 100%;">
{% for ref in references %}
{{ ref|safe }}
{% endfor %}
</ul>
</div>
<pdf:nextpage/>
<h2>References</h2>
<ul style="max-width: 100%;">
{% for ref in references %}
{{ ref|safe }}
{% endfor %}
</ul>
</div>
</div>
</body>
Expand Down
22 changes: 5 additions & 17 deletions metrics/utils.py
Original file line number Diff line number Diff line change
@@ -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')
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')

0 comments on commit 40d0a00

Please sign in to comment.