From 90b5e0b00cf6bff95262f3f112a9f53a585876ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marike=20Vo=C3=9Fbeck?= Date: Tue, 17 Mar 2026 16:41:26 +0100 Subject: [PATCH] Pdf generation now works on clicking button. Looks ugly though --- .../templates/austritt/employee_form.html | 5 ++- austritt/urls.py | 4 +- austritt/views.py | 43 ++++++++++--------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/austritt/templates/austritt/employee_form.html b/austritt/templates/austritt/employee_form.html index 1873750..107b02e 100644 --- a/austritt/templates/austritt/employee_form.html +++ b/austritt/templates/austritt/employee_form.html @@ -55,9 +55,12 @@ {% translate "PDF Abgabe Arbeitsmittel" %} {% if pdf_data %} {% include 'austritt/pdf_template.html' %} + + + {% endif %} {% endif %} - + {% else %} {% if wizard.steps.step1 == 2 %} Veränderungsrelevante Angaben diff --git a/austritt/urls.py b/austritt/urls.py index 7d0a9d1..3175920 100644 --- a/austritt/urls.py +++ b/austritt/urls.py @@ -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') + ] diff --git a/austritt/views.py b/austritt/views.py index 86270af..03defed 100644 --- a/austritt/views.py +++ b/austritt/views.py @@ -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