Skip to content

Commit

Permalink
Add simple pdf export
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Nov 12, 2024
1 parent d7c8c5e commit b4fbeea
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ <h3 class="h5">{% translate 'Refine your query' %}</h3>
<li>
<a href="{% url "evidencecollection:evidence-export" %}?{{ getvars }}&format=xlsx">{% trans "Export search as XLSX" %}</a>
</li>
<li>
<a href="{% url "evidencecollection:evidence-export" %}?{{ getvars }}&format=pdf">{% trans "Export search as PDF" %}</a>
</li>
</ul>
</div>
{% else %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{% load i18n %}
<html>
<head>
<title>Export</title>
<style>
html {
font-family: "Inter", sans-serif;
}
.page {
break-after: page;

}
.description {
margin: 2em;
}

@page {
counter-increment: page;
margin: 2cm;
size: a4;


@bottom-center {
content: {% blocktrans %}"Page " counter(page) " of " counter(pages);{% endblocktrans%}
}
}

th {
font-weight: bold;
}
</style>
</head>
<body>
{% for row in rows %}
<div class="page">
<h1>{{ row.title }}</h1>
<table>
<tr>
<th>{% trans "Date" %}</th>
<td>{{ row.date }}</td>
</tr>
<tr>
<th>{% trans "Source" %}</th>
<td>
<a href="{{ row.source__url }}" target="_blank">{{ row.source__url }}</a>
</td>
</tr>
<tr>
<th>{% trans "Evidence Type" %}</th>
<td>{{ row.type__name }}</td>
</tr>
<tr>
<th>{% trans "Evidence Area" %}</th>
<td>{{ row.area__name }}</td>
</tr>
<tr>
<th>{% trans "Person" %}</th>
<td>{{ row.person__name }}</td>
</tr>
<tr>
<th>{% trans "Evidence Quality" %}</th>
<td>{{ row.quality__name }}</td>
</tr>
</table>
<div class="description">{{ row.description }}</div>
</div>
{% endfor %}
</body>
</html>
16 changes: 15 additions & 1 deletion froide_evidencecollection/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import csv
import io

from django.conf import settings
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils.translation import gettext as _
from django.views.generic import DetailView

import openpyxl

from froide.foirequest.pdf_generator import get_wp
from froide.helper.breadcrumbs import BreadcrumbView
from froide.helper.search.views import BaseSearchView
from froide_evidencecollection.documents import EvidenceDocument
Expand Down Expand Up @@ -67,7 +70,7 @@ class EvidenceExportView(EvidenceListView):
"title",
"description",
]
FORMATS = ["csv", "xlsx"]
FORMATS = ["csv", "xlsx", "pdf"]

def get_rows(self):
self.object_list = self.get_queryset()
Expand Down Expand Up @@ -112,3 +115,14 @@ def generate_xlsx(self, rows):
f.getvalue(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)

def generate_pdf(self, rows):
html = render_to_string(
"froide_evidencecollection/pdf_export.html",
context={"rows": rows, "SITE_NAME": settings.SITE_NAME},
)
wp = get_wp()
if not wp:
raise Exception("WeasyPrint needs to be installed")
doc = wp.HTML(string=html)
return doc.write_pdf(), "application/pdf"

0 comments on commit b4fbeea

Please sign in to comment.