File size: 1,915 Bytes
5ed5b69
 
ab81fa8
5ed5b69
 
 
 
 
 
ab81fa8
5ed5b69
 
 
ab81fa8
5ed5b69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 # For APP_TITLE
from assets.logo import get_logo_path # We'll create this

def generate_pdf_report(chat_messages: List[ChatMessage], patient_name: str = "Patient") -> BytesIO:
    buffer = BytesIO()
    doc = SimpleDocTemplate(buffer, pagesize=letter)
    styles = getSampleStyleSheet()
    story = []

    # Logo (optional)
    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 # Handle if logo not found or invalid

    # Title
    title = Paragraph(f"{settings.APP_TITLE} - Consultation Report", styles['h1'])
    story.append(title)
    story.append(Spacer(1, 0.2*inch))

    # Patient Info
    patient_info = Paragraph(f"Patient: {patient_name}", styles['h2'])
    story.append(patient_info)
    story.append(Spacer(1, 0.2*inch))

    # Chat Transcript
    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') # Handle newlines in PDF
        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