Spaces:
Sleeping
Sleeping
Update utils/pdf_generator.py
Browse files- utils/pdf_generator.py +201 -1
utils/pdf_generator.py
CHANGED
|
@@ -31,11 +31,104 @@ try:
|
|
| 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
|
|
@@ -199,6 +292,113 @@ 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
|
|
|
|
| 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
|
| 41 |
+
title: PDF title
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
BytesIO object containing the PDF
|
| 45 |
+
"""
|
| 46 |
+
try:
|
| 47 |
+
# Create a BytesIO object to store the PDF
|
| 48 |
+
buffer = io.BytesIO()
|
| 49 |
+
|
| 50 |
+
# Register a Unicode font that supports Braille
|
| 51 |
+
pdfmetrics.registerFont(TTFont('DejaVu', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
|
| 52 |
+
|
| 53 |
+
# Create the PDF document
|
| 54 |
+
doc = SimpleDocTemplate(
|
| 55 |
+
buffer,
|
| 56 |
+
pagesize=letter,
|
| 57 |
+
rightMargin=72,
|
| 58 |
+
leftMargin=72,
|
| 59 |
+
topMargin=72,
|
| 60 |
+
bottomMargin=72
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Define styles
|
| 64 |
+
styles = getSampleStyleSheet()
|
| 65 |
+
title_style = styles['Title']
|
| 66 |
+
heading_style = styles['Heading2']
|
| 67 |
+
normal_style = styles['Normal']
|
| 68 |
+
|
| 69 |
+
# Create a custom style for Braille text with Unicode support
|
| 70 |
+
braille_style = ParagraphStyle(
|
| 71 |
+
'Braille',
|
| 72 |
+
parent=normal_style,
|
| 73 |
+
fontName='DejaVu', # Use DejaVu font which supports Unicode Braille
|
| 74 |
+
fontSize=14,
|
| 75 |
+
leading=18,
|
| 76 |
+
spaceAfter=12
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Create the content
|
| 80 |
+
content = []
|
| 81 |
+
|
| 82 |
+
# Add title
|
| 83 |
+
content.append(Paragraph(title, title_style))
|
| 84 |
+
content.append(Spacer(1, 12))
|
| 85 |
+
|
| 86 |
+
# Add original text section
|
| 87 |
+
content.append(Paragraph("Original Text", heading_style))
|
| 88 |
+
content.append(Spacer(1, 6))
|
| 89 |
+
|
| 90 |
+
# Split original text by lines and add each as a paragraph
|
| 91 |
+
for line in original_text.split('\n'):
|
| 92 |
+
if line.strip():
|
| 93 |
+
content.append(Paragraph(line.replace('&', '&').replace('<', '<').replace('>', '>'), normal_style))
|
| 94 |
+
else:
|
| 95 |
+
content.append(Spacer(1, 12))
|
| 96 |
+
|
| 97 |
+
content.append(Spacer(1, 24))
|
| 98 |
+
|
| 99 |
+
# Add Braille section
|
| 100 |
+
content.append(Paragraph("Braille Translation", heading_style))
|
| 101 |
+
content.append(Spacer(1, 6))
|
| 102 |
+
|
| 103 |
+
# Split Braille text by lines and add each as a paragraph
|
| 104 |
+
for line in braille_text.split('\n'):
|
| 105 |
+
if line.strip():
|
| 106 |
+
content.append(Paragraph(line.replace('&', '&').replace('<', '<').replace('>', '>'), braille_style))
|
| 107 |
+
else:
|
| 108 |
+
content.append(Spacer(1, 12))
|
| 109 |
+
|
| 110 |
+
# Build the PDF
|
| 111 |
+
doc.build(content)
|
| 112 |
+
|
| 113 |
+
# Reset buffer position to the beginning
|
| 114 |
+
buffer.seek(0)
|
| 115 |
+
return buffer
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"Error in create_braille_pdf: {str(e)}")
|
| 118 |
+
# Create a simple PDF with error message
|
| 119 |
+
simple_buffer = io.BytesIO()
|
| 120 |
+
doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
|
| 121 |
+
styles = getSampleStyleSheet()
|
| 122 |
+
content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
|
| 123 |
+
doc.build(content)
|
| 124 |
+
simple_buffer.seek(0)
|
| 125 |
+
return simple_buffer
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def create_braille_pdf_working(original_text, braille_text, title="Menu in Braille"):
|
| 129 |
+
"""
|
| 130 |
+
Create a PDF file with original text and its Braille translation.
|
| 131 |
+
|
| 132 |
Args:
|
| 133 |
original_text: Original text content
|
| 134 |
braille_text: Braille translation
|
|
|
|
| 292 |
"""
|
| 293 |
Create a PDF file with side-by-side comparison of original text and Braille.
|
| 294 |
|
| 295 |
+
Args:
|
| 296 |
+
original_text: Original text content
|
| 297 |
+
braille_text: Braille translation
|
| 298 |
+
title: PDF title
|
| 299 |
+
|
| 300 |
+
Returns:
|
| 301 |
+
BytesIO object containing the PDF
|
| 302 |
+
"""
|
| 303 |
+
try:
|
| 304 |
+
# Create a BytesIO object to store the PDF
|
| 305 |
+
buffer = io.BytesIO()
|
| 306 |
+
|
| 307 |
+
# Register a Unicode font that supports Braille
|
| 308 |
+
pdfmetrics.registerFont(TTFont('DejaVu', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
|
| 309 |
+
|
| 310 |
+
# Create the PDF document
|
| 311 |
+
doc = SimpleDocTemplate(
|
| 312 |
+
buffer,
|
| 313 |
+
pagesize=letter,
|
| 314 |
+
rightMargin=72,
|
| 315 |
+
leftMargin=72,
|
| 316 |
+
topMargin=72,
|
| 317 |
+
bottomMargin=72
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
# Define styles
|
| 321 |
+
styles = getSampleStyleSheet()
|
| 322 |
+
title_style = styles['Title']
|
| 323 |
+
heading_style = styles['Heading2']
|
| 324 |
+
normal_style = styles['Normal']
|
| 325 |
+
|
| 326 |
+
# Create a custom style for Braille text with Unicode support
|
| 327 |
+
braille_style = ParagraphStyle(
|
| 328 |
+
'Braille',
|
| 329 |
+
parent=normal_style,
|
| 330 |
+
fontName='DejaVu', # Use DejaVu font which supports Unicode Braille
|
| 331 |
+
fontSize=14,
|
| 332 |
+
leading=18
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
# Create the content
|
| 336 |
+
content = []
|
| 337 |
+
|
| 338 |
+
# Add title
|
| 339 |
+
content.append(Paragraph(title, title_style))
|
| 340 |
+
content.append(Spacer(1, 12))
|
| 341 |
+
|
| 342 |
+
# Create a simpler table structure
|
| 343 |
+
table_data = [["Original Text", "Braille Translation"]]
|
| 344 |
+
|
| 345 |
+
# Process text line by line
|
| 346 |
+
orig_lines = original_text.split('\n')
|
| 347 |
+
braille_lines = braille_text.split('\n')
|
| 348 |
+
|
| 349 |
+
# Make sure both lists have the same length
|
| 350 |
+
max_len = max(len(orig_lines), len(braille_lines))
|
| 351 |
+
orig_lines = orig_lines + [''] * (max_len - len(orig_lines))
|
| 352 |
+
braille_lines = braille_lines + [''] * (max_len - len(braille_lines))
|
| 353 |
+
|
| 354 |
+
# Add each line pair to the table
|
| 355 |
+
for i in range(max_len):
|
| 356 |
+
orig = orig_lines[i].replace('&', '&').replace('<', '<').replace('>', '>')
|
| 357 |
+
braille = braille_lines[i].replace('&', '&').replace('<', '<').replace('>', '>')
|
| 358 |
+
|
| 359 |
+
# Create paragraphs with appropriate styles
|
| 360 |
+
orig_para = Paragraph(orig, normal_style)
|
| 361 |
+
braille_para = Paragraph(braille, braille_style)
|
| 362 |
+
|
| 363 |
+
table_data.append([orig_para, braille_para])
|
| 364 |
+
|
| 365 |
+
# Create the table
|
| 366 |
+
table = Table(table_data, colWidths=[doc.width/2-12, doc.width/2-12])
|
| 367 |
+
table.setStyle(TableStyle([
|
| 368 |
+
('FONT', (0, 0), (0, 0), 'Helvetica-Bold'),
|
| 369 |
+
('FONT', (1, 0), (1, 0), 'Helvetica-Bold'),
|
| 370 |
+
('BACKGROUND', (0, 0), (1, 0), colors.lightgrey),
|
| 371 |
+
('ALIGN', (0, 0), (1, 0), 'CENTER'),
|
| 372 |
+
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
| 373 |
+
('GRID', (0, 0), (1, 0), 1, colors.black),
|
| 374 |
+
('BOX', (0, 0), (-1, -1), 1, colors.black),
|
| 375 |
+
('LINEABOVE', (0, 1), (-1, -1), 1, colors.black)
|
| 376 |
+
]))
|
| 377 |
+
|
| 378 |
+
content.append(table)
|
| 379 |
+
|
| 380 |
+
# Build the PDF
|
| 381 |
+
doc.build(content)
|
| 382 |
+
|
| 383 |
+
# Reset buffer position to the beginning
|
| 384 |
+
buffer.seek(0)
|
| 385 |
+
return buffer
|
| 386 |
+
except Exception as e:
|
| 387 |
+
print(f"Error in create_braille_pdf_with_comparison: {str(e)}")
|
| 388 |
+
# Create a simple PDF with error message
|
| 389 |
+
simple_buffer = io.BytesIO()
|
| 390 |
+
doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
|
| 391 |
+
styles = getSampleStyleSheet()
|
| 392 |
+
content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
|
| 393 |
+
doc.build(content)
|
| 394 |
+
simple_buffer.seek(0)
|
| 395 |
+
return simple_buffer
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
def create_braille_pdf_with_comparison_working(original_text, braille_text, title="Menu in Braille"):
|
| 399 |
+
"""
|
| 400 |
+
Create a PDF file with side-by-side comparison of original text and Braille.
|
| 401 |
+
|
| 402 |
Args:
|
| 403 |
original_text: Original text content
|
| 404 |
braille_text: Braille translation
|