Spaces:
Sleeping
Sleeping
Update pdf_generator.py
Browse files- pdf_generator.py +86 -34
pdf_generator.py
CHANGED
@@ -3,8 +3,8 @@ import re
|
|
3 |
from reportlab.lib import colors
|
4 |
from reportlab.lib.pagesizes import A4
|
5 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
6 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Flowable
|
7 |
-
from reportlab.lib.units import mm
|
8 |
from reportlab.graphics.shapes import Drawing, Rect, String, Line
|
9 |
|
10 |
class SliderFlowable(Flowable):
|
@@ -94,32 +94,22 @@ def create_page_template(canvas, doc):
|
|
94 |
canvas.drawString(30, 20, f"Page {doc.page}")
|
95 |
canvas.restoreState()
|
96 |
|
97 |
-
def generate_pdf(
|
98 |
buffer = io.BytesIO()
|
99 |
doc = SimpleDocTemplate(buffer, pagesize=A4, rightMargin=20*mm, leftMargin=20*mm, topMargin=20*mm, bottomMargin=20*mm)
|
100 |
styles = create_styles()
|
101 |
-
story = [Paragraph("
|
102 |
|
103 |
-
for page in pages[:-1]: # Skip the last page
|
104 |
if 'input_key' in page and page['input_key'] is not None:
|
105 |
story.append(Paragraph(page['title'], styles['Heading1']))
|
106 |
-
story.append(Paragraph(page['content'], styles['BodyText']))
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
conclusion = answers.get(f"{page['input_key']}_conclusion", "")
|
115 |
-
story.append(Paragraph(f"<b>Option selected:</b> {option}", styles['Answer']))
|
116 |
-
story.append(Paragraph(f"<b>Conclusion:</b> {conclusion}", styles['Answer']))
|
117 |
-
else:
|
118 |
-
story.append(Paragraph(f"<b>Answer:</b> {answer}", styles['Answer']))
|
119 |
-
|
120 |
-
# Add quantitative criteria visualization if applicable
|
121 |
-
if page['input_key'] in ['technological_literacy', 'cognitive_mismatch']:
|
122 |
-
story.extend(process_quantitative_criteria(answer, styles))
|
123 |
|
124 |
story.append(Spacer(1, 12))
|
125 |
|
@@ -127,36 +117,98 @@ def generate_pdf(pages, answers):
|
|
127 |
buffer.seek(0)
|
128 |
return buffer
|
129 |
|
130 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
story = []
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
if parsed:
|
136 |
-
name,
|
|
|
|
|
137 |
if is_percentage:
|
138 |
slider = SliderFlowable(name, value*100, min_val*100, max_val*100, is_percentage=True)
|
139 |
else:
|
140 |
slider = SliderFlowable(name, value, min_val, max_val, is_percentage=False)
|
|
|
141 |
story.append(slider)
|
|
|
|
|
142 |
return story
|
143 |
|
144 |
def parse_quantitative_criteria(input_string):
|
145 |
-
match = re.match(r'(.+)
|
146 |
if match:
|
147 |
-
name,
|
148 |
name = name.strip()
|
149 |
|
150 |
# Handle percentage inputs
|
151 |
-
is_percentage = '%' in
|
152 |
-
|
153 |
-
|
154 |
-
max_val = float(max_val.rstrip('%') if max_val else 100)
|
155 |
|
156 |
if is_percentage:
|
157 |
-
value /= 100
|
158 |
min_val /= 100
|
159 |
max_val /= 100
|
160 |
|
161 |
-
|
|
|
|
|
162 |
return None
|
|
|
3 |
from reportlab.lib import colors
|
4 |
from reportlab.lib.pagesizes import A4
|
5 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
6 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Flowable
|
7 |
+
from reportlab.lib.units import mm, inch
|
8 |
from reportlab.graphics.shapes import Drawing, Rect, String, Line
|
9 |
|
10 |
class SliderFlowable(Flowable):
|
|
|
94 |
canvas.drawString(30, 20, f"Page {doc.page}")
|
95 |
canvas.restoreState()
|
96 |
|
97 |
+
def generate_pdf(session_state):
|
98 |
buffer = io.BytesIO()
|
99 |
doc = SimpleDocTemplate(buffer, pagesize=A4, rightMargin=20*mm, leftMargin=20*mm, topMargin=20*mm, bottomMargin=20*mm)
|
100 |
styles = create_styles()
|
101 |
+
story = [Paragraph("Pilot Drafting", styles['Title'])]
|
102 |
|
103 |
+
for page in session_state.pages[:-1]: # Skip the last page
|
104 |
if 'input_key' in page and page['input_key'] is not None:
|
105 |
story.append(Paragraph(page['title'], styles['Heading1']))
|
|
|
106 |
|
107 |
+
if page['input_key'] == 'workload_scope':
|
108 |
+
story.extend(process_workload_scope(session_state, styles))
|
109 |
+
elif page['input_key'] == 'useful_assets':
|
110 |
+
story.extend(process_useful_assets(session_state, styles))
|
111 |
+
elif page['input_key'] == 'pilot_evaluation':
|
112 |
+
story.extend(process_pilot_evaluation(session_state, styles))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
story.append(Spacer(1, 12))
|
115 |
|
|
|
117 |
buffer.seek(0)
|
118 |
return buffer
|
119 |
|
120 |
+
def process_workload_scope(session_state, styles):
|
121 |
+
story = []
|
122 |
+
answers = session_state.answers['workload_scope']
|
123 |
+
|
124 |
+
story.append(Paragraph("Feature Prioritization", styles['Heading2']))
|
125 |
+
for i, feature in enumerate(answers['feature_prioritization']):
|
126 |
+
lines = feature.split('\n')
|
127 |
+
for line in lines:
|
128 |
+
story.append(Paragraph(f"<b>Feature {i+1}</b>: {line}", styles['Answer']))
|
129 |
+
|
130 |
+
# story.append(Paragraph("Preferred Start Period", styles['Heading2']))
|
131 |
+
# story.append(Paragraph(str(answers['preferred_start_period']), styles['Answer']))
|
132 |
+
story.append(Paragraph("Preferred Start Period", styles['Heading2']))
|
133 |
+
story.append(Paragraph(f"<b>{answers['preferred_start_period'][0]}</b> - <b>{answers['preferred_start_period'][1]}</b>", styles['Answer']))
|
134 |
+
|
135 |
+
story.append(Paragraph("Team Composition", styles['Heading2']))
|
136 |
+
for team_member in ['partner', 'ibm']:
|
137 |
+
lines = answers['team_composition'][team_member].split('\n')
|
138 |
+
for line in lines:
|
139 |
+
story.append(Paragraph(f"<b>{team_member.capitalize()}</b>: {line}", styles['Answer']))
|
140 |
+
|
141 |
+
return story
|
142 |
+
|
143 |
+
def process_useful_assets(session_state, styles):
|
144 |
+
story = []
|
145 |
+
answers = session_state.answers['useful_assets']
|
146 |
+
|
147 |
+
story.append(Paragraph("Solution Elements", styles['Heading2']))
|
148 |
+
for element in answers['solution_elements']:
|
149 |
+
story.append(Paragraph(element, styles['Answer']))
|
150 |
+
|
151 |
+
story.append(Paragraph("IBM Software", styles['Heading2']))
|
152 |
+
for software in answers['ibm_software']:
|
153 |
+
story.append(Paragraph(software, styles['Answer']))
|
154 |
+
|
155 |
+
story.append(Paragraph("Open Source Supports", styles['Heading2']))
|
156 |
+
for support in answers['open_source_supports']:
|
157 |
+
story.append(Paragraph(support, styles['Answer']))
|
158 |
+
|
159 |
+
return story
|
160 |
+
|
161 |
+
def process_pilot_evaluation(session_state, styles):
|
162 |
+
story = []
|
163 |
+
answers = session_state.answers['useful_assets']
|
164 |
+
|
165 |
+
story.extend(process_validation_criteria(answers, styles))
|
166 |
+
|
167 |
+
return story
|
168 |
+
|
169 |
+
def process_validation_criteria(session_state, styles):
|
170 |
story = []
|
171 |
+
|
172 |
+
# Qualitative Criteria
|
173 |
+
story.append(Paragraph("Qualitative Criteria:", styles['Heading2']))
|
174 |
+
for i, criterion in enumerate(session_state.get('qualitative', [])):
|
175 |
+
description = session_state.get(f'qual_desc_{i}', '')
|
176 |
+
story.append(Paragraph(f"<b>{criterion}</b>: <font face='Helvetica' size=11 color='#350863'>{description}</font>", styles['Answer']))
|
177 |
+
|
178 |
+
# Quantitative Criteria
|
179 |
+
story.append(Paragraph("Quantitative Criteria:", styles['Heading2']))
|
180 |
+
for i, criterion in enumerate(session_state.get('quantitative', [])):
|
181 |
+
parsed = parse_quantitative_criteria(criterion)
|
182 |
if parsed:
|
183 |
+
name, min_val, max_val, is_percentage, is_integer = parsed
|
184 |
+
value = session_state.get(f'quant_value_{i}', min_val)
|
185 |
+
|
186 |
if is_percentage:
|
187 |
slider = SliderFlowable(name, value*100, min_val*100, max_val*100, is_percentage=True)
|
188 |
else:
|
189 |
slider = SliderFlowable(name, value, min_val, max_val, is_percentage=False)
|
190 |
+
|
191 |
story.append(slider)
|
192 |
+
story.append(Paragraph(f"<b>{name}</b>: {value:.2f}", styles['Answer']))
|
193 |
+
|
194 |
return story
|
195 |
|
196 |
def parse_quantitative_criteria(input_string):
|
197 |
+
match = re.match(r'(.+)\[([-+]?(?:\d*\.*\d+)(?:%)?)\s*-\s*([-+]?(?:\d*\.*\d+)(?:%)?)?\]', input_string)
|
198 |
if match:
|
199 |
+
name, min_val, max_val = match.groups()
|
200 |
name = name.strip()
|
201 |
|
202 |
# Handle percentage inputs
|
203 |
+
is_percentage = '%' in min_val or '%' in max_val
|
204 |
+
min_val = float(min_val.rstrip('%'))
|
205 |
+
max_val = float(max_val.rstrip('%'))
|
|
|
206 |
|
207 |
if is_percentage:
|
|
|
208 |
min_val /= 100
|
209 |
max_val /= 100
|
210 |
|
211 |
+
is_integer = '.' not in input_string or (min_val.is_integer() and max_val.is_integer())
|
212 |
+
|
213 |
+
return name, min_val, max_val, is_percentage, is_integer
|
214 |
return None
|