OnlyTheTruth03 commited on
Commit
1a2e2c8
·
1 Parent(s): 9de5fcf

Tamil font added

Browse files
src/fonts/NotoSansTamil-Bold.ttf ADDED
Binary file (79 kB). View file
 
src/fonts/NotoSansTamil-Regular.ttf ADDED
Binary file (79 kB). View file
 
src/pdf_utils.py CHANGED
@@ -1,31 +1,84 @@
 
 
1
  from io import BytesIO
2
  from reportlab.lib.pagesizes import A4
3
- from reportlab.pdfgen import canvas
 
 
 
4
  from reportlab.lib.units import inch
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def answer_to_pdf(question: str, answer: str) -> BytesIO:
7
  buffer = BytesIO()
8
- c = canvas.Canvas(buffer, pagesize=A4)
9
- width, height = A4
10
-
11
- textobject = c.beginText(1 * inch, height - 1 * inch)
12
- textobject.setFont("Helvetica", 11)
13
-
14
- textobject.textLine("OTT Bot – Answer")
15
- textobject.textLine("-" * 50)
16
- textobject.textLine("")
17
- textobject.textLine(f"Question:")
18
- textobject.textLine(question)
19
- textobject.textLine("")
20
- textobject.textLine("Answer:")
21
- textobject.textLine("")
22
-
23
- for line in answer.split("\n"):
24
- textobject.textLine(line)
25
-
26
- c.drawText(textobject)
27
- c.showPage()
28
- c.save()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  buffer.seek(0)
31
  return buffer
 
1
+ # src/pdf_utils.py
2
+
3
  from io import BytesIO
4
  from reportlab.lib.pagesizes import A4
5
+ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
6
+ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
7
+ from reportlab.pdfbase import pdfmetrics
8
+ from reportlab.pdfbase.ttfonts import TTFont
9
  from reportlab.lib.units import inch
10
+ import os
11
+
12
+ # ---------------- FONT REGISTRATION ----------------
13
+
14
+ FONT_DIR = "fonts"
15
+
16
+ REGULAR_FONT = os.path.join(FONT_DIR, "NotoSansTamil-Regular.ttf")
17
+ BOLD_FONT = os.path.join(FONT_DIR, "NotoSansTamil-Bold.ttf")
18
+
19
+ pdfmetrics.registerFont(TTFont("Tamil", REGULAR_FONT))
20
+ pdfmetrics.registerFont(TTFont("Tamil-Bold", BOLD_FONT))
21
+
22
+ # ---------------- PDF GENERATOR ----------------
23
 
24
  def answer_to_pdf(question: str, answer: str) -> BytesIO:
25
  buffer = BytesIO()
26
+
27
+ doc = SimpleDocTemplate(
28
+ buffer,
29
+ pagesize=A4,
30
+ rightMargin=72,
31
+ leftMargin=72,
32
+ topMargin=72,
33
+ bottomMargin=72,
34
+ )
35
+
36
+ styles = getSampleStyleSheet()
37
+
38
+ styles.add(
39
+ ParagraphStyle(
40
+ name="TitleStyle",
41
+ fontName="Tamil-Bold",
42
+ fontSize=14,
43
+ spaceAfter=12,
44
+ )
45
+ )
46
+
47
+ styles.add(
48
+ ParagraphStyle(
49
+ name="HeadingStyle",
50
+ fontName="Tamil-Bold",
51
+ fontSize=11,
52
+ spaceAfter=6,
53
+ )
54
+ )
55
+
56
+ styles.add(
57
+ ParagraphStyle(
58
+ name="BodyStyle",
59
+ fontName="Tamil",
60
+ fontSize=11,
61
+ leading=14,
62
+ spaceAfter=6,
63
+ )
64
+ )
65
+
66
+ story = []
67
+
68
+ # ---------- Title ----------
69
+ story.append(Paragraph("OTT Bot – Answer", styles["TitleStyle"]))
70
+ story.append(Spacer(1, 12))
71
+
72
+ # ---------- Question ----------
73
+ story.append(Paragraph("Question:", styles["HeadingStyle"]))
74
+ story.append(Paragraph(question.replace("\n", "<br/>"), styles["BodyStyle"]))
75
+ story.append(Spacer(1, 12))
76
+
77
+ # ---------- Answer ----------
78
+ story.append(Paragraph("Answer:", styles["HeadingStyle"]))
79
+ story.append(Paragraph(answer.replace("\n", "<br/>"), styles["BodyStyle"]))
80
+
81
+ doc.build(story)
82
 
83
  buffer.seek(0)
84
  return buffer