File size: 2,163 Bytes
1a2e2c8
 
87fa998
 
1a2e2c8
 
 
 
87fa998
1a2e2c8
 
 
 
2865031
1a2e2c8
 
 
 
 
 
 
 
87fa998
 
 
1a2e2c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87fa998
 
 
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
# src/pdf_utils.py

from io import BytesIO
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import inch
import os

# ---------------- FONT REGISTRATION ----------------

FONT_DIR = "src/fonts"

REGULAR_FONT = os.path.join(FONT_DIR, "NotoSansTamil-Regular.ttf")
BOLD_FONT = os.path.join(FONT_DIR, "NotoSansTamil-Bold.ttf")

pdfmetrics.registerFont(TTFont("Tamil", REGULAR_FONT))
pdfmetrics.registerFont(TTFont("Tamil-Bold", BOLD_FONT))

# ---------------- PDF GENERATOR ----------------

def answer_to_pdf(question: str, answer: str) -> BytesIO:
    buffer = BytesIO()

    doc = SimpleDocTemplate(
        buffer,
        pagesize=A4,
        rightMargin=72,
        leftMargin=72,
        topMargin=72,
        bottomMargin=72,
    )

    styles = getSampleStyleSheet()

    styles.add(
        ParagraphStyle(
            name="TitleStyle",
            fontName="Tamil-Bold",
            fontSize=14,
            spaceAfter=12,
        )
    )

    styles.add(
        ParagraphStyle(
            name="HeadingStyle",
            fontName="Tamil-Bold",
            fontSize=11,
            spaceAfter=6,
        )
    )

    styles.add(
        ParagraphStyle(
            name="BodyStyle",
            fontName="Tamil",
            fontSize=11,
            leading=14,
            spaceAfter=6,
        )
    )

    story = []

    # ---------- Title ----------
    story.append(Paragraph("OTT Bot – Answer", styles["TitleStyle"]))
    story.append(Spacer(1, 12))

    # ---------- Question ----------
    story.append(Paragraph("Question:", styles["HeadingStyle"]))
    story.append(Paragraph(question.replace("\n", "<br/>"), styles["BodyStyle"]))
    story.append(Spacer(1, 12))

    # ---------- Answer ----------
    story.append(Paragraph("Answer:", styles["HeadingStyle"]))
    story.append(Paragraph(answer.replace("\n", "<br/>"), styles["BodyStyle"]))

    doc.build(story)

    buffer.seek(0)
    return buffer