Chamin09 commited on
Commit
2fb5ace
·
verified ·
1 Parent(s): c50b116

Update utils/pdf_generator.py

Browse files
Files changed (1) hide show
  1. utils/pdf_generator.py +100 -0
utils/pdf_generator.py CHANGED
@@ -199,6 +199,106 @@ def create_braille_pdf_with_comparison(original_text, braille_text, title="Menu
199
  """
200
  Create a PDF file with side-by-side comparison of original text and Braille.
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  Args:
203
  original_text: Original text content
204
  braille_text: Braille translation
 
199
  """
200
  Create a PDF file with side-by-side comparison of original text and Braille.
201
 
202
+ Args:
203
+ original_text: Original text content
204
+ braille_text: Braille translation
205
+ title: PDF title
206
+
207
+ Returns:
208
+ BytesIO object containing the PDF
209
+ """
210
+ try:
211
+ # Create a BytesIO object to store the PDF
212
+ buffer = io.BytesIO()
213
+
214
+ # Create the PDF document
215
+ doc = SimpleDocTemplate(
216
+ buffer,
217
+ pagesize=letter,
218
+ rightMargin=72,
219
+ leftMargin=72,
220
+ topMargin=72,
221
+ bottomMargin=72
222
+ )
223
+
224
+ # Define styles
225
+ styles = getSampleStyleSheet()
226
+ title_style = styles['Title']
227
+ heading_style = styles['Heading2']
228
+ normal_style = styles['Normal']
229
+
230
+ # Create a custom style for Braille text - use standard font
231
+ braille_style = ParagraphStyle(
232
+ 'Braille',
233
+ parent=normal_style,
234
+ fontName='Helvetica',
235
+ fontSize=14,
236
+ leading=18
237
+ )
238
+
239
+ # Create the content
240
+ content = []
241
+
242
+ # Add title
243
+ content.append(Paragraph(title, title_style))
244
+ content.append(Spacer(1, 12))
245
+
246
+ # Create a simpler table structure
247
+ data = [["Original Text", "Braille Translation"]]
248
+
249
+ # Process text line by line
250
+ orig_lines = original_text.split('\n')
251
+ braille_lines = braille_text.split('\n')
252
+
253
+ # Make sure both lists have the same length
254
+ max_len = max(len(orig_lines), len(braille_lines))
255
+ orig_lines = orig_lines + [''] * (max_len - len(orig_lines))
256
+ braille_lines = braille_lines + [''] * (max_len - len(braille_lines))
257
+
258
+ # Add each line pair to the table
259
+ for i in range(max_len):
260
+ orig = orig_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
261
+ braille = braille_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
262
+ data.append([orig, braille])
263
+
264
+ # Create the table
265
+ table = Table(data, colWidths=[doc.width/2-12, doc.width/2-12])
266
+ table.setStyle(TableStyle([
267
+ ('FONT', (0, 0), (1, 0), 'Helvetica-Bold'),
268
+ ('BACKGROUND', (0, 0), (1, 0), colors.lightgrey),
269
+ ('ALIGN', (0, 0), (1, 0), 'CENTER'),
270
+ ('VALIGN', (0, 0), (-1, -1), 'TOP'),
271
+ ('GRID', (0, 0), (1, 0), 1, colors.black),
272
+ ('BOX', (0, 0), (-1, -1), 1, colors.black),
273
+ ('LINEABOVE', (0, 1), (-1, -1), 1, colors.black),
274
+ ('FONT', (0, 1), (0, -1), 'Helvetica'),
275
+ ('FONT', (1, 1), (1, -1), 'Helvetica')
276
+ ]))
277
+
278
+ content.append(table)
279
+
280
+ # Build the PDF
281
+ doc.build(content)
282
+
283
+ # Reset buffer position to the beginning
284
+ buffer.seek(0)
285
+ return buffer
286
+ except Exception as e:
287
+ print(f"Error in create_braille_pdf_with_comparison: {str(e)}")
288
+ # Return the error message in a simple PDF
289
+ simple_buffer = io.BytesIO()
290
+ doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
291
+ styles = getSampleStyleSheet()
292
+ content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
293
+ doc.build(content)
294
+ simple_buffer.seek(0)
295
+ return simple_buffer
296
+
297
+
298
+ def create_braille_pdf_with_comparison1(original_text, braille_text, title="Menu in Braille"):
299
+ """
300
+ Create a PDF file with side-by-side comparison of original text and Braille.
301
+
302
  Args:
303
  original_text: Original text content
304
  braille_text: Braille translation