Spaces:
Sleeping
Sleeping
Update utils/pdf_generator.py
Browse files- utils/pdf_generator.py +116 -0
utils/pdf_generator.py
CHANGED
@@ -292,6 +292,122 @@ def create_braille_pdf_with_comparison(original_text, braille_text, title="Menu
|
|
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
|
|
|
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 |
+
# Process text line by line
|
343 |
+
orig_lines = original_text.split('\n')
|
344 |
+
braille_lines = braille_text.split('\n')
|
345 |
+
|
346 |
+
# Make sure both lists have the same length
|
347 |
+
max_len = max(len(orig_lines), len(braille_lines))
|
348 |
+
orig_lines = orig_lines + [''] * (max_len - len(orig_lines))
|
349 |
+
braille_lines = braille_lines + [''] * (max_len - len(braille_lines))
|
350 |
+
|
351 |
+
# Break content into smaller chunks (10 lines per table)
|
352 |
+
chunk_size = 10
|
353 |
+
for chunk_start in range(0, max_len, chunk_size):
|
354 |
+
chunk_end = min(chunk_start + chunk_size, max_len)
|
355 |
+
|
356 |
+
# Create a table for this chunk
|
357 |
+
table_data = [["Original Text", "Braille Translation"]]
|
358 |
+
|
359 |
+
for i in range(chunk_start, chunk_end):
|
360 |
+
orig = orig_lines[i].replace('&', '&').replace('<', '<').replace('>', '>')
|
361 |
+
braille = braille_lines[i].replace('&', '&').replace('<', '<').replace('>', '>')
|
362 |
+
|
363 |
+
# Limit the length of each cell to prevent overflow
|
364 |
+
if len(orig) > 100:
|
365 |
+
orig = orig[:97] + "..."
|
366 |
+
if len(braille) > 100:
|
367 |
+
braille = braille[:97] + "..."
|
368 |
+
|
369 |
+
table_data.append([orig, braille])
|
370 |
+
|
371 |
+
# Create the table
|
372 |
+
table = Table(table_data, colWidths=[doc.width/2-12, doc.width/2-12])
|
373 |
+
table.setStyle(TableStyle([
|
374 |
+
('FONT', (0, 0), (0, 0), 'Helvetica-Bold'),
|
375 |
+
('FONT', (1, 0), (1, 0), 'Helvetica-Bold'),
|
376 |
+
('BACKGROUND', (0, 0), (1, 0), colors.lightgrey),
|
377 |
+
('ALIGN', (0, 0), (1, 0), 'CENTER'),
|
378 |
+
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
379 |
+
('GRID', (0, 0), (1, 0), 1, colors.black),
|
380 |
+
('BOX', (0, 0), (-1, -1), 1, colors.black),
|
381 |
+
('LINEABOVE', (0, 1), (-1, -1), 1, colors.black),
|
382 |
+
('FONT', (0, 1), (0, -1), 'Helvetica'),
|
383 |
+
('FONT', (1, 1), (1, -1), 'DejaVu')
|
384 |
+
]))
|
385 |
+
|
386 |
+
content.append(table)
|
387 |
+
content.append(Spacer(1, 12))
|
388 |
+
|
389 |
+
# Build the PDF
|
390 |
+
doc.build(content)
|
391 |
+
|
392 |
+
# Reset buffer position to the beginning
|
393 |
+
buffer.seek(0)
|
394 |
+
return buffer
|
395 |
+
except Exception as e:
|
396 |
+
print(f"Error in create_braille_pdf_with_comparison: {str(e)}")
|
397 |
+
# Create a simple PDF with error message
|
398 |
+
simple_buffer = io.BytesIO()
|
399 |
+
doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
|
400 |
+
styles = getSampleStyleSheet()
|
401 |
+
content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
|
402 |
+
doc.build(content)
|
403 |
+
simple_buffer.seek(0)
|
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.
|
410 |
+
|
411 |
Args:
|
412 |
original_text: Original text content
|
413 |
braille_text: Braille translation
|