Spaces:
Sleeping
Sleeping
File size: 4,776 Bytes
e2a1a46 | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from io import BytesIO
from datetime import datetime
class PrescriptionPDFGenerator:
def __init__(self):
self.styles = getSampleStyleSheet()
self._create_custom_styles()
def _create_custom_styles(self):
self.styles.add(ParagraphStyle(
name='Header',
parent=self.styles['Heading1'],
fontSize=18,
spaceAfter=20,
textColor=colors.HexColor('#1B67E0')
))
self.styles.add(ParagraphStyle(
name='SubHeader',
parent=self.styles['Heading2'],
fontSize=14,
spaceAfter=10,
textColor=colors.HexColor('#2C3E50')
))
self.styles.add(ParagraphStyle(
name='NormalText',
parent=self.styles['Normal'],
fontSize=10,
spaceAfter=6
))
self.styles.add(ParagraphStyle(
name='Warning',
parent=self.styles['Normal'],
fontSize=10,
textColor=colors.red,
spaceAfter=6
))
def generate_report(self, data):
buffer = BytesIO()
doc = SimpleDocTemplate(buffer, pagesize=letter,
rightMargin=72, leftMargin=72,
topMargin=72, bottomMargin=72)
story = []
# Header
story.append(Paragraph("Prescription Analysis Report", self.styles['Header']))
story.append(Paragraph(f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M')}", self.styles['NormalText']))
story.append(Spacer(1, 12))
# Patient Info
story.append(Paragraph("Patient Information", self.styles['SubHeader']))
patient_data = [
["Patient Name:", data.get('patient_name', 'N/A')],
["Doctor Name:", data.get('doctor_name', 'N/A')],
["Date:", data.get('date', 'N/A')]
]
t = Table(patient_data, colWidths=[1.5*inch, 4*inch])
t.setStyle(TableStyle([
('FONTNAME', (0,0), (0,-1), 'Helvetica-Bold'),
('FONTNAME', (1,0), (1,-1), 'Helvetica'),
('TEXTCOLOR', (0,0), (-1,-1), colors.black),
('ALIGN', (0,0), (-1,-1), 'LEFT'),
('VALIGN', (0,0), (-1,-1), 'TOP'),
]))
story.append(t)
story.append(Spacer(1, 20))
# Medicines
story.append(Paragraph("Prescribed Medicines", self.styles['SubHeader']))
medicines = data.get('medicines', [])
if medicines:
med_data = [["Medicine", "Dosage", "Instructions"]]
for med in medicines:
med_data.append([
med.get('name', 'Unknown'),
f"{med.get('strength', '')} {med.get('frequency', '')}",
med.get('instructions', '')
])
t = Table(med_data, colWidths=[2*inch, 1.5*inch, 3*inch])
t.setStyle(TableStyle([
('BACKGROUND', (0,0), (-1,0), colors.HexColor('#E8F2FF')),
('TEXTCOLOR', (0,0), (-1,0), colors.HexColor('#1B67E0')),
('ALIGN', (0,0), (-1,-1), 'LEFT'),
('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'),
('FONTSIZE', (0,0), (-1,0), 10),
('BOTTOMPADDING', (0,0), (-1,0), 12),
('BACKGROUND', (0,1), (-1,-1), colors.white),
('TEXTCOLOR', (0,1), (-1,-1), colors.black),
('FONTNAME', (0,1), (-1,-1), 'Helvetica'),
('FONTSIZE', (0,1), (-1,-1), 9),
('GRID', (0,0), (-1,-1), 1, colors.HexColor('#EBECF0')),
('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
]))
story.append(t)
else:
story.append(Paragraph("No medicines detected.", self.styles['NormalText']))
story.append(Spacer(1, 20))
# Health Tip
if data.get('health_tip'):
story.append(Paragraph("Health Tip", self.styles['SubHeader']))
story.append(Paragraph(data['health_tip'], self.styles['NormalText']))
story.append(Spacer(1, 20))
# Disclaimer
story.append(Spacer(1, 30))
story.append(Paragraph("DISCLAIMER: This report is generated by AI and may contain errors. Please consult a healthcare professional for medical advice.", self.styles['Warning']))
doc.build(story)
buffer.seek(0)
return buffer |