Spaces:
Runtime error
Runtime error
| import os | |
| import tempfile | |
| from reportlab.lib.pagesizes import letter | |
| from reportlab.lib import colors | |
| from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | |
| from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle | |
| from reportlab.pdfbase import pdfmetrics | |
| from reportlab.pdfbase.ttfonts import TTFont | |
| import io | |
| # Try to register a font that supports Braille Unicode characters | |
| try: | |
| # Check for common Braille fonts | |
| font_paths = [ | |
| "DejaVuSans.ttf", # Common on Linux | |
| "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", | |
| "/System/Library/Fonts/Arial Unicode.ttf", # Mac | |
| "C:\\Windows\\Fonts\\arial.ttf" # Windows | |
| ] | |
| font_registered = False | |
| for font_path in font_paths: | |
| if os.path.exists(font_path): | |
| pdfmetrics.registerFont(TTFont('BrailleFont', font_path)) | |
| font_registered = True | |
| break | |
| if not font_registered: | |
| # Use default font if none of the above are found | |
| print("No suitable font found for Braille. Using default font.") | |
| except Exception as e: | |
| print(f"Error registering font: {str(e)}") | |
| def create_braille_pdf(original_text, braille_text, title="Menu in Braille"): | |
| """ | |
| Create a PDF file with original text and its Braille translation. | |
| Args: | |
| original_text: Original text content | |
| braille_text: Braille translation | |
| title: PDF title | |
| Returns: | |
| BytesIO object containing the PDF | |
| """ | |
| # Create a BytesIO object to store the PDF | |
| buffer = io.BytesIO() | |
| # Create the PDF document | |
| doc = SimpleDocTemplate( | |
| buffer, | |
| pagesize=letter, | |
| rightMargin=72, | |
| leftMargin=72, | |
| topMargin=72, | |
| bottomMargin=72 | |
| ) | |
| # Define styles | |
| styles = getSampleStyleSheet() | |
| title_style = styles['Title'] | |
| heading_style = styles['Heading2'] | |
| normal_style = styles['Normal'] | |
| # Create a custom style for Braille text | |
| braille_style = ParagraphStyle( | |
| 'Braille', | |
| parent=normal_style, | |
| fontName='BrailleFont' if font_registered else 'Helvetica', | |
| fontSize=14, | |
| leading=18, | |
| spaceAfter=12 | |
| ) | |
| # Create the content | |
| content = [] | |
| # Add title | |
| content.append(Paragraph(title, title_style)) | |
| content.append(Spacer(1, 12)) | |
| # Add original text section | |
| content.append(Paragraph("Original Text", heading_style)) | |
| content.append(Spacer(1, 6)) | |
| # Split original text by lines and add each as a paragraph | |
| for line in original_text.split('\n'): | |
| if line.strip(): | |
| content.append(Paragraph(line, normal_style)) | |
| else: | |
| content.append(Spacer(1, 12)) | |
| content.append(Spacer(1, 24)) | |
| # Add Braille section | |
| content.append(Paragraph("Braille Translation", heading_style)) | |
| content.append(Spacer(1, 6)) | |
| # Split Braille text by lines and add each as a paragraph | |
| for line in braille_text.split('\n'): | |
| if line.strip(): | |
| content.append(Paragraph(line, braille_style)) | |
| else: | |
| content.append(Spacer(1, 12)) | |
| # Build the PDF | |
| doc.build(content) | |
| # Reset buffer position to the beginning | |
| buffer.seek(0) | |
| return buffer | |
| def create_braille_pdf_with_comparison(original_text, braille_text, title="Menu in Braille"): | |
| """ | |
| Create a PDF file with side-by-side comparison of original text and Braille. | |
| Args: | |
| original_text: Original text content | |
| braille_text: Braille translation | |
| title: PDF title | |
| Returns: | |
| BytesIO object containing the PDF | |
| """ | |
| # Create a BytesIO object to store the PDF | |
| buffer = io.BytesIO() | |
| # Create the PDF document | |
| doc = SimpleDocTemplate( | |
| buffer, | |
| pagesize=letter, | |
| rightMargin=72, | |
| leftMargin=72, | |
| topMargin=72, | |
| bottomMargin=72 | |
| ) | |
| # Define styles | |
| styles = getSampleStyleSheet() | |
| title_style = styles['Title'] | |
| heading_style = styles['Heading2'] | |
| normal_style = styles['Normal'] | |
| # Create a custom style for Braille text | |
| braille_style = ParagraphStyle( | |
| 'Braille', | |
| parent=normal_style, | |
| fontName='BrailleFont' if font_registered else 'Helvetica', | |
| fontSize=14, | |
| leading=18 | |
| ) | |
| # Create the content | |
| content = [] | |
| # Add title | |
| content.append(Paragraph(title, title_style)) | |
| content.append(Spacer(1, 12)) | |
| # Split text into lines | |
| original_lines = original_text.split('\n') | |
| braille_lines = braille_text.split('\n') | |
| # Ensure both lists have the same length | |
| max_lines = max(len(original_lines), len(braille_lines)) | |
| original_lines = original_lines + [''] * (max_lines - len(original_lines)) | |
| braille_lines = braille_lines + [''] * (max_lines - len(braille_lines)) | |
| # Create a table for side-by-side comparison | |
| table_data = [ | |
| [Paragraph("Original Text", heading_style), Paragraph("Braille Translation", heading_style)] | |
| ] | |
| # Add each line as a row in the table | |
| for i in range(max_lines): | |
| original_para = Paragraph(original_lines[i], normal_style) if original_lines[i].strip() else Spacer(1, 12) | |
| braille_para = Paragraph(braille_lines[i], braille_style) if braille_lines[i].strip() else Spacer(1, 12) | |
| table_data.append([original_para, braille_para]) | |
| # Create the table | |
| table = Table(table_data, colWidths=[doc.width/2.0-12, doc.width/2.0-12]) | |
| # Style the table | |
| table.setStyle(TableStyle([ | |
| ('VALIGN', (0, 0), (-1, -1), 'TOP'), | |
| ('GRID', (0, 0), (-1, 0), 1, colors.black), | |
| ('BOX', (0, 0), (-1, -1), 1, colors.black), | |
| ('BACKGROUND', (0, 0), (1, 0), colors.lightgrey) | |
| ])) | |
| content.append(table) | |
| # Build the PDF | |
| doc.build(content) | |
| # Reset buffer position to the beginning | |
| buffer.seek(0) | |
| return buffer | |