Update services/pdf_report.py
Browse files- services/pdf_report.py +49 -8
services/pdf_report.py
CHANGED
@@ -1,11 +1,52 @@
|
|
1 |
-
from reportlab.
|
|
|
2 |
from reportlab.lib.styles import getSampleStyleSheet
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
def
|
5 |
-
|
|
|
6 |
styles = getSampleStyleSheet()
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from reportlab.lib.pagesizes import letter
|
2 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
|
3 |
from reportlab.lib.styles import getSampleStyleSheet
|
4 |
+
from reportlab.lib.units import inch
|
5 |
+
from io import BytesIO
|
6 |
+
from typing import List
|
7 |
+
from models.chat import ChatMessage
|
8 |
+
from config.settings import settings # For APP_TITLE
|
9 |
+
from assets.logo import get_logo_path # We'll create this
|
10 |
|
11 |
+
def generate_pdf_report(chat_messages: List[ChatMessage], patient_name: str = "Patient") -> BytesIO:
|
12 |
+
buffer = BytesIO()
|
13 |
+
doc = SimpleDocTemplate(buffer, pagesize=letter)
|
14 |
styles = getSampleStyleSheet()
|
15 |
+
story = []
|
16 |
+
|
17 |
+
# Logo (optional)
|
18 |
+
logo_path = get_logo_path()
|
19 |
+
if logo_path:
|
20 |
+
try:
|
21 |
+
img = Image(logo_path, width=1*inch, height=1*inch)
|
22 |
+
img.hAlign = 'LEFT'
|
23 |
+
story.append(img)
|
24 |
+
story.append(Spacer(1, 0.2*inch))
|
25 |
+
except Exception:
|
26 |
+
pass # Handle if logo not found or invalid
|
27 |
+
|
28 |
+
# Title
|
29 |
+
title = Paragraph(f"{settings.APP_TITLE} - Consultation Report", styles['h1'])
|
30 |
+
story.append(title)
|
31 |
+
story.append(Spacer(1, 0.2*inch))
|
32 |
+
|
33 |
+
# Patient Info
|
34 |
+
patient_info = Paragraph(f"Patient: {patient_name}", styles['h2'])
|
35 |
+
story.append(patient_info)
|
36 |
+
story.append(Spacer(1, 0.2*inch))
|
37 |
+
|
38 |
+
# Chat Transcript
|
39 |
+
story.append(Paragraph("Consultation Transcript:", styles['h3']))
|
40 |
+
for msg in chat_messages:
|
41 |
+
role_style = styles['Code'] if msg.role == 'assistant' else styles['Normal']
|
42 |
+
prefix = "AI Assistant: " if msg.role == 'assistant' else "You: "
|
43 |
+
if msg.role == 'tool':
|
44 |
+
prefix = f"Tool ({msg.tool_name}): "
|
45 |
+
|
46 |
+
content = msg.content.replace('\n', '<br/>\n') # Handle newlines in PDF
|
47 |
+
story.append(Paragraph(f"<b>{prefix}</b>{content}", role_style))
|
48 |
+
story.append(Spacer(1, 0.1*inch))
|
49 |
+
|
50 |
+
doc.build(story)
|
51 |
+
buffer.seek(0)
|
52 |
+
return buffer
|