File size: 4,968 Bytes
595267b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Export results to PDF and DOCX formats."""

from docx import Document
from docx.shared import Pt, RGBColor
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from pathlib import Path
import uuid
from datetime import datetime


# Create directory for exports
EXPORT_DIR = Path("static/exports")
EXPORT_DIR.mkdir(parents=True, exist_ok=True)


def export_to_docx(original_text: str, results: list, result_type: str = "Paraphrased") -> str:
    """

    Export results to DOCX format.

    

    Args:

        original_text: The original input text

        results: List of results (paraphrases, translations, etc.)

        result_type: Type of result (e.g., "Paraphrased", "Translated")

    

    Returns:

        Relative path to the generated DOCX file

    """
    try:
        filename = f"{uuid.uuid4().hex}.docx"
        filepath = EXPORT_DIR / filename
        
        doc = Document()
        
        # Add title
        title = doc.add_heading(f'{result_type} Text Results', 0)
        
        # Add metadata
        doc.add_paragraph(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
        doc.add_paragraph("")
        
        # Add original text
        doc.add_heading('Original Text:', level=1)
        doc.add_paragraph(original_text)
        doc.add_paragraph("")
        
        # Add results
        doc.add_heading(f'{result_type} Versions:', level=1)
        
        if isinstance(results, list):
            for idx, result in enumerate(results, 1):
                para = doc.add_paragraph()
                run = para.add_run(f"{idx}. {result}")
                run.font.size = Pt(11)
                doc.add_paragraph("")
        else:
            para = doc.add_paragraph(str(results))
            para.style.font.size = Pt(11)
        
        doc.save(str(filepath))
        
        return f"exports/{filename}"
    
    except Exception as e:
        raise RuntimeError(f"DOCX export failed: {str(e)}")


def export_to_pdf(original_text: str, results: list, result_type: str = "Paraphrased") -> str:
    """

    Export results to PDF format.

    

    Args:

        original_text: The original input text

        results: List of results (paraphrases, translations, etc.)

        result_type: Type of result (e.g., "Paraphrased", "Translated")

    

    Returns:

        Relative path to the generated PDF file

    """
    try:
        filename = f"{uuid.uuid4().hex}.pdf"
        filepath = EXPORT_DIR / filename
        
        doc = SimpleDocTemplate(str(filepath), pagesize=letter)
        story = []
        styles = getSampleStyleSheet()
        
        # Custom styles
        title_style = ParagraphStyle(
            'CustomTitle',
            parent=styles['Heading1'],
            fontSize=18,
            textColor=RGBColor(0, 0, 128),
            spaceAfter=12
        )
        
        heading_style = ParagraphStyle(
            'CustomHeading',
            parent=styles['Heading2'],
            fontSize=14,
            textColor=RGBColor(0, 0, 0),
            spaceAfter=6
        )
        
        # Add title
        story.append(Paragraph(f"{result_type} Text Results", title_style))
        story.append(Spacer(1, 0.2*inch))
        
        # Add metadata
        story.append(Paragraph(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", styles['Normal']))
        story.append(Spacer(1, 0.3*inch))
        
        # Add original text
        story.append(Paragraph("Original Text:", heading_style))
        story.append(Paragraph(original_text, styles['Normal']))
        story.append(Spacer(1, 0.3*inch))
        
        # Add results
        story.append(Paragraph(f"{result_type} Versions:", heading_style))
        story.append(Spacer(1, 0.1*inch))
        
        if isinstance(results, list):
            for idx, result in enumerate(results, 1):
                story.append(Paragraph(f"{idx}. {result}", styles['Normal']))
                story.append(Spacer(1, 0.1*inch))
        else:
            story.append(Paragraph(str(results), styles['Normal']))
        
        doc.build(story)
        
        return f"exports/{filename}"
    
    except Exception as e:
        raise RuntimeError(f"PDF export failed: {str(e)}")


def cleanup_old_exports(max_age_hours: int = 24):
    """Remove export files older than max_age_hours."""
    import time
    current_time = time.time()
    
    for export_file in EXPORT_DIR.glob("*.*"):
        file_age = (current_time - export_file.stat().st_mtime) / 3600
        if file_age > max_age_hours:
            export_file.unlink()