Spaces:
Sleeping
Sleeping
Update utils/pdf_generator.py
Browse files- utils/pdf_generator.py +144 -0
utils/pdf_generator.py
CHANGED
|
@@ -288,10 +288,154 @@ def create_braille_pdf1(original_text, braille_text, title="Menu in Braille"):
|
|
| 288 |
buffer.seek(0)
|
| 289 |
return buffer
|
| 290 |
|
|
|
|
| 291 |
def create_braille_pdf_with_comparison(original_text, braille_text, title="Menu in Braille"):
|
| 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
|
|
|
|
| 288 |
buffer.seek(0)
|
| 289 |
return buffer
|
| 290 |
|
| 291 |
+
|
| 292 |
def create_braille_pdf_with_comparison(original_text, braille_text, title="Menu in Braille"):
|
| 293 |
"""
|
| 294 |
Create a PDF file with side-by-side comparison of original text and Braille.
|
| 295 |
|
| 296 |
+
Args:
|
| 297 |
+
original_text: Original text content
|
| 298 |
+
braille_text: Braille translation
|
| 299 |
+
title: PDF title
|
| 300 |
+
|
| 301 |
+
Returns:
|
| 302 |
+
BytesIO object containing the PDF
|
| 303 |
+
"""
|
| 304 |
+
try:
|
| 305 |
+
# Create a BytesIO object to store the PDF
|
| 306 |
+
buffer = io.BytesIO()
|
| 307 |
+
|
| 308 |
+
# Register a Unicode font that supports Braille
|
| 309 |
+
try:
|
| 310 |
+
pdfmetrics.registerFont(TTFont('DejaVu', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
|
| 311 |
+
braille_font = 'DejaVu'
|
| 312 |
+
except:
|
| 313 |
+
# Fallback to default font if DejaVu is not available
|
| 314 |
+
braille_font = 'Helvetica'
|
| 315 |
+
|
| 316 |
+
# Create the PDF document
|
| 317 |
+
doc = SimpleDocTemplate(
|
| 318 |
+
buffer,
|
| 319 |
+
pagesize=letter,
|
| 320 |
+
rightMargin=72,
|
| 321 |
+
leftMargin=72,
|
| 322 |
+
topMargin=72,
|
| 323 |
+
bottomMargin=72
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
# Define styles
|
| 327 |
+
styles = getSampleStyleSheet()
|
| 328 |
+
title_style = styles['Title']
|
| 329 |
+
heading_style = styles['Heading2']
|
| 330 |
+
normal_style = ParagraphStyle(
|
| 331 |
+
'Normal',
|
| 332 |
+
fontName='Helvetica',
|
| 333 |
+
fontSize=10,
|
| 334 |
+
leading=12,
|
| 335 |
+
wordWrap='CJK'
|
| 336 |
+
)
|
| 337 |
+
braille_style = ParagraphStyle(
|
| 338 |
+
'Braille',
|
| 339 |
+
fontName=braille_font,
|
| 340 |
+
fontSize=10,
|
| 341 |
+
leading=12,
|
| 342 |
+
wordWrap='CJK'
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
# Create the content
|
| 346 |
+
content = []
|
| 347 |
+
|
| 348 |
+
# Add title
|
| 349 |
+
content.append(Paragraph(title, title_style))
|
| 350 |
+
content.append(Spacer(1, 12))
|
| 351 |
+
|
| 352 |
+
# Split text into paragraphs
|
| 353 |
+
orig_paragraphs = original_text.split('\n\n')
|
| 354 |
+
braille_paragraphs = braille_text.split('\n\n')
|
| 355 |
+
|
| 356 |
+
# Ensure both lists have the same length
|
| 357 |
+
max_paragraphs = max(len(orig_paragraphs), len(braille_paragraphs))
|
| 358 |
+
orig_paragraphs = orig_paragraphs + [''] * (max_paragraphs - len(orig_paragraphs))
|
| 359 |
+
braille_paragraphs = braille_paragraphs + [''] * (max_paragraphs - len(braille_paragraphs))
|
| 360 |
+
|
| 361 |
+
# Process each paragraph separately
|
| 362 |
+
for i in range(max_paragraphs):
|
| 363 |
+
# Create section header for each paragraph
|
| 364 |
+
if i > 0:
|
| 365 |
+
content.append(Spacer(1, 20))
|
| 366 |
+
|
| 367 |
+
content.append(Paragraph(f"Section {i+1}", heading_style))
|
| 368 |
+
content.append(Spacer(1, 8))
|
| 369 |
+
|
| 370 |
+
# Create a table for this paragraph
|
| 371 |
+
data = [["Original Text", "Braille Translation"]]
|
| 372 |
+
|
| 373 |
+
# Split paragraph into lines
|
| 374 |
+
orig_lines = orig_paragraphs[i].split('\n')
|
| 375 |
+
braille_lines = braille_paragraphs[i].split('\n')
|
| 376 |
+
|
| 377 |
+
# Ensure both line lists have the same length
|
| 378 |
+
max_lines = max(len(orig_lines), len(braille_lines))
|
| 379 |
+
orig_lines = orig_lines + [''] * (max_lines - len(orig_lines))
|
| 380 |
+
braille_lines = braille_lines + [''] * (max_lines - len(braille_lines))
|
| 381 |
+
|
| 382 |
+
# Process each line
|
| 383 |
+
for j in range(max_lines):
|
| 384 |
+
# Escape special characters
|
| 385 |
+
orig = orig_lines[j].replace('&', '&').replace('<', '<').replace('>', '>')
|
| 386 |
+
braille = braille_lines[j].replace('&', '&').replace('<', '<').replace('>', '>')
|
| 387 |
+
|
| 388 |
+
# Create paragraphs with proper wrapping
|
| 389 |
+
orig_para = Paragraph(orig, normal_style)
|
| 390 |
+
braille_para = Paragraph(braille, braille_style)
|
| 391 |
+
|
| 392 |
+
data.append([orig_para, braille_para])
|
| 393 |
+
|
| 394 |
+
# Create a table with fixed width columns and automatic row heights
|
| 395 |
+
available_width = doc.width - 24 # Account for margins
|
| 396 |
+
col_width = available_width / 2
|
| 397 |
+
|
| 398 |
+
table = Table(data, colWidths=[col_width, col_width], repeatRows=1)
|
| 399 |
+
|
| 400 |
+
# Style the table
|
| 401 |
+
table.setStyle(TableStyle([
|
| 402 |
+
('BACKGROUND', (0, 0), (1, 0), colors.lightgrey),
|
| 403 |
+
('TEXTCOLOR', (0, 0), (1, 0), colors.black),
|
| 404 |
+
('ALIGN', (0, 0), (1, 0), 'CENTER'),
|
| 405 |
+
('FONTNAME', (0, 0), (1, 0), 'Helvetica-Bold'),
|
| 406 |
+
('FONTSIZE', (0, 0), (1, 0), 12),
|
| 407 |
+
('BOTTOMPADDING', (0, 0), (1, 0), 8),
|
| 408 |
+
('BACKGROUND', (0, 1), (-1, -1), colors.white),
|
| 409 |
+
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
| 410 |
+
('BOX', (0, 0), (-1, -1), 1, colors.black),
|
| 411 |
+
('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
|
| 412 |
+
('ROWBACKGROUNDS', (0, 1), (-1, -1), [colors.white, colors.whitesmoke])
|
| 413 |
+
]))
|
| 414 |
+
|
| 415 |
+
content.append(table)
|
| 416 |
+
|
| 417 |
+
# Build the PDF
|
| 418 |
+
doc.build(content)
|
| 419 |
+
|
| 420 |
+
# Reset buffer position to the beginning
|
| 421 |
+
buffer.seek(0)
|
| 422 |
+
return buffer
|
| 423 |
+
except Exception as e:
|
| 424 |
+
print(f"Error in create_braille_pdf_with_comparison: {str(e)}")
|
| 425 |
+
# Create a simple PDF with error message
|
| 426 |
+
simple_buffer = io.BytesIO()
|
| 427 |
+
doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
|
| 428 |
+
styles = getSampleStyleSheet()
|
| 429 |
+
content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
|
| 430 |
+
doc.build(content)
|
| 431 |
+
simple_buffer.seek(0)
|
| 432 |
+
return simple_buffer
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
def create_braille_pdf_with_comparison_single_line(original_text, braille_text, title="Menu in Braille"):
|
| 436 |
+
"""
|
| 437 |
+
Create a PDF file with side-by-side comparison of original text and Braille.
|
| 438 |
+
|
| 439 |
Args:
|
| 440 |
original_text: Original text content
|
| 441 |
braille_text: Braille translation
|