BrailleMenuGenV2 / utils /pdf_generator.py
Chamin09's picture
Update utils/pdf_generator.py
c00919e verified
raw
history blame
19.3 kB
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
"""
try:
# Create a BytesIO object to store the PDF
buffer = io.BytesIO()
# Register a Unicode font that supports Braille
pdfmetrics.registerFont(TTFont('DejaVu', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
# 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 with Unicode support
braille_style = ParagraphStyle(
'Braille',
parent=normal_style,
fontName='DejaVu', # Use DejaVu font which supports Unicode Braille
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.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'), 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.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'), 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
except Exception as e:
print(f"Error in create_braille_pdf: {str(e)}")
# Create a simple PDF with error message
simple_buffer = io.BytesIO()
doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
styles = getSampleStyleSheet()
content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
doc.build(content)
simple_buffer.seek(0)
return simple_buffer
def create_braille_pdf_working(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
"""
try:
# 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='Helvetica', # Use standard font to avoid issues
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.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'), 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.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'), 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
except Exception as e:
print(f"Error in create_braille_pdf: {str(e)}")
raise
def create_braille_pdf1(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
"""
try:
# Create a BytesIO object to store the PDF
buffer = io.BytesIO()
# Register a Unicode font that supports Braille
pdfmetrics.registerFont(TTFont('DejaVu', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
# 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 with Unicode support
braille_style = ParagraphStyle(
'Braille',
parent=normal_style,
fontName='DejaVu', # Use DejaVu font which supports Unicode Braille
fontSize=14,
leading=18
)
# Create the content
content = []
# Add title
content.append(Paragraph(title, title_style))
content.append(Spacer(1, 12))
# Create a simpler table structure
table_data = [["Original Text", "Braille Translation"]]
# Process text line by line
orig_lines = original_text.split('\n')
braille_lines = braille_text.split('\n')
# Make sure both lists have the same length
max_len = max(len(orig_lines), len(braille_lines))
orig_lines = orig_lines + [''] * (max_len - len(orig_lines))
braille_lines = braille_lines + [''] * (max_len - len(braille_lines))
# Add each line pair to the table
for i in range(max_len):
orig = orig_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
braille = braille_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
# Create paragraphs with appropriate styles
orig_para = Paragraph(orig, normal_style)
braille_para = Paragraph(braille, braille_style)
table_data.append([orig_para, braille_para])
# Create the table
table = Table(table_data, colWidths=[doc.width/2-12, doc.width/2-12])
table.setStyle(TableStyle([
('FONT', (0, 0), (0, 0), 'Helvetica-Bold'),
('FONT', (1, 0), (1, 0), 'Helvetica-Bold'),
('BACKGROUND', (0, 0), (1, 0), colors.lightgrey),
('ALIGN', (0, 0), (1, 0), 'CENTER'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('GRID', (0, 0), (1, 0), 1, colors.black),
('BOX', (0, 0), (-1, -1), 1, colors.black),
('LINEABOVE', (0, 1), (-1, -1), 1, colors.black)
]))
content.append(table)
# Build the PDF
doc.build(content)
# Reset buffer position to the beginning
buffer.seek(0)
return buffer
except Exception as e:
print(f"Error in create_braille_pdf_with_comparison: {str(e)}")
# Create a simple PDF with error message
simple_buffer = io.BytesIO()
doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
styles = getSampleStyleSheet()
content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
doc.build(content)
simple_buffer.seek(0)
return simple_buffer
def create_braille_pdf_with_comparison_working(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
"""
try:
# 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 - use standard font
braille_style = ParagraphStyle(
'Braille',
parent=normal_style,
fontName='Helvetica',
fontSize=14,
leading=18
)
# Create the content
content = []
# Add title
content.append(Paragraph(title, title_style))
content.append(Spacer(1, 12))
# Create a simpler table structure
data = [["Original Text", "Braille Translation"]]
# Process text line by line
orig_lines = original_text.split('\n')
braille_lines = braille_text.split('\n')
# Make sure both lists have the same length
max_len = max(len(orig_lines), len(braille_lines))
orig_lines = orig_lines + [''] * (max_len - len(orig_lines))
braille_lines = braille_lines + [''] * (max_len - len(braille_lines))
# Add each line pair to the table
for i in range(max_len):
orig = orig_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
braille = braille_lines[i].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
data.append([orig, braille])
# Create the table
table = Table(data, colWidths=[doc.width/2-12, doc.width/2-12])
table.setStyle(TableStyle([
('FONT', (0, 0), (1, 0), 'Helvetica-Bold'),
('BACKGROUND', (0, 0), (1, 0), colors.lightgrey),
('ALIGN', (0, 0), (1, 0), 'CENTER'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('GRID', (0, 0), (1, 0), 1, colors.black),
('BOX', (0, 0), (-1, -1), 1, colors.black),
('LINEABOVE', (0, 1), (-1, -1), 1, colors.black),
('FONT', (0, 1), (0, -1), 'Helvetica'),
('FONT', (1, 1), (1, -1), 'Helvetica')
]))
content.append(table)
# Build the PDF
doc.build(content)
# Reset buffer position to the beginning
buffer.seek(0)
return buffer
except Exception as e:
print(f"Error in create_braille_pdf_with_comparison: {str(e)}")
# Return the error message in a simple PDF
simple_buffer = io.BytesIO()
doc = SimpleDocTemplate(simple_buffer, pagesize=letter)
styles = getSampleStyleSheet()
content = [Paragraph(f"Error creating PDF: {str(e)}", styles['Normal'])]
doc.build(content)
simple_buffer.seek(0)
return simple_buffer
def create_braille_pdf_with_comparison1(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