File size: 5,552 Bytes
2508004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f584ef2
2508004
f584ef2
2508004
 
f584ef2
 
 
 
 
 
 
 
 
 
 
 
2508004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import platform
from smolagents import tool

def get_libreoffice_path():
    """
    Get the correct LibreOffice path based on the operating system.
    
    Returns:
        str: Path to LibreOffice executable or None if not found
    """
    system = platform.system()
    
    if system == "Darwin":  # macOS
        # Common LibreOffice installation paths on macOS
        possible_paths = [
            "/Applications/LibreOffice.app/Contents/MacOS/soffice",
            "/Applications/LibreOffice Developer Edition.app/Contents/MacOS/soffice",
            "/opt/homebrew/bin/soffice",  # Homebrew installation
            "/usr/local/bin/soffice"
        ]
        
        for path in possible_paths:
            if os.path.exists(path):
                return path
    
    elif system == "Linux":
        # Common LibreOffice paths on Linux
        possible_paths = [
            "/usr/bin/libreoffice",
            "/usr/bin/soffice",
            "/snap/bin/libreoffice",
            "/usr/local/bin/libreoffice"
        ]
        
        for path in possible_paths:
            if os.path.exists(path):
                return path
    
    elif system == "Windows":
        # Common LibreOffice paths on Windows
        possible_paths = [
            r"C:\Program Files\LibreOffice\program\soffice.exe",
            r"C:\Program Files (x86)\LibreOffice\program\soffice.exe"
        ]
        
        for path in possible_paths:
            if os.path.exists(path):
                return path
    
    # Try to find it in PATH as fallback
    try:
        result = subprocess.run(['which', 'soffice'], capture_output=True, text=True)
        if result.returncode == 0:
            return result.stdout.strip()
    except:
        pass
    
    try:
        result = subprocess.run(['which', 'libreoffice'], capture_output=True, text=True)
        if result.returncode == 0:
            return result.stdout.strip()
    except:
        pass
    
    return None

@tool
def convert_to_pdf_with_libreoffice(input_file: str, output_dir: str = None) -> str:
    """
    Convert a document to PDF using LibreOffice.
    
    Args:
        input_file: Path to the input document
        output_dir: Directory to save the PDF (optional, defaults to same directory as input)
    
    Returns:
        str: Path to the generated PDF file or error message
    """
    libreoffice_path = get_libreoffice_path()
    
    if not libreoffice_path:
        return "LibreOffice not found. Please install LibreOffice from https://www.libreoffice.org/"
    
    if not os.path.exists(input_file):
        return f"Input file not found: {input_file}"
    
    if output_dir is None:
        output_dir = os.path.dirname(input_file)
    
    if not os.path.exists(output_dir):
        os.makedirs(output_dir, exist_ok=True)
    
    try:
        # Use LibreOffice headless mode to convert to PDF
        cmd = [
            libreoffice_path,
            '--headless',
            '--convert-to', 'pdf',
            '--outdir', output_dir,
            input_file
        ]
        
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
        
        if result.returncode == 0:
            # Generate expected output filename
            base_name = os.path.splitext(os.path.basename(input_file))[0]
            pdf_path = os.path.join(output_dir, f"{base_name}.pdf")
            
            if os.path.exists(pdf_path):
                return pdf_path
            else:
                return f"PDF conversion completed but file not found at expected location: {pdf_path}"
        else:
            return f"LibreOffice conversion failed: {result.stderr}"
            
    except subprocess.TimeoutExpired:
        return "LibreOffice conversion timed out after 60 seconds"
    except Exception as e:
        return f"Error during LibreOffice conversion: {str(e)}"

@tool
def check_libreoffice_availability() -> bool:
    """
    Check if LibreOffice is available on the system.
    
    Returns:
        bool: True if LibreOffice is available, False otherwise
    """
    libreoffice_path = get_libreoffice_path()
    return libreoffice_path is not None

@tool
def get_libreoffice_info() -> str:
    """
    Get detailed information about LibreOffice installation for troubleshooting.
    
    Returns:
        str: Detailed information about LibreOffice availability and installation
    """
    libreoffice_path = get_libreoffice_path()
    
    if not libreoffice_path:
        system = platform.system()
        install_info = {
            "Darwin": "Install with: brew install libreoffice OR download from https://www.libreoffice.org/",
            "Linux": "Install with: sudo apt install libreoffice OR sudo yum install libreoffice",
            "Windows": "Download from https://www.libreoffice.org/"
        }
        
        return f"LibreOffice not found on {system}. {install_info.get(system, 'Install from https://www.libreoffice.org/')}"
    
    try:
        # Get version info
        result = subprocess.run([libreoffice_path, '--version'], capture_output=True, text=True, timeout=10)
        version_info = result.stdout.strip() if result.returncode == 0 else "Version unknown"
        
        return f"LibreOffice found at: {libreoffice_path}\nVersion: {version_info}"
    except:
        return f"LibreOffice found at: {libreoffice_path}\nVersion: Unable to determine"

if __name__ == "__main__":
    # Test the LibreOffice detection
    print(check_libreoffice_availability())