Spaces:
Runtime error
Runtime error
Update pdf_generator.py
Browse files- pdf_generator.py +86 -2
pdf_generator.py
CHANGED
|
@@ -1,9 +1,55 @@
|
|
| 1 |
from io import BytesIO
|
|
|
|
| 2 |
from reportlab.lib import colors
|
| 3 |
from reportlab.lib.pagesizes import A4
|
| 4 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 5 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
|
| 6 |
from reportlab.lib.units import mm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def create_styles():
|
| 9 |
styles = getSampleStyleSheet()
|
|
@@ -85,6 +131,44 @@ def generate_pdf(session_state):
|
|
| 85 |
story.append(t)
|
| 86 |
story.append(Spacer(1, 12))
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
doc.build(story, onFirstPage=create_page_template, onLaterPages=create_page_template)
|
| 89 |
buffer.seek(0)
|
| 90 |
-
return buffer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from io import BytesIO
|
| 2 |
+
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, Table, TableStyle, Flowable
|
| 7 |
from reportlab.lib.units import mm
|
| 8 |
+
from reportlab.graphics.shapes import Drawing, Rect, String, Line
|
| 9 |
+
|
| 10 |
+
class SliderFlowable(Flowable):
|
| 11 |
+
def __init__(self, name, value, min_val, max_val, is_percentage=False):
|
| 12 |
+
Flowable.__init__(self)
|
| 13 |
+
self.name = name
|
| 14 |
+
self.value = value
|
| 15 |
+
self.min_val = min_val
|
| 16 |
+
self.max_val = max_val
|
| 17 |
+
self.is_percentage = is_percentage
|
| 18 |
+
self.width = 400
|
| 19 |
+
self.height = 80
|
| 20 |
+
|
| 21 |
+
def draw(self):
|
| 22 |
+
drawing = Drawing(self.width, self.height)
|
| 23 |
+
|
| 24 |
+
# Draw slider bar
|
| 25 |
+
bar = Rect(50, 30, 300, 20, fillColor=colors.HexColor("#f7fbfd"), strokeColor=colors.HexColor("#9999ff"))
|
| 26 |
+
drawing.add(bar)
|
| 27 |
+
|
| 28 |
+
# Draw slider value
|
| 29 |
+
if self.max_val == self.min_val:
|
| 30 |
+
value_width = 50 # or some default width
|
| 31 |
+
else:
|
| 32 |
+
value_width = 50 + ((self.value - self.min_val) / (self.max_val - self.min_val) * 300)
|
| 33 |
+
value_bar = Rect(50, 30, value_width - 50, 20, fillColor=colors.HexColor("#9999ff"), strokeColor=None)
|
| 34 |
+
drawing.add(value_bar)
|
| 35 |
+
|
| 36 |
+
# Add slider name
|
| 37 |
+
drawing.add(String(0, 60, self.name, fontSize=12, fillColor=colors.HexColor("#26004d")))
|
| 38 |
+
|
| 39 |
+
# Add range labels
|
| 40 |
+
min_str = f"{self.min_val:.1f}%" if self.is_percentage else f"{self.min_val:.1f}"
|
| 41 |
+
max_str = f"{self.max_val:.1f}%" if self.is_percentage else f"{self.max_val:.1f}"
|
| 42 |
+
drawing.add(String(40, 10, min_str, fontSize=10, fillColor=colors.HexColor("#26004d")))
|
| 43 |
+
drawing.add(String(340, 10, max_str, fontSize=10, fillColor=colors.HexColor("#26004d")))
|
| 44 |
+
|
| 45 |
+
# Add value label
|
| 46 |
+
value_str = f"{self.value:.1f}%" if self.is_percentage else f"{self.value:.1f}"
|
| 47 |
+
drawing.add(String(value_width - 20, 55, value_str, fontSize=10, fillColor=colors.HexColor("#26004d")))
|
| 48 |
+
|
| 49 |
+
# Add value marker
|
| 50 |
+
drawing.add(Line(value_width, 25, value_width, 55, strokeColor=colors.HexColor("#26004d"), strokeWidth=2))
|
| 51 |
+
|
| 52 |
+
drawing.drawOn(self.canv, 0, 0)
|
| 53 |
|
| 54 |
def create_styles():
|
| 55 |
styles = getSampleStyleSheet()
|
|
|
|
| 131 |
story.append(t)
|
| 132 |
story.append(Spacer(1, 12))
|
| 133 |
|
| 134 |
+
# Add quantitative criteria visualization if applicable
|
| 135 |
+
if page['input_key'] in ['technological_literacy', 'cognitive_mismatch']:
|
| 136 |
+
story.extend(process_quantitative_criteria(answer, styles))
|
| 137 |
+
|
| 138 |
doc.build(story, onFirstPage=create_page_template, onLaterPages=create_page_template)
|
| 139 |
buffer.seek(0)
|
| 140 |
+
return buffer
|
| 141 |
+
|
| 142 |
+
def process_quantitative_criteria(answer, styles):
|
| 143 |
+
story = []
|
| 144 |
+
lines = answer.split('\n')
|
| 145 |
+
for line in lines:
|
| 146 |
+
parsed = parse_quantitative_criteria(line)
|
| 147 |
+
if parsed:
|
| 148 |
+
name, value, min_val, max_val, is_percentage = parsed
|
| 149 |
+
if is_percentage:
|
| 150 |
+
slider = SliderFlowable(name, value*100, min_val*100, max_val*100, is_percentage=True)
|
| 151 |
+
else:
|
| 152 |
+
slider = SliderFlowable(name, value, min_val, max_val, is_percentage=False)
|
| 153 |
+
story.append(slider)
|
| 154 |
+
return story
|
| 155 |
+
|
| 156 |
+
def parse_quantitative_criteria(input_string):
|
| 157 |
+
match = re.match(r'(.+):\s*([-+]?(?:\d*\.*\d+)(?:%)?)(?:\s*\[([-+]?(?:\d*\.*\d+)(?:%)?)\s*-\s*([-+]?(?:\d*\.*\d+)(?:%)?)?\])?', input_string)
|
| 158 |
+
if match:
|
| 159 |
+
name, value, min_val, max_val = match.groups()
|
| 160 |
+
name = name.strip()
|
| 161 |
+
|
| 162 |
+
# Handle percentage inputs
|
| 163 |
+
is_percentage = '%' in value or '%' in min_val or '%' in max_val
|
| 164 |
+
value = float(value.rstrip('%'))
|
| 165 |
+
min_val = float(min_val.rstrip('%') if min_val else 0)
|
| 166 |
+
max_val = float(max_val.rstrip('%') if max_val else 100)
|
| 167 |
+
|
| 168 |
+
if is_percentage:
|
| 169 |
+
value /= 100
|
| 170 |
+
min_val /= 100
|
| 171 |
+
max_val /= 100
|
| 172 |
+
|
| 173 |
+
return name, value, min_val, max_val, is_percentage
|
| 174 |
+
return None
|