Spaces:
Sleeping
Sleeping
File size: 6,032 Bytes
93c4f75 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
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
|