The Docx2PDF API is a FastAPI application designed to convert DOCX files to PDF format. It provides a simple and efficient way to handle file conversion in a web environment.
- File Conversion: Converts DOCX files to PDF.
- REST API: Easy to integrate with other applications using HTTP requests.
- Cross-Origin Resource Sharing (CORS): Configured to allow requests from any origin.
- Asynchronous Handling: Processes file uploads and conversions asynchronously for better performance.
- Python 3.6 or higher
- FastAPI
- Uvicorn for running the server
- LibreOffice installed on the server for file conversion
- Install the required Python packages:
pip install fastapi uvicorn
- Ensure LibreOffice is installed on your server.
- Start the FastAPI server:
uvicorn main:app --host 0.0.0.0 --port 8001
- Use the
/convert/
endpoint to upload a DOCX file and receive a converted PDF in response.
- URL:
/convert/
- Method: POST
- Body:
doc_file
: The DOCX file to be converted.
import requests
url = 'http://localhost:8001/convert/'
files = {'doc_file': open('yourfile.docx', 'rb')}
response = requests.post(url, files=files)
with open('output.pdf', 'wb') as f:
f.write(response.content)
- The API handles file-related errors and ensures the temporary files are cleaned up after conversion.
- The API currently only supports DOCX to PDF conversion.