latest
This commit is contained in:
@@ -39,28 +39,41 @@ class DocumentProcessor:
|
||||
|
||||
# Use LibreOffice for conversion
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
output_file = Path(temp_dir) / f"{input_file.stem}.pdf"
|
||||
|
||||
# Convert using LibreOffice
|
||||
cmd = [
|
||||
'libreoffice',
|
||||
'--headless',
|
||||
'--convert-to', 'pdf',
|
||||
'--outdir', str(temp_dir),
|
||||
str(input_file)
|
||||
]
|
||||
|
||||
try:
|
||||
subprocess.run(cmd, check=True, capture_output=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise RuntimeError(f"Conversion failed: {e.stderr.decode()}")
|
||||
with tempfile.TemporaryDirectory() as profile_dir:
|
||||
output_file = Path(temp_dir) / f"{input_file.stem}.pdf"
|
||||
|
||||
# Convert using LibreOffice with explicit profile directory
|
||||
cmd = [
|
||||
'/Applications/LibreOffice.app/Contents/MacOS/soffice',
|
||||
'--headless',
|
||||
'--invisible',
|
||||
'--nodefault',
|
||||
'--nolockcheck',
|
||||
'--nologo',
|
||||
'--norestore',
|
||||
f'-env:UserInstallation=file://{profile_dir}',
|
||||
'--convert-to', 'pdf',
|
||||
'--outdir', str(temp_dir),
|
||||
str(input_file)
|
||||
]
|
||||
|
||||
try:
|
||||
result = subprocess.run(cmd, check=True, capture_output=True, timeout=120)
|
||||
except subprocess.CalledProcessError as e:
|
||||
stderr_msg = e.stderr.decode() if e.stderr else "No stderr output"
|
||||
stdout_msg = e.stdout.decode() if e.stdout else "No stdout output"
|
||||
raise RuntimeError(f"Conversion failed: {stderr_msg}. Stdout: {stdout_msg}")
|
||||
except subprocess.TimeoutExpired:
|
||||
raise RuntimeError("Conversion failed: Process timed out after 120 seconds")
|
||||
|
||||
if not output_file.exists():
|
||||
raise RuntimeError("Conversion failed: Output file not created")
|
||||
if not output_file.exists():
|
||||
# List all files in temp_dir for debugging
|
||||
files_in_dir = list(Path(temp_dir).glob('*'))
|
||||
raise RuntimeError(f"Conversion failed: Output file not created. Files in output dir: {files_in_dir}")
|
||||
|
||||
# Read and return the PDF content
|
||||
with open(output_file, 'rb') as f:
|
||||
return f.read()
|
||||
# Read and return the PDF content
|
||||
with open(output_file, 'rb') as f:
|
||||
return f.read()
|
||||
|
||||
def batch_convert_directory(self, directory: str) -> List[Dict]:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user