Chamin09 commited on
Commit
34a1549
·
verified ·
1 Parent(s): a77d2e1

Update utils/pdf_generator.py

Browse files
Files changed (1) hide show
  1. utils/pdf_generator.py +116 -0
utils/pdf_generator.py CHANGED
@@ -404,6 +404,122 @@ def create_braille_pdf_with_comparison(original_text, braille_text, title="Menu
404
  return simple_buffer
405
 
406
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
  def create_braille_pdf_with_comparison_wrong(original_text, braille_text, title="Menu in Braille"):
408
  """
409
  Create a PDF file with side-by-side comparison of original text and Braille.
 
404
  return simple_buffer
405
 
406
 
407
+ def create_braille_pdf_with_comparison_slow(original_text, braille_text, title="Menu in Braille"):
408
+ """
409
+ Create a PDF file with side-by-side comparison of original text and Braille.
410
+
411
+ Args:
412
+ original_text: Original text content
413
+ braille_text: Braille translation
414
+ title: PDF title
415
+
416
+ Returns:
417
+ BytesIO object containing the PDF
418
+ """
419
+ try:
420
+ # Create a BytesIO object to store the PDF
421
+ buffer = io.BytesIO()
422
+
423
+ # Register a Unicode font that supports Braille
424
+ pdfmetrics.registerFont(TTFont('DejaVu', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
425
+
426
+ # Create the PDF document
427
+ doc = SimpleDocTemplate(
428
+ buffer,
429
+ pagesize=letter,
430
+ rightMargin=72,
431
+ leftMargin=72,
432
+ topMargin=72,
433
+ bottomMargin=72
434
+ )
435
+
436
+ # Define styles
437
+ styles = getSampleStyleSheet()
438
+ title_style = styles['Title']
439
+ heading_style = styles['Heading2']
440
+ normal_style = styles['Normal']
441
+
442
+ # Create a custom style for Braille text with Unicode support
443
+ braille_style = ParagraphStyle(
444
+ 'Braille',
445
+ parent=normal_style,
446
+ fontName='DejaVu', # Use DejaVu font which supports Unicode Braille
447
+ fontSize=14,
448
+ leading=18
449
+ )
450
+
451
+ # Create the content
452
+ content = []
453
+
454
+ # Add title
455
+ content.append(Paragraph(title, title_style))
456
+ content.append(Spacer(1, 12))
457
+
458
+ # Process text line by line
459
+ orig_lines = original_text.split('\n')
460
+ braille_lines = braille_text.split('\n')
461
+
462
+ # Make sure both lists have the same length
463
+ max_len = max(len(orig_lines), len(braille_lines))
464
+ orig_lines = orig_lines + [''] * (max_len - len(orig_lines))
465
+ braille_lines = braille_lines + [''] * (max_len - len(braille_lines))
466
+
467
+ # Break content into smaller chunks (10 lines per table)
468
+ chunk_size = 10
469
+ for chunk_start in range(0, max_len, chunk_size):
470
+ chunk_end = min(chunk_start + chunk_size, max_len)
471
+
472
+ # Create a table for this chunk
473
+ table_data = [["Original Text", "Braille Translation"]]
474
+
475
+ for i in range(chunk_start, chunk_end):
476
+ orig = orig_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
477
+ braille = braille_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
478
+
479
+ # Limit the length of each cell to prevent overflow
480
+ if len(orig) > 100:
481
+ orig = orig[:97] + "..."
482
+ if len(braille) > 100:
483
+ braille = braille[:97] + "..."
484
+
485
+ table_data.append([orig, braille])
486
+
487
+ # Create the table
488
+ table = Table(table_data, colWidths=[doc.width/2-12, doc.width/2-12])
489
+ table.setStyle(TableStyle([
490
+ ('FONT', (0, 0), (0, 0), 'Helvetica-Bold'),
491
+ ('FONT', (1, 0), (1, 0), 'Helvetica-Bold'),
492
+ ('BACKGROUND', (0, 0), (1, 0), colors.lightgrey),
493
+ ('ALIGN', (0, 0), (1, 0), 'CENTER'),
494
+ ('VALIGN', (0, 0), (-1, -1), 'TOP'),
495
+ ('GRID', (0, 0), (1, 0), 1, colors.black),
496
+ ('BOX', (0, 0), (-1, -1), 1, colors.black),
497
+ ('LINEABOVE', (0, 1), (-1, -1), 1, colors.black),
498
+ ('FONT', (0, 1), (0, -1), 'Helvetica'),
499
+ ('FONT', (1, 1), (1, -1), 'DejaVu')
500
+ ]))
501
+
502
+ content.append(table)
503
+ content.append(Spacer(1, 12))
504
+
505
+ # Build the PDF
506
+ doc.build(content)
507
+
508
+ # Reset buffer position to the beginning
509
+ buffer.seek(0)
510
+ return buffer
511
+ except Exception as e:
512
+ print(f"Error in create_braille_pdf_with_comparison: {str(e)}")
513
+ # Create a simple PDF with error message
514
+ simple_buffer = io.BytesIO()
515
+ doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
516
+ styles = getSampleStyleSheet()
517
+ content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
518
+ doc.build(content)
519
+ simple_buffer.seek(0)
520
+ return simple_buffer
521
+
522
+
523
  def create_braille_pdf_with_comparison_wrong(original_text, braille_text, title="Menu in Braille"):
524
  """
525
  Create a PDF file with side-by-side comparison of original text and Braille.