You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
from flask import Flask, request, send_from_directory, abort import os from pdf_to_word import pdf_to_word import logging
# Configure logging logging.basicConfig(level=logging.DEBUG, filename='/app/logs/app.log', filemode='w', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
app = Flask(__name__) UPLOAD_FOLDER = '/app/uploads' OUTPUT_FOLDER = '/app/outputs'
if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) if not os.path.exists(OUTPUT_FOLDER): os.makedirs(OUTPUT_FOLDER)
@app.route('/upload-pdf', methods=['POST']) def upload_pdf(): file = request.files['file'] if file and file.filename.endswith('.pdf'): pdf_path = os.path.join(UPLOAD_FOLDER, file.filename) file.save(pdf_path) logging.info(f'File uploaded and saved to {pdf_path}') output_path = pdf_to_word(pdf_path, OUTPUT_FOLDER, lang='fas+eng') if output_path: logging.info(f'Sending file {output_path}') return send_from_directory(OUTPUT_FOLDER, os.path.basename(output_path), as_attachment=True) else: logging.error('Conversion failed.') abort(500, 'Conversion failed.') else: logging.warning('Invalid file type or no file uploaded.') abort(400, 'Invalid file type or no file uploaded.')
if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)
|