PILDRAFT / pdf_generator.py
MilanM's picture
Update pdf_generator.py
f725ded verified
raw
history blame
3.54 kB
from io import BytesIO
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
from reportlab.lib.units import mm
def create_styles():
styles = getSampleStyleSheet()
styles['Title'].fontName = 'Helvetica-Bold'
styles['Title'].fontSize = 18
styles['Title'].spaceAfter = 16
styles['Title'].textColor = colors.HexColor("#26004d")
styles['Heading1'].fontName = 'Helvetica-Bold'
styles['Heading1'].fontSize = 16
styles['Heading1'].spaceAfter = 10
styles['Heading1'].textColor = colors.HexColor("#3b0b75")
styles['Heading2'].fontName = 'Helvetica'
styles['Heading2'].fontSize = 14
styles['Heading2'].spaceAfter = 12
styles['Heading2'].textColor = colors.HexColor("#52176a")
styles['BodyText'].fontName = 'Helvetica'
styles['BodyText'].fontSize = 12
styles['BodyText'].spaceAfter = 12
styles['BodyText'].textColor = colors.HexColor("#26004d")
styles.add(ParagraphStyle(
name='Answer',
parent=styles['BodyText'],
backColor=colors.HexColor("#f0f2fd"),
borderColor=colors.HexColor("#9999ff"),
borderWidth=0.5,
borderPadding=(5, 5, 5, 5),
spaceAfter=10
))
return styles
def create_page_template(canvas, doc):
canvas.saveState()
canvas.setFillColor(colors.HexColor("#e6ebfb"))
canvas.rect(0, 0, doc.pagesize[0], doc.pagesize[1], fill=1)
canvas.setFillColor(colors.HexColor("#26004d"))
canvas.setFont('Helvetica', 9)
canvas.drawString(30, 20, f"Page {doc.page}")
canvas.restoreState()
def generate_pdf(pages, answers):
buffer = BytesIO()
doc = SimpleDocTemplate(buffer, pagesize=A4, rightMargin=20*mm, leftMargin=20*mm, topMargin=20*mm, bottomMargin=20*mm)
styles = create_styles()
story = [Paragraph("AI Trust and Opacity Evaluation", styles['Title'])]
for page in pages[:-1]: # Skip the last page
if 'input_key' in page and page['input_key'] is not None:
story.append(Paragraph(page['title'], styles['Heading1']))
story.append(Paragraph(page['content'], styles['BodyText']))
answer = answers.get(page['input_key'], "")
if isinstance(answer, list):
answer = ', '.join(answer)
if page.get('input_type') == 'combined':
option = answers.get(page['input_key'], "")
conclusion = answers.get(f"{page['input_key']}_conclusion", "")
answer = f"Option selected: {option}\n\nConclusion: {conclusion}"
# Create a table for the answer to allow multi-line text within a block
data = [[Paragraph(answer, styles['Answer'])]]
t = Table(data, colWidths=[450])
t.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, -1), colors.HexColor("#f0f2fd")),
('TEXTCOLOR', (0, 0), (-1, -1), colors.HexColor("#26004d")),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.HexColor("#9999ff")),
('BOX', (0, 0), (-1, -1), 0.25, colors.HexColor("#9999ff")),
]))
story.append(t)
story.append(Spacer(1, 12))
doc.build(story, onFirstPage=create_page_template, onLaterPages=create_page_template)
buffer.seek(0)
return buffer