Chamin09 commited on
Commit
fd58911
·
verified ·
1 Parent(s): 9d0323b

Update utils/pdf_generator.py

Browse files
Files changed (1) hide show
  1. utils/pdf_generator.py +85 -0
utils/pdf_generator.py CHANGED
@@ -31,10 +31,95 @@ try:
31
  except Exception as e:
32
  print(f"Error registering font: {str(e)}")
33
 
 
34
  def create_braille_pdf(original_text, braille_text, title="Menu in Braille"):
35
  """
36
  Create a PDF file with original text and its Braille translation.
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  Args:
39
  original_text: Original text content
40
  braille_text: Braille translation
 
31
  except Exception as e:
32
  print(f"Error registering font: {str(e)}")
33
 
34
+
35
  def create_braille_pdf(original_text, braille_text, title="Menu in Braille"):
36
  """
37
  Create a PDF file with original text and its Braille translation.
38
 
39
+ Args:
40
+ original_text: Original text content
41
+ braille_text: Braille translation
42
+ title: PDF title
43
+
44
+ Returns:
45
+ BytesIO object containing the PDF
46
+ """
47
+ try:
48
+ # Create a BytesIO object to store the PDF
49
+ buffer = io.BytesIO()
50
+
51
+ # Create the PDF document
52
+ doc = SimpleDocTemplate(
53
+ buffer,
54
+ pagesize=letter,
55
+ rightMargin=72,
56
+ leftMargin=72,
57
+ topMargin=72,
58
+ bottomMargin=72
59
+ )
60
+
61
+ # Define styles
62
+ styles = getSampleStyleSheet()
63
+ title_style = styles['Title']
64
+ heading_style = styles['Heading2']
65
+ normal_style = styles['Normal']
66
+
67
+ # Create a custom style for Braille text
68
+ braille_style = ParagraphStyle(
69
+ 'Braille',
70
+ parent=normal_style,
71
+ fontName='Helvetica', # Use standard font to avoid issues
72
+ fontSize=14,
73
+ leading=18,
74
+ spaceAfter=12
75
+ )
76
+
77
+ # Create the content
78
+ content = []
79
+
80
+ # Add title
81
+ content.append(Paragraph(title, title_style))
82
+ content.append(Spacer(1, 12))
83
+
84
+ # Add original text section
85
+ content.append(Paragraph("Original Text", heading_style))
86
+ content.append(Spacer(1, 6))
87
+
88
+ # Split original text by lines and add each as a paragraph
89
+ for line in original_text.split('\n'):
90
+ if line.strip():
91
+ content.append(Paragraph(line.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'), normal_style))
92
+ else:
93
+ content.append(Spacer(1, 12))
94
+
95
+ content.append(Spacer(1, 24))
96
+
97
+ # Add Braille section
98
+ content.append(Paragraph("Braille Translation", heading_style))
99
+ content.append(Spacer(1, 6))
100
+
101
+ # Split Braille text by lines and add each as a paragraph
102
+ for line in braille_text.split('\n'):
103
+ if line.strip():
104
+ content.append(Paragraph(line.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'), braille_style))
105
+ else:
106
+ content.append(Spacer(1, 12))
107
+
108
+ # Build the PDF
109
+ doc.build(content)
110
+
111
+ # Reset buffer position to the beginning
112
+ buffer.seek(0)
113
+ return buffer
114
+ except Exception as e:
115
+ print(f"Error in create_braille_pdf: {str(e)}")
116
+ raise
117
+
118
+
119
+ def create_braille_pdf1(original_text, braille_text, title="Menu in Braille"):
120
+ """
121
+ Create a PDF file with original text and its Braille translation.
122
+
123
  Args:
124
  original_text: Original text content
125
  braille_text: Braille translation