File size: 10,998 Bytes
f9766bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
pdf_export.py
Generates formatted PDF exports for study notes and quiz results using reportlab.
"""
import io
from datetime import date
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import mm
from reportlab.platypus import (
    SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle,
    HRFlowable, KeepTogether
)
from reportlab.lib.enums import TA_CENTER, TA_LEFT

# ── Brand colours ─────────────────────────────────────────────────────────────
PURPLE = colors.HexColor("#6c47ff")
ORANGE = colors.HexColor("#f97316")
LIGHT  = colors.HexColor("#f7f4ef")
MUTED  = colors.HexColor("#6b6880")
DARK   = colors.HexColor("#1a1523")
WHITE  = colors.white
GREEN  = colors.HexColor("#10b981")
RED    = colors.HexColor("#ef4444")


def _base_styles():
    base = getSampleStyleSheet()
    styles = {
        "title": ParagraphStyle(
            "Title2", parent=base["Title"],
            fontSize=26, textColor=PURPLE, spaceAfter=4,
            fontName="Helvetica-Bold", alignment=TA_CENTER,
        ),
        "subtitle": ParagraphStyle(
            "Subtitle", parent=base["Normal"],
            fontSize=11, textColor=MUTED, spaceAfter=16,
            fontName="Helvetica", alignment=TA_CENTER,
        ),
        "section_head": ParagraphStyle(
            "SectionHead", parent=base["Heading2"],
            fontSize=13, textColor=PURPLE, spaceBefore=14, spaceAfter=6,
            fontName="Helvetica-Bold",
        ),
        "body": ParagraphStyle(
            "Body2", parent=base["Normal"],
            fontSize=10, textColor=DARK, spaceAfter=6,
            fontName="Helvetica", leading=15,
        ),
        "term": ParagraphStyle(
            "Term", parent=base["Normal"],
            fontSize=10, textColor=PURPLE, spaceAfter=2,
            fontName="Helvetica-Bold",
        ),
        "definition": ParagraphStyle(
            "Def", parent=base["Normal"],
            fontSize=9.5, textColor=MUTED, spaceAfter=8,
            fontName="Helvetica", leading=14,
        ),
        "summary_box": ParagraphStyle(
            "SummaryBox", parent=base["Normal"],
            fontSize=10, textColor=DARK, spaceAfter=6,
            fontName="Helvetica-Oblique", leading=15,
        ),
        "label": ParagraphStyle(
            "Label", parent=base["Normal"],
            fontSize=8, textColor=MUTED, spaceAfter=2,
            fontName="Helvetica", alignment=TA_CENTER,
        ),
        "score_big": ParagraphStyle(
            "ScoreBig", parent=base["Normal"],
            fontSize=40, textColor=PURPLE, spaceAfter=4,
            fontName="Helvetica-Bold", alignment=TA_CENTER,
        ),
        "feedback": ParagraphStyle(
            "Feedback", parent=base["Normal"],
            fontSize=12, textColor=DARK, spaceAfter=12,
            fontName="Helvetica-Bold", alignment=TA_CENTER,
        ),
        "q_text": ParagraphStyle(
            "QText", parent=base["Normal"],
            fontSize=10, textColor=DARK, spaceAfter=3,
            fontName="Helvetica-Bold",
        ),
        "q_detail": ParagraphStyle(
            "QDetail", parent=base["Normal"],
            fontSize=9.5, textColor=MUTED, spaceAfter=2,
            fontName="Helvetica",
        ),
    }
    return styles


def export_study_notes_pdf(content: dict) -> bytes:
    """Generate a PDF for study notes. Returns bytes."""
    buffer = io.BytesIO()
    doc = SimpleDocTemplate(
        buffer, pagesize=A4,
        leftMargin=20*mm, rightMargin=20*mm,
        topMargin=18*mm, bottomMargin=18*mm,
    )
    s = _base_styles()
    story = []

    # Header
    story.append(Paragraph("LearnCraft", s["title"]))
    story.append(Paragraph("Personalized Study Notes", s["subtitle"]))
    story.append(HRFlowable(width="100%", thickness=2, color=PURPLE, spaceAfter=12))

    # Meta table
    meta_data = [
        ["Topic", content.get("topic", "β€”"), "Level", content.get("level", "β€”")],
        ["Style", content.get("style", "β€”"), "Read Time", content.get("read_time", "β€”")],
        ["Generated", str(date.today()), "", ""],
    ]
    meta_table = Table(meta_data, colWidths=[30*mm, 65*mm, 30*mm, 50*mm])
    meta_table.setStyle(TableStyle([
        ("FONTNAME",    (0,0), (-1,-1), "Helvetica"),
        ("FONTSIZE",    (0,0), (-1,-1), 9),
        ("FONTNAME",    (0,0), (0,-1), "Helvetica-Bold"),
        ("FONTNAME",    (2,0), (2,-1), "Helvetica-Bold"),
        ("TEXTCOLOR",   (0,0), (0,-1), PURPLE),
        ("TEXTCOLOR",   (2,0), (2,-1), PURPLE),
        ("TEXTCOLOR",   (1,0), (1,-1), DARK),
        ("TEXTCOLOR",   (3,0), (3,-1), DARK),
        ("BACKGROUND",  (0,0), (-1,-1), LIGHT),
        ("ROWBACKGROUNDS", (0,0), (-1,-1), [LIGHT, WHITE]),
        ("GRID",        (0,0), (-1,-1), 0.5, colors.HexColor("#e2ddf5")),
        ("ROUNDEDCORNERS", [4]),
        ("TOPPADDING",  (0,0), (-1,-1), 5),
        ("BOTTOMPADDING",(0,0), (-1,-1), 5),
        ("LEFTPADDING", (0,0), (-1,-1), 8),
    ]))
    story.append(meta_table)
    story.append(Spacer(1, 10))

    # Sections
    for section in content.get("sections", []):
        story.append(Paragraph(f"β–Έ  {section['title']}", s["section_head"]))
        story.append(HRFlowable(width="100%", thickness=0.5, color=colors.HexColor("#e2ddf5"), spaceAfter=6))
        story.append(Paragraph(section["content"], s["body"]))

    # Key terms
    key_terms = content.get("key_terms", [])
    if key_terms:
        story.append(Spacer(1, 6))
        story.append(Paragraph("Key Terms", s["section_head"]))
        story.append(HRFlowable(width="100%", thickness=0.5, color=colors.HexColor("#e2ddf5"), spaceAfter=8))
        for term in key_terms:
            story.append(Paragraph(term["term"], s["term"]))
            story.append(Paragraph(term["definition"], s["definition"]))

    # Summary
    summary = content.get("summary", "")
    if summary:
        story.append(Spacer(1, 4))
        story.append(Paragraph("Quick Summary", s["section_head"]))
        summary_table = Table([[Paragraph(f'"{summary}"', s["summary_box"])]], colWidths=[170*mm])
        summary_table.setStyle(TableStyle([
            ("BACKGROUND",   (0,0), (-1,-1), colors.HexColor("#ede9fe")),
            ("LEFTPADDING",  (0,0), (-1,-1), 12),
            ("RIGHTPADDING", (0,0), (-1,-1), 12),
            ("TOPPADDING",   (0,0), (-1,-1), 10),
            ("BOTTOMPADDING",(0,0), (-1,-1), 10),
            ("ROUNDEDCORNERS", [8]),
        ]))
        story.append(summary_table)

    # Footer
    story.append(Spacer(1, 16))
    story.append(HRFlowable(width="100%", thickness=1, color=colors.HexColor("#e2ddf5"), spaceAfter=6))
    story.append(Paragraph(f"Generated by LearnCraft Β· {date.today()}", s["label"]))

    doc.build(story)
    return buffer.getvalue()


def export_quiz_results_pdf(quiz: dict, results: dict, answers: dict) -> bytes:
    """Generate a PDF for quiz results. Returns bytes."""
    buffer = io.BytesIO()
    doc = SimpleDocTemplate(
        buffer, pagesize=A4,
        leftMargin=20*mm, rightMargin=20*mm,
        topMargin=18*mm, bottomMargin=18*mm,
    )
    s = _base_styles()
    story = []

    # Header
    story.append(Paragraph("LearnCraft", s["title"]))
    story.append(Paragraph("Quiz Results Report", s["subtitle"]))
    story.append(HRFlowable(width="100%", thickness=2, color=PURPLE, spaceAfter=14))

    # Score hero
    score     = results["score_percent"]
    score_col = GREEN if score >= 60 else RED
    story.append(Paragraph(f"{score}%", ParagraphStyle(
        "BigScore", fontSize=48, textColor=score_col,
        fontName="Helvetica-Bold", alignment=TA_CENTER, spaceAfter=4,
    )))
    story.append(Paragraph(
        f"{results['correct']} / {results['total']} correct  Β·  {results['feedback']}",
        s["feedback"]
    ))
    story.append(Spacer(1, 6))

    # Meta
    meta_data = [
        ["Topic",      quiz.get("topic", "β€”"),      "Difficulty", quiz.get("difficulty", "β€”")],
        ["Questions",  str(results["total"]),         "Date",       str(date.today())],
    ]
    meta_table = Table(meta_data, colWidths=[30*mm, 65*mm, 30*mm, 50*mm])
    meta_table.setStyle(TableStyle([
        ("FONTNAME",    (0,0), (-1,-1), "Helvetica"),
        ("FONTSIZE",    (0,0), (-1,-1), 9),
        ("FONTNAME",    (0,0), (0,-1), "Helvetica-Bold"),
        ("FONTNAME",    (2,0), (2,-1), "Helvetica-Bold"),
        ("TEXTCOLOR",   (0,0), (0,-1), PURPLE),
        ("TEXTCOLOR",   (2,0), (2,-1), PURPLE),
        ("BACKGROUND",  (0,0), (-1,-1), LIGHT),
        ("ROWBACKGROUNDS", (0,0), (-1,-1), [LIGHT, WHITE]),
        ("GRID",        (0,0), (-1,-1), 0.5, colors.HexColor("#e2ddf5")),
        ("TOPPADDING",  (0,0), (-1,-1), 5),
        ("BOTTOMPADDING",(0,0), (-1,-1), 5),
        ("LEFTPADDING", (0,0), (-1,-1), 8),
    ]))
    story.append(meta_table)
    story.append(Spacer(1, 14))

    # Answer review
    story.append(Paragraph("Answer Review", s["section_head"]))
    story.append(HRFlowable(width="100%", thickness=0.5, color=colors.HexColor("#e2ddf5"), spaceAfter=8))

    for i, q in enumerate(quiz.get("questions", [])):
        correct    = results["details"][i]["correct"]
        user_ans   = str(answers.get(i, "No answer"))
        right_ans  = results["details"][i]["correct_answer"]
        explanation= results["details"][i].get("explanation", "")
        icon       = "βœ“" if correct else "βœ—"
        bg_color   = colors.HexColor("#d1fae5") if correct else colors.HexColor("#fee2e2")
        icon_color = GREEN if correct else RED

        block = [
            [
                Paragraph(f"<font color='{'#10b981' if correct else '#ef4444'}'><b>{icon}</b></font>  Q{i+1}: {q['question']}", s["q_text"]),
            ],
            [
                Paragraph(
                    f"Your answer: <font color='{'#10b981' if correct else '#ef4444'}'><b>{user_ans}</b></font>   |   "
                    f"Correct: <font color='#10b981'><b>{right_ans}</b></font>"
                    + (f"<br/><i>{explanation}</i>" if explanation else ""),
                    s["q_detail"]
                ),
            ],
        ]
        t = Table(block, colWidths=[170*mm])
        t.setStyle(TableStyle([
            ("BACKGROUND",    (0,0), (-1,-1), bg_color),
            ("LEFTPADDING",   (0,0), (-1,-1), 10),
            ("RIGHTPADDING",  (0,0), (-1,-1), 10),
            ("TOPPADDING",    (0,0), (-1,-1), 8),
            ("BOTTOMPADDING", (0,0), (-1,-1), 8),
            ("ROUNDEDCORNERS", [6]),
        ]))
        story.append(KeepTogether([t, Spacer(1, 5)]))

    # Footer
    story.append(Spacer(1, 12))
    story.append(HRFlowable(width="100%", thickness=1, color=colors.HexColor("#e2ddf5"), spaceAfter=6))
    story.append(Paragraph(f"Generated by LearnCraft Β· {date.today()}", s["label"]))

    doc.build(story)
    return buffer.getvalue()