Skip to content

Commit 74e8e2f

Browse files
Use xhtml2pdf inplace of reportlab for submission pdf export (#4577)
Fixes #4255
1 parent 6d34b02 commit 74e8e2f

5 files changed

Lines changed: 85 additions & 469 deletions

File tree

hypha/apply/funds/models/mixins.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,20 @@ def get_answer_from_label(self, label):
385385
if answer and not answer == "N":
386386
return answer
387387
return None
388+
389+
def get_text_questions_answers_as_dict(self):
390+
data_dict = {}
391+
for field_id in self.question_text_field_ids:
392+
if field_id not in self.named_blocks:
393+
question_field = self.serialize(field_id)
394+
if isinstance(question_field["answer"], str):
395+
answer = question_field["answer"]
396+
else:
397+
answer = ",".join(question_field["answer"])
398+
if answer and not answer == "None":
399+
data_dict[question_field["question"]] = answer
400+
elif question_field["type"] == "checkbox":
401+
data_dict[question_field["question"]] = False
402+
else:
403+
data_dict[question_field["question"]] = "-"
404+
return data_dict
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "base-pdf.html" %}
2+
{% load i18n %}
3+
4+
{% block content %}
5+
<h2 style="margin-bottom: 0;">{{ title }} <a href="{{ link }}">(#{{ id }})</a></h2>
6+
<p style="margin-top:0; margin-bottom:8px; border-bottom: 1px solid lightgray;">{{ stage }} : {{ fund }} : {{ round }} : {{ lead }}</p>
7+
8+
<div style="font-size:14px;">
9+
{% for question, answer in data.items %}
10+
<p><b>{{ question }}</b></p>
11+
<p> {{ answer }}</p>
12+
{% endfor %}
13+
</div>
14+
15+
{% endblock %}

hypha/apply/funds/views/submission_detail.py

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from django.conf import settings
22
from django.core.exceptions import PermissionDenied
33
from django.http import (
4-
FileResponse,
54
Http404,
65
HttpRequest,
76
HttpResponse,
87
HttpResponseRedirect,
98
)
109
from django.shortcuts import redirect
10+
from django.templatetags.static import static
1111
from django.urls import reverse_lazy
12+
from django.utils import timezone
1213
from django.utils.decorators import method_decorator
14+
from django.utils.text import slugify
1315
from django.views import View
1416
from django.views.generic import (
1517
DetailView,
@@ -22,10 +24,11 @@
2224
staff_required,
2325
)
2426
from hypha.apply.utils.models import PDFPageSettings
25-
from hypha.apply.utils.pdfs import draw_submission_content, make_pdf
27+
from hypha.apply.utils.pdfs import render_as_pdf
2628
from hypha.apply.utils.views import (
2729
ViewDispatcher,
2830
)
31+
from hypha.core.models import SystemSettings
2932

3033
from ..models import (
3134
ApplicationSubmission,
@@ -287,28 +290,40 @@ def should_redirect(cls, request, submission):
287290
class SubmissionDetailPDFView(SingleObjectMixin, View):
288291
model = ApplicationSubmission
289292

293+
def get_slugified_file_name(self, export_type):
294+
return f"{timezone.localdate().strftime('%Y%m%d')}-{slugify(self.object.title)}.{export_type}"
295+
290296
def get(self, request, *args, **kwargs):
291297
self.object = self.get_object()
292298
pdf_page_settings = PDFPageSettings.load(request_or_site=request)
293-
content = draw_submission_content(self.object.output_text_answers())
294-
pdf = make_pdf(
295-
title=self.object.title,
296-
sections=[
297-
{
298-
"content": content,
299-
"title": "Submission",
300-
"meta": [
301-
self.object.stage,
302-
self.object.page,
303-
self.object.round,
304-
f"Lead: {self.object.lead}",
305-
],
306-
},
307-
],
308-
pagesize=pdf_page_settings.download_page_size,
299+
context = {}
300+
context["pagesize"] = pdf_page_settings.download_page_size
301+
context["show_footer"] = True
302+
site_settings = SystemSettings.objects.first()
303+
if site_settings:
304+
if site_settings.site_logo_default:
305+
context["logo"] = request.build_absolute_uri(
306+
site_settings.site_logo_default.file.url
307+
)
308+
else:
309+
context["logo"] = request.build_absolute_uri(static("images/logo.png"))
310+
311+
context["link"] = self.request.build_absolute_uri(
312+
self.object.get_absolute_url()
309313
)
310-
return FileResponse(
311-
pdf,
312-
as_attachment=True,
313-
filename=self.object.title + ".pdf",
314+
context["id"] = self.object.application_id
315+
context["data"] = self.object.get_text_questions_answers_as_dict()
316+
context["title"] = self.object.title
317+
context["stage"] = self.object.stage
318+
context["fund"] = self.object.page
319+
context["round"] = self.object.round
320+
context["lead"] = self.object.lead
321+
context["show_header"] = True
322+
context["header_title"] = "Submission details"
323+
template_path = "funds/submission-pdf.html"
324+
return render_as_pdf(
325+
request=request,
326+
template_name=template_path,
327+
context=context,
328+
filename=self.get_slugified_file_name("pdf"),
314329
)

0 commit comments

Comments
 (0)