1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
from datetime import datetime
from typing import List
from io import BytesIO
from xhtml2pdf import pisa
from calendar_manager import CalendarManager
from prediction_controller import PredictionController
from date_service import DateService
class PredictionReportService:
"""Service to export the current prediction and entries into a PDF report."""
@staticmethod
def export_pdf(calendar_manager: CalendarManager,
prediction_controller: PredictionController,
out_path: str,
dateformat: str = "dd.MM.yyyy") -> bool:
try:
# Gather data
launch_dt = prediction_controller.get_launch_date()
duration = prediction_controller.get_duration()
prediction_dt = prediction_controller.get_prediction()
entries = calendar_manager.list_entries()
# Format helpers
def fmt_date(dt):
return DateService.format_date_for_display(dt, dateformat) if dt else ""
launch_str = fmt_date(launch_dt)
duration_str = f"{duration.years} Jahre" if duration and hasattr(duration, 'years') else ""
prediction_str = fmt_date(prediction_dt)
# Sum time_periods (months and days separately)
total_months = 0
total_days = 0
for e in entries:
tp = getattr(e, 'time_period', None)
if not tp:
continue
try:
parts = tp.split()
if len(parts) >= 2:
value = int(parts[0])
unit = parts[1].lower()
if value > 0:
if 'monat' in unit:
total_months += value
elif 'tag' in unit:
total_days += value
except Exception:
# Ignore unparsable time_periods
pass
sum_parts = []
if total_months > 0:
sum_parts.append(f"{total_months} Monate")
if total_days > 0:
sum_parts.append(f"{total_days} Tage")
sum_str = ", ".join(sum_parts) if sum_parts else "0"
# Build HTML
created_stamp = datetime.now().strftime('%d.%m.%Y %H:%M:%S')
html = []
html.append("<!DOCTYPE html>")
html.append("<html><head><meta charset='utf-8'>")
html.append("""
<style>
@page {
size: A4;
margin: 1.5cm;
}
body {
font-family: Arial, sans-serif;
color: #222;
font-size: 10pt;
}
h1 {
font-size: 14pt;
margin-bottom: 8px;
}
.meta {
margin-bottom: 16px;
}
.meta div {
margin: 3px 0;
font-size: 10pt;
}
table {
border-collapse: collapse;
width: 100%;
margin-top: 10px;
}
th, td {
border: 1px solid #e1e4ee;
padding: 3px 4px;
vertical-align: top;
font-size: 9pt;
line-height: 1.4;
}
th {
background-color: #f5f7fb;
text-align: left;
font-weight: bold;
}
tfoot td {
font-weight: bold;
background-color: #fafafa;
}
.stamp {
margin-top: 20px;
font-size: 8pt;
color: #666;
}
</style>
""")
html.append("</head><body>")
html.append("<h1>Ausfallzeitenrechner — Bericht</h1>")
html.append("<div class='meta'>")
html.append(f"<div><strong>Promotionsdatum:</strong> {launch_str}</div>")
html.append(f"<div><strong>Bewerbungszeitraum:</strong> {duration_str}</div>")
html.append(f"<div><strong>Bewerbungsfrist:</strong> {prediction_str}</div>")
html.append("</div>")
# Table header
html.append("<table>")
html.append(
"<thead><tr>"
"<th>Beginn</th>"
"<th>Ende</th>"
"<th>Art</th>"
"<th>Kommentar</th>"
"<th>Beginn Anrechnung</th>"
"<th>Ende Anrechnung</th>"
"<th>Zeitraum</th>"
"</tr></thead><tbody>"
)
# Rows
for e in entries:
start_text = fmt_date(e.start_date)
end_text = fmt_date(e.end_date)
keyword_text = e.keyword
commentary_text = getattr(e, 'commentary', '') or ''
c_start_text = fmt_date(getattr(e, 'corrected_start_date', None))
c_end_text = fmt_date(getattr(e, 'corrected_end_date', None))
time_text = getattr(e, 'time_period', '') or ''
# Escape HTML special characters
def esc(s: str) -> str:
return (s.replace('&', '&')
.replace('<', '<')
.replace('>', '>')
.replace('"', '"'))
html.append(
f"<tr><td>{esc(start_text)}</td>"
f"<td>{esc(end_text)}</td>"
f"<td>{esc(keyword_text)}</td>"
f"<td>{esc(commentary_text)}</td>"
f"<td>{esc(c_start_text)}</td>"
f"<td>{esc(c_end_text)}</td>"
f"<td>{esc(time_text)}</td></tr>"
)
html.append("</tbody>")
html.append(f"<tfoot><tr><td colspan='7'>Summe Anrechnungszeitraum: {sum_str}</td></tr></tfoot>")
html.append("</table>")
html.append(f"<div class='stamp'>Erstellt am: {created_stamp}</div>")
html.append("</body></html>")
# Ensure extension
if not out_path.lower().endswith('.pdf'):
out_path = out_path + '.pdf'
# Generate PDF using xhtml2pdf
html_content = "".join(html)
with open(out_path, "wb") as pdf_file:
pisa_status = pisa.CreatePDF(
html_content,
dest=pdf_file,
encoding='utf-8'
)
return not pisa_status.err
except Exception as e:
print(f"Error exporting PDF: {e}")
return False
|