pktpaulie commited on
Commit
758b961
·
verified ·
1 Parent(s): 6bbcc4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -214,22 +214,35 @@ def save_bytes_as_pdf(docx_bytes, output_path='output.pdf'):
214
 
215
  # Clean up the temporary directory
216
  os.remove(output_path)
217
-
218
  def save_docx_as_pdf(input_path, output_path='output.pdf'):
219
  if input_path.lower().endswith('.docx'):
220
  try:
221
- # Attempt to use unoconv
222
- subprocess.run(['unoconv', '-o', output_path, input_path])
223
  if not os.path.exists(output_path):
224
- raise FileNotFoundError("unoconv failed to convert DOCX to PDF")
225
- except FileNotFoundError:
226
- # Fallback to text-based display
227
- with open(output_path, 'w') as f:
228
- f.write(input_path.read())
229
  elif input_path.lower().endswith('.pdf'):
230
  shutil.copy(input_path, output_path)
231
  else:
232
  raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
  # def save_docx_as_pdf(input_path, output_path='output.pdf'):
235
  # if input_path.lower().endswith('.docx'):
 
214
 
215
  # Clean up the temporary directory
216
  os.remove(output_path)
 
217
  def save_docx_as_pdf(input_path, output_path='output.pdf'):
218
  if input_path.lower().endswith('.docx'):
219
  try:
220
+ # Convert .docx to .pdf using LibreOffice
221
+ subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', input_path, '--outdir', os.path.dirname(output_path)], check=True)
222
  if not os.path.exists(output_path):
223
+ raise FileNotFoundError("Conversion failed; output PDF not found.")
224
+ except (FileNotFoundError, subprocess.CalledProcessError):
225
+ st.error("Failed to convert DOCX to PDF. Please check LibreOffice installation.")
 
 
226
  elif input_path.lower().endswith('.pdf'):
227
  shutil.copy(input_path, output_path)
228
  else:
229
  raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
230
+
231
+ # def save_docx_as_pdf(input_path, output_path='output.pdf'):
232
+ # if input_path.lower().endswith('.docx'):
233
+ # try:
234
+ # # Attempt to use unoconv
235
+ # subprocess.run(['unoconv', '-o', output_path, input_path])
236
+ # if not os.path.exists(output_path):
237
+ # raise FileNotFoundError("unoconv failed to convert DOCX to PDF")
238
+ # except FileNotFoundError:
239
+ # # Fallback to text-based display
240
+ # with open(output_path, 'w') as f:
241
+ # f.write(input_path.read())
242
+ # elif input_path.lower().endswith('.pdf'):
243
+ # shutil.copy(input_path, output_path)
244
+ # else:
245
+ # raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
246
 
247
  # def save_docx_as_pdf(input_path, output_path='output.pdf'):
248
  # if input_path.lower().endswith('.docx'):