mgbam commited on
Commit
5ed5b69
·
verified ·
1 Parent(s): 2cbd099

Update services/pdf_report.py

Browse files
Files changed (1) hide show
  1. services/pdf_report.py +49 -8
services/pdf_report.py CHANGED
@@ -1,11 +1,52 @@
1
- from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
 
2
  from reportlab.lib.styles import getSampleStyleSheet
 
 
 
 
 
 
3
 
4
- def generate_pdf(report_data, filename="report.pdf"):
5
- doc = SimpleDocTemplate(filename)
 
6
  styles = getSampleStyleSheet()
7
- flow = [Paragraph("Clinical Report", styles["Title"]), Spacer(1,12)]
8
- for section, content in report_data.items():
9
- flow += [Paragraph(f"<b>{section}</b>", styles["Heading2"]), Paragraph(str(content), styles["BodyText"]), Spacer(1,12)]
10
- doc.build(flow)
11
- return filename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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