|
from reportlab.lib.pagesizes import letter |
|
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image |
|
from reportlab.lib.styles import getSampleStyleSheet |
|
from reportlab.lib.units import inch |
|
from io import BytesIO |
|
from typing import List |
|
from models.chat import ChatMessage |
|
from config.settings import settings |
|
from assets.logo import get_logo_path |
|
|
|
def generate_pdf_report(chat_messages: List[ChatMessage], patient_name: str = "Patient") -> BytesIO: |
|
buffer = BytesIO() |
|
doc = SimpleDocTemplate(buffer, pagesize=letter) |
|
styles = getSampleStyleSheet() |
|
story = [] |
|
|
|
|
|
logo_path = get_logo_path() |
|
if logo_path: |
|
try: |
|
img = Image(logo_path, width=1*inch, height=1*inch) |
|
img.hAlign = 'LEFT' |
|
story.append(img) |
|
story.append(Spacer(1, 0.2*inch)) |
|
except Exception: |
|
pass |
|
|
|
|
|
title = Paragraph(f"{settings.APP_TITLE} - Consultation Report", styles['h1']) |
|
story.append(title) |
|
story.append(Spacer(1, 0.2*inch)) |
|
|
|
|
|
patient_info = Paragraph(f"Patient: {patient_name}", styles['h2']) |
|
story.append(patient_info) |
|
story.append(Spacer(1, 0.2*inch)) |
|
|
|
|
|
story.append(Paragraph("Consultation Transcript:", styles['h3'])) |
|
for msg in chat_messages: |
|
role_style = styles['Code'] if msg.role == 'assistant' else styles['Normal'] |
|
prefix = "AI Assistant: " if msg.role == 'assistant' else "You: " |
|
if msg.role == 'tool': |
|
prefix = f"Tool ({msg.tool_name}): " |
|
|
|
content = msg.content.replace('\n', '<br/>\n') |
|
story.append(Paragraph(f"<b>{prefix}</b>{content}", role_style)) |
|
story.append(Spacer(1, 0.1*inch)) |
|
|
|
doc.build(story) |
|
buffer.seek(0) |
|
return buffer |