File size: 2,289 Bytes
1067825 | 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 | from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_CENTER
from fastapi.responses import FileResponse
from backend.state import session_store
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
def generate_pdf(text, file_path):
from reportlab.lib.enums import TA_CENTER
doc = SimpleDocTemplate(file_path)
styles = getSampleStyleSheet()
title_style = ParagraphStyle(
name="Title",
parent=styles["Heading1"],
alignment=TA_CENTER,
spaceAfter=20
)
section_style = ParagraphStyle(
name="Section",
parent=styles["Heading2"],
spaceBefore=15,
spaceAfter=10
)
bullet_style = ParagraphStyle(
name="Bullet",
parent=styles["Normal"],
leftIndent=15,
spaceAfter=5
)
normal_style = styles["Normal"]
content = []
lines = text.split("\n")
for line in lines:
line = line.strip()
line = line.replace("**", "")
if not line:
content.append(Spacer(1, 10))
continue
if "data analysis report" in line.lower():
content.append(Paragraph(line, title_style))
elif line[0].isdigit() and "." in line:
content.append(Paragraph(line, section_style))
elif line.startswith("*") or line.startswith("-"):
bullet = line.lstrip("*- ").strip()
content.append(Paragraph(f"• {bullet}", bullet_style))
else:
content.append(Paragraph(line, normal_style))
doc.build(content)
def generate_report_file(session_id):
if session_id not in session_store:
raise Exception("session not found")
ai_text = session_store[session_id].get("explanation")
if not ai_text:
raise Exception("Run /explain first")
file_path = f"report_{session_id}.pdf"
generate_pdf(ai_text, file_path)
return FileResponse(
path=file_path,
filename="report.pdf",
media_type="application/pdf"
) |