Update services/pdf_report.py
Browse files- services/pdf_report.py +99 -33
services/pdf_report.py
CHANGED
|
@@ -1,52 +1,118 @@
|
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from config.settings import settings # For APP_TITLE
|
| 9 |
-
from assets.logo import get_logo_path #
|
|
|
|
| 10 |
|
| 11 |
def generate_pdf_report(chat_messages: List[ChatMessage], patient_name: str = "Patient") -> BytesIO:
|
| 12 |
buffer = BytesIO()
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
styles = getSampleStyleSheet()
|
| 15 |
story = []
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
story.append(title)
|
| 31 |
story.append(Spacer(1, 0.2*inch))
|
| 32 |
|
| 33 |
-
# Patient Info
|
| 34 |
-
|
|
|
|
| 35 |
story.append(patient_info)
|
| 36 |
-
story.append(Spacer(1, 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# Chat Transcript
|
| 39 |
-
story.append(Paragraph("Consultation Transcript:", styles['h3']))
|
| 40 |
for msg in chat_messages:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return buffer
|
|
|
|
| 1 |
+
# /home/user/app/services/pdf_report.py
|
| 2 |
from reportlab.lib.pagesizes import letter
|
| 3 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
|
| 4 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 5 |
from reportlab.lib.units import inch
|
| 6 |
from io import BytesIO
|
| 7 |
+
from typing import List, Optional # Optional for tool_name
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
# Assuming ChatMessage is defined in models.py or models/chat.py
|
| 11 |
+
# Make sure ChatMessage has 'role', 'content', and optionally 'tool_name' attributes.
|
| 12 |
+
# Example ChatMessage structure (Pydantic or dataclass):
|
| 13 |
+
# class ChatMessage(BaseModel):
|
| 14 |
+
# role: str # "user", "assistant", "tool"
|
| 15 |
+
# content: str
|
| 16 |
+
# tool_name: Optional[str] = None
|
| 17 |
+
from models import ChatMessage # Or from models.chat import ChatMessage
|
| 18 |
from config.settings import settings # For APP_TITLE
|
| 19 |
+
from assets.logo import get_logo_path # Corrected import path
|
| 20 |
+
from services.logger import app_logger # For logging issues
|
| 21 |
|
| 22 |
def generate_pdf_report(chat_messages: List[ChatMessage], patient_name: str = "Patient") -> BytesIO:
|
| 23 |
buffer = BytesIO()
|
| 24 |
+
# Reduce margins for more content space
|
| 25 |
+
doc = SimpleDocTemplate(buffer, pagesize=letter,
|
| 26 |
+
leftMargin=0.75*inch, rightMargin=0.75*inch,
|
| 27 |
+
topMargin=0.75*inch, bottomMargin=0.75*inch)
|
| 28 |
styles = getSampleStyleSheet()
|
| 29 |
story = []
|
| 30 |
|
| 31 |
+
# Style adjustments
|
| 32 |
+
styles['h1'].alignment = 1 # Center align H1
|
| 33 |
+
styles['Normal'].spaceBefore = 6
|
| 34 |
+
styles['Normal'].spaceAfter = 6
|
| 35 |
+
code_style = styles['Code'] # For AI and Tool messages
|
| 36 |
+
code_style.spaceBefore = 6
|
| 37 |
+
code_style.spaceAfter = 6
|
| 38 |
+
code_style.leftIndent = 10 # Indent AI/Tool messages slightly
|
| 39 |
+
|
| 40 |
+
# 1. Logo (optional)
|
| 41 |
+
logo_path_str = get_logo_path()
|
| 42 |
+
if logo_path_str:
|
| 43 |
+
logo_file = Path(logo_path_str) # Convert to Path object
|
| 44 |
+
if logo_file.exists():
|
| 45 |
+
try:
|
| 46 |
+
# Adjust width/height as needed, preserve aspect ratio if possible
|
| 47 |
+
img = Image(logo_file, width=1.0*inch, height=1.0*inch, preserveAspectRatio=True)
|
| 48 |
+
img.hAlign = 'LEFT' # Align logo to the left
|
| 49 |
+
story.append(img)
|
| 50 |
+
story.append(Spacer(1, 0.2*inch))
|
| 51 |
+
except Exception as e:
|
| 52 |
+
app_logger.warning(f"Could not add logo to PDF from path '{logo_path_str}': {e}")
|
| 53 |
+
else:
|
| 54 |
+
app_logger.warning(f"Logo path '{logo_path_str}' from get_logo_path() does not exist.")
|
| 55 |
+
|
| 56 |
+
# 2. Title
|
| 57 |
+
title_text = f"{settings.APP_TITLE} - Consultation Report"
|
| 58 |
+
title = Paragraph(title_text, styles['h1'])
|
| 59 |
story.append(title)
|
| 60 |
story.append(Spacer(1, 0.2*inch))
|
| 61 |
|
| 62 |
+
# 3. Patient Info
|
| 63 |
+
patient_info_text = f"<b>Patient:</b> {patient_name}" # Make "Patient:" bold
|
| 64 |
+
patient_info = Paragraph(patient_info_text, styles['h2'])
|
| 65 |
story.append(patient_info)
|
| 66 |
+
story.append(Spacer(1, 0.3*inch)) # More space after patient info
|
| 67 |
+
|
| 68 |
+
# 4. Chat Transcript
|
| 69 |
+
story.append(Paragraph("<u>Consultation Transcript:</u>", styles['h3'])) # Underline transcript title
|
| 70 |
+
story.append(Spacer(1, 0.1*inch))
|
| 71 |
|
|
|
|
|
|
|
| 72 |
for msg in chat_messages:
|
| 73 |
+
prefix = ""
|
| 74 |
+
current_style = styles['Normal']
|
| 75 |
+
|
| 76 |
+
if msg.role == 'assistant':
|
| 77 |
+
prefix = "<b>AI Assistant:</b> "
|
| 78 |
+
current_style = code_style
|
| 79 |
+
elif msg.role == 'user':
|
| 80 |
+
prefix = "<b>You:</b> "
|
| 81 |
+
current_style = styles['Normal']
|
| 82 |
+
elif msg.role == 'tool':
|
| 83 |
+
tool_name = getattr(msg, 'tool_name', 'UnknownTool') # Handle if tool_name is missing
|
| 84 |
+
prefix = f"<b>Tool ({tool_name}):</b> "
|
| 85 |
+
current_style = code_style
|
| 86 |
+
else:
|
| 87 |
+
prefix = f"<b>{msg.role.capitalize()}:</b> " # Fallback for other roles
|
| 88 |
+
|
| 89 |
+
# Sanitize content for ReportLab Paragraph (handles HTML-like tags)
|
| 90 |
+
# Replace newlines with <br/> for PDF line breaks
|
| 91 |
+
content = msg.content.replace('\n', '<br/>\n')
|
| 92 |
+
# Escape < and > that are not part of <br/>
|
| 93 |
+
content = content.replace("<", "<").replace(">", ">").replace("<br/>", "<br/>")
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
story.append(Paragraph(prefix + content, current_style))
|
| 97 |
+
story.append(Spacer(1, 0.05*inch)) # Smaller spacer between messages
|
| 98 |
+
except Exception as e:
|
| 99 |
+
app_logger.error(f"Error adding message to PDF: {prefix}{content}. Error: {e}")
|
| 100 |
+
story.append(Paragraph(f"<i>Error rendering message: {e}</i>", styles['Italic']))
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
doc.build(story)
|
| 105 |
+
buffer.seek(0)
|
| 106 |
+
app_logger.info(f"PDF report generated successfully for patient: {patient_name}")
|
| 107 |
+
except Exception as e:
|
| 108 |
+
app_logger.error(f"Failed to build PDF document: {e}", exc_info=True)
|
| 109 |
+
# Return an empty or error buffer if build fails
|
| 110 |
+
buffer = BytesIO() # Reset buffer
|
| 111 |
+
error_doc = SimpleDocTemplate(buffer, pagesize=letter)
|
| 112 |
+
error_story = [Paragraph("Error generating PDF report. Please check logs.", styles['h1'])]
|
| 113 |
+
try:
|
| 114 |
+
error_doc.build(error_story)
|
| 115 |
+
except: pass # If even error doc fails, just return empty buffer
|
| 116 |
+
buffer.seek(0)
|
| 117 |
+
|
| 118 |
return buffer
|