WMDE
/
eva
forked from tohe/eva
7
1
Fork 0

Pdf generation now works on clicking button. Looks ugly though

This commit is contained in:
Marike Voßbeck 2026-03-17 16:41:26 +01:00
parent 3354c6dbfc
commit 90b5e0b00c
3 changed files with 29 additions and 23 deletions

View File

@ -55,9 +55,12 @@
{% translate "PDF Abgabe Arbeitsmittel" %}
{% if pdf_data %}
{% include 'austritt/pdf_template.html' %}
<a href="{% url 'pdf-preview' %}" target="_blank">
<button type="button">Als PDF speichern</button>
</a>
{% endif %}
{% endif %}
{% else %}
{% if wizard.steps.step1 == 2 %}
Veränderungsrelevante Angaben

View File

@ -1,6 +1,6 @@
from django.urls import path
from .views import EvaFormView, success, long_process, change_process
from .views import EvaFormView, PDFPreviewView, success, long_process, change_process
urlpatterns = [
path('', EvaFormView.as_view(condition_dict = {'1': long_process,
@ -9,5 +9,7 @@ urlpatterns = [
'4': change_process,}),
name='evaform'),
path('form/pdf-preview/', PDFPreviewView.as_view(), name='pdf-preview'),
path('success', success, name='success')
]

View File

@ -1,11 +1,13 @@
from smtplib import SMTPException
import collections
from xhtml2pdf import pisa
from django.views.generic.edit import CreateView
from django.views import View
from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.core.mail import send_mail, BadHeaderError
from django.template.loader import get_template
from django.template.loader import get_template, render_to_string
from formtools.wizard.views import CookieWizardView
from django.shortcuts import render
from django.conf import settings
@ -116,30 +118,15 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView):
'datatable': 1,})
if (self.steps.step1 == 5):
context.update({'pdf_data': beautify_data_pdf(self.get_all_cleaned_data()),
})
pdf_data = beautify_data_pdf(self.get_all_cleaned_data())
# storing for PDF class
self.request.session['pdf_data'] = pdf_data
self.request.session.modified = True #forcing session saving
context.update({'pdf_data': pdf_data,})
return context
def get_pdf_data(self, form, **kwargs):
#print('GETCONTEXT')
context = super().get_pdf_data(form=form, **kwargs)
testmode = settings.DEBUG or settings.MAILTEST
context.update({'choice': self.choice,
'choice_string': TYPE_CHOICES[self.choice],
'TESTMODE': testmode})
# deliver context for forms if we are in the last step
if (self.steps.step1 == 6 or (self.choice != 'IN' and self.steps.step1 == 6)):
context.update({'data': self.beautify_data_pdf(self.get_all_cleaned_data()),
'datatable': 2,})
return context
def get_form_instance(self,step):
''' this method assures, that we use the same model instance for all steps'''
@ -261,4 +248,18 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView):
return newdata
# Class for rendering PDf
class PDFPreviewView(LoginRequiredMixin, View):
def get(self, request):
pdf_data=request.session.get('pdf_data')
#getting names for pdf file naming
firstname = pdf_data.get('firstname')
lastname = pdf_data.get('lastname')
context ={'pdf_data': pdf_data}
html_string =render_to_string('austritt/pdf_template.html', context)
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition']=f'inline; filename="Rueckgabe_Arbeitsmittel_{firstname}_{lastname}.pdf"'
pisa.CreatePDF(html_string, dest=response)
return response