Spaces:
Sleeping
Sleeping
| # modules/pdf_generator.py - PDF generation functions | |
| import io | |
| from reportlab.lib.pagesizes import A4 | |
| from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle | |
| from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | |
| from reportlab.lib.units import inch | |
| from reportlab.lib import colors | |
| from reportlab.lib.enums import TA_CENTER | |
| def generate_basic_report(student_data, term_data, marks, design): | |
| """Generate basic PDF report""" | |
| buffer = io.BytesIO() | |
| doc = SimpleDocTemplate(buffer, pagesize=A4) | |
| story = [] | |
| styles = getSampleStyleSheet() | |
| # Title | |
| title_style = ParagraphStyle( | |
| 'CustomTitle', | |
| parent=styles['Heading1'], | |
| fontSize=14, | |
| textColor=colors.black, | |
| alignment=TA_CENTER | |
| ) | |
| story.append(Paragraph(f"<b>{design.school_name}</b>", title_style)) | |
| story.append(Paragraph(f"END OF TERM {term_data['term_number']} REPORT", title_style)) | |
| story.append(Spacer(1, 0.2*inch)) | |
| # Student info | |
| student_info = [ | |
| ['NAME:', student_data['name'], 'CLASS:', student_data['class_name']], | |
| ['REG NO:', student_data['registration_number'], 'TERM:', term_data['term_name']] | |
| ] | |
| student_table = Table(student_info, colWidths=[1*inch, 2*inch, 1*inch, 2*inch]) | |
| student_table.setStyle(TableStyle([ | |
| ('FONTNAME', (0,0), (0,-1), 'Helvetica-Bold'), | |
| ('FONTNAME', (2,0), (2,-1), 'Helvetica-Bold'), | |
| ('GRID', (0,0), (-1,-1), 1, colors.black), | |
| ])) | |
| story.append(student_table) | |
| story.append(Spacer(1, 0.2*inch)) | |
| # Results table | |
| results_data = [['SUBJECT', 'SCORE', 'GRADE']] | |
| for _, row in marks.iterrows(): | |
| results_data.append([row['subject'], f"{row['total']:.0f}", row['grade']]) | |
| results_table = Table(results_data, colWidths=[2*inch, 1*inch, 1*inch]) | |
| results_table.setStyle(TableStyle([ | |
| ('BACKGROUND', (0,0), (-1,0), colors.grey), | |
| ('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'), | |
| ('GRID', (0,0), (-1,-1), 1, colors.black), | |
| ])) | |
| story.append(results_table) | |
| doc.build(story) | |
| return buffer.getvalue() |