import os import subprocess import tempfile import uuid from flask import Flask, request, send_file, jsonify, render_template_string from werkzeug.utils import secure_filename from flask_cors import CORS app = Flask(__name__) CORS(app) # HTML template for simple UI HTML_TEMPLATE = ''' Word to PDF Converter

📄 Word to PDF Converter

Convert Word documents to PDF instantly for free

.DOC .DOCX .TXT .ODT
📤

Click to upload Word file

or drag and drop

Max size: 50MB

No file chosen

API Usage:

Endpoint: POST /convert

Example (curl):

curl -X POST -F "file=@document.docx" {{ url_for('convert_word_to_pdf') }} --output output.pdf
''' @app.route('/') def home(): return render_template_string(HTML_TEMPLATE) @app.route('/api/help') def api_help(): return jsonify({ "endpoints": { "GET /": "Web interface", "POST /convert": "Convert Word to PDF (returns PDF file)", "GET /api/help": "This help message", "GET /api/health": "Health check" }, "supported_formats": [".doc", ".docx", ".txt", ".odt"], "max_file_size": "50MB", "example_curl": "curl -X POST -F 'file=@document.docx' https://shuvo108-convertfilling.hf.space/convert --output output.pdf" }) @app.route('/api/health') def health_check(): return jsonify({ "status": "healthy", "service": "word-to-pdf-converter", "version": "1.0.0" }) @app.route('/convert', methods=['POST']) def convert_word_to_pdf(): if 'file' not in request.files: return 'No file uploaded', 400 file = request.files['file'] if file.filename == '': return 'No file selected', 400 # Check file extension allowed_ext = ['.doc', '.docx', '.txt', '.odt'] if not any(file.filename.lower().endswith(ext) for ext in allowed_ext): return 'File type not supported. Use: .doc, .docx, .txt, .odt', 400 # Check file size (50MB limit) file.seek(0, 2) # Seek to end file_size = file.tell() file.seek(0) # Reset to beginning if file_size > 50 * 1024 * 1024: return 'File too large. Max 50MB', 400 with tempfile.TemporaryDirectory() as tmpdir: # Save uploaded file input_path = os.path.join(tmpdir, secure_filename(file.filename)) file.save(input_path) # Generate output filename base_name = os.path.splitext(file.filename)[0] pdf_filename = f"{base_name}_converted.pdf" output_path = os.path.join(tmpdir, pdf_filename) try: # Try LibreOffice first libreoffice_paths = ['/usr/bin/libreoffice', '/usr/local/bin/libreoffice'] for libreoffice in libreoffice_paths: if os.path.exists(libreoffice): cmd = [ libreoffice, '--headless', '--convert-to', 'pdf', '--outdir', tmpdir, input_path ] result = subprocess.run(cmd, capture_output=True, text=True, timeout=30) if result.returncode == 0: # Find the generated PDF for f in os.listdir(tmpdir): if f.endswith('.pdf'): generated_pdf = os.path.join(tmpdir, f) os.rename(generated_pdf, output_path) break if os.path.exists(output_path): return send_file( output_path, as_attachment=True, download_name=pdf_filename, mimetype='application/pdf' ) # Fallback: if LibreOffice failed return 'Conversion failed. Please try a different file.', 500 except subprocess.TimeoutExpired: return 'Conversion timeout. File might be too large or complex.', 500 except Exception as e: return f'Error: {str(e)}', 500 if __name__ == '__main__': port = int(os.environ.get('PORT', 7860)) app.run(host='0.0.0.0', port=port, debug=False)