Spaces:
Sleeping
Sleeping
Commit ·
8e7152e
0
Parent(s):
v2.0: Add all PDF operations - PDF to Word, Image OCR, PDF Split/Merge
Browse files- Dockerfile +44 -0
- README.md +246 -0
- app.py +433 -0
- requirements.txt +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hugging Face Spaces Dockerfile for Document Conversion
|
| 2 |
+
# This installs LibreOffice for Word to PDF conversion
|
| 3 |
+
|
| 4 |
+
FROM python:3.10-slim
|
| 5 |
+
|
| 6 |
+
# Install LibreOffice, Tesseract OCR, and required system dependencies
|
| 7 |
+
RUN apt-get update && apt-get install -y \
|
| 8 |
+
libreoffice \
|
| 9 |
+
libreoffice-writer \
|
| 10 |
+
libreoffice-calc \
|
| 11 |
+
libreoffice-impress \
|
| 12 |
+
tesseract-ocr \
|
| 13 |
+
tesseract-ocr-eng \
|
| 14 |
+
default-jre-headless \
|
| 15 |
+
libgl1-mesa-glx \
|
| 16 |
+
&& apt-get clean \
|
| 17 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 18 |
+
|
| 19 |
+
# Set working directory
|
| 20 |
+
WORKDIR /app
|
| 21 |
+
|
| 22 |
+
# Copy requirements and install Python dependencies
|
| 23 |
+
COPY requirements.txt .
|
| 24 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 25 |
+
|
| 26 |
+
# Copy application code
|
| 27 |
+
COPY app.py .
|
| 28 |
+
|
| 29 |
+
# Create temp directory for conversions
|
| 30 |
+
RUN mkdir -p /tmp/conversions
|
| 31 |
+
|
| 32 |
+
# Expose port 7860 (Hugging Face Spaces default)
|
| 33 |
+
EXPOSE 7860
|
| 34 |
+
|
| 35 |
+
# Set environment variables
|
| 36 |
+
ENV PYTHONUNBUFFERED=1
|
| 37 |
+
ENV PORT=7860
|
| 38 |
+
|
| 39 |
+
# Health check
|
| 40 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
| 41 |
+
CMD python -c "import requests; requests.get('http://localhost:7860/health')"
|
| 42 |
+
|
| 43 |
+
# Run the application
|
| 44 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "2", "--timeout", "60", "app:app"]
|
README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
| 11 |
+
|
| 12 |
+
# 📄 Document Conversion API - Word to PDF
|
| 13 |
+
|
| 14 |
+
Free, self-hosted document conversion service using LibreOffice. Deploy on Hugging Face Spaces for unlimited FREE usage!
|
| 15 |
+
|
| 16 |
+
## ✨ Features
|
| 17 |
+
|
| 18 |
+
- **100% FREE** - No API keys, no limits, no credit card
|
| 19 |
+
- **High Quality** - Uses LibreOffice for professional PDF conversion
|
| 20 |
+
- **Fast** - Converts documents in seconds
|
| 21 |
+
- **Self-Hosted** - Complete control and privacy
|
| 22 |
+
- **Multiple Formats** - Supports DOCX, DOC, ODT, RTF, TXT → PDF
|
| 23 |
+
|
| 24 |
+
## 🚀 Quick Deploy to Hugging Face Spaces
|
| 25 |
+
|
| 26 |
+
### Step 1: Create a New Space
|
| 27 |
+
|
| 28 |
+
1. Go to [Hugging Face Spaces](https://huggingface.co/spaces)
|
| 29 |
+
2. Click **"Create new Space"**
|
| 30 |
+
3. Fill in:
|
| 31 |
+
- **Space name**: `nextools-doc-converter` (or your choice)
|
| 32 |
+
- **License**: Apache 2.0
|
| 33 |
+
- **Select the SDK**: **Docker**
|
| 34 |
+
- **Space hardware**: CPU basic (FREE)
|
| 35 |
+
- **Visibility**: Public
|
| 36 |
+
|
| 37 |
+
### Step 2: Upload Files
|
| 38 |
+
|
| 39 |
+
Upload these 3 files to your Space:
|
| 40 |
+
|
| 41 |
+
1. `Dockerfile`
|
| 42 |
+
2. `app.py`
|
| 43 |
+
3. `requirements.txt`
|
| 44 |
+
|
| 45 |
+
### Step 3: Wait for Build
|
| 46 |
+
|
| 47 |
+
- Hugging Face will automatically build your Docker container
|
| 48 |
+
- Takes about 5-10 minutes (first time only)
|
| 49 |
+
- Watch the logs for "Application startup complete"
|
| 50 |
+
|
| 51 |
+
### Step 4: Get Your API URL
|
| 52 |
+
|
| 53 |
+
Your API will be available at:
|
| 54 |
+
```
|
| 55 |
+
https://YOUR-USERNAME-nextools-doc-converter.hf.space
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
### Step 5: Add to Your Vercel .env.local
|
| 59 |
+
|
| 60 |
+
```bash
|
| 61 |
+
# Document Conversion API
|
| 62 |
+
DOC_CONVERSION_API_URL=https://YOUR-USERNAME-nextools-doc-converter.hf.space
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
## 📡 API Usage
|
| 66 |
+
|
| 67 |
+
### Convert Document to PDF
|
| 68 |
+
|
| 69 |
+
**Endpoint:** `POST /convert`
|
| 70 |
+
|
| 71 |
+
**cURL Example:**
|
| 72 |
+
```bash
|
| 73 |
+
curl -X POST \
|
| 74 |
+
https://YOUR-USERNAME-nextools-doc-converter.hf.space/convert \
|
| 75 |
+
-F "file=@document.docx" \
|
| 76 |
+
--output converted.pdf
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
**JavaScript Example:**
|
| 80 |
+
```javascript
|
| 81 |
+
const formData = new FormData();
|
| 82 |
+
formData.append('file', file);
|
| 83 |
+
|
| 84 |
+
const response = await fetch('https://YOUR-API-URL/convert', {
|
| 85 |
+
method: 'POST',
|
| 86 |
+
body: formData
|
| 87 |
+
});
|
| 88 |
+
|
| 89 |
+
const pdfBlob = await response.blob();
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
### Health Check
|
| 93 |
+
|
| 94 |
+
**Endpoint:** `GET /health`
|
| 95 |
+
|
| 96 |
+
```bash
|
| 97 |
+
curl https://YOUR-API-URL/health
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
**Response:**
|
| 101 |
+
```json
|
| 102 |
+
{
|
| 103 |
+
"status": "healthy",
|
| 104 |
+
"libreoffice": true,
|
| 105 |
+
"message": "Service is running"
|
| 106 |
+
}
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
## 🔧 Test Locally (Optional)
|
| 110 |
+
|
| 111 |
+
### Using Docker:
|
| 112 |
+
```bash
|
| 113 |
+
# Build
|
| 114 |
+
docker build -t doc-converter .
|
| 115 |
+
|
| 116 |
+
# Run
|
| 117 |
+
docker run -p 7860:7860 doc-converter
|
| 118 |
+
|
| 119 |
+
# Test
|
| 120 |
+
curl -X POST http://localhost:7860/convert \
|
| 121 |
+
-F "file=@test.docx" \
|
| 122 |
+
--output converted.pdf
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
### Using Python (requires LibreOffice installed):
|
| 126 |
+
```bash
|
| 127 |
+
# Install LibreOffice first:
|
| 128 |
+
# Ubuntu/Debian: sudo apt install libreoffice
|
| 129 |
+
# Mac: brew install libreoffice
|
| 130 |
+
# Windows: Download from libreoffice.org
|
| 131 |
+
|
| 132 |
+
# Install dependencies
|
| 133 |
+
pip install -r requirements.txt
|
| 134 |
+
|
| 135 |
+
# Run
|
| 136 |
+
python app.py
|
| 137 |
+
|
| 138 |
+
# Test
|
| 139 |
+
curl -X POST http://localhost:7860/convert \
|
| 140 |
+
-F "file=@test.docx" \
|
| 141 |
+
--output converted.pdf
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
## 📊 Supported Formats
|
| 145 |
+
|
| 146 |
+
### Input Formats:
|
| 147 |
+
- `.docx` - Microsoft Word (2007+)
|
| 148 |
+
- `.doc` - Microsoft Word (97-2003)
|
| 149 |
+
- `.odt` - OpenDocument Text
|
| 150 |
+
- `.rtf` - Rich Text Format
|
| 151 |
+
- `.txt` - Plain Text
|
| 152 |
+
|
| 153 |
+
### Output Format:
|
| 154 |
+
- `.pdf` - PDF (Portable Document Format)
|
| 155 |
+
|
| 156 |
+
## 🎯 Why Hugging Face Spaces?
|
| 157 |
+
|
| 158 |
+
1. **FREE Forever** - No billing, no credit card
|
| 159 |
+
2. **No Rate Limits** - Unlimited conversions
|
| 160 |
+
3. **Always Online** - 99.9% uptime
|
| 161 |
+
4. **Fast** - Global CDN delivery
|
| 162 |
+
5. **Easy Deploy** - Just upload files
|
| 163 |
+
6. **Auto-Scaling** - Handles traffic spikes
|
| 164 |
+
|
| 165 |
+
## 🔒 Security & Privacy
|
| 166 |
+
|
| 167 |
+
- Files are processed in memory
|
| 168 |
+
- Automatic cleanup after conversion
|
| 169 |
+
- No data is stored or logged
|
| 170 |
+
- CORS enabled for your domains
|
| 171 |
+
- SSL/HTTPS encryption
|
| 172 |
+
|
| 173 |
+
## 🐛 Troubleshooting
|
| 174 |
+
|
| 175 |
+
### Build Failed?
|
| 176 |
+
- Check Dockerfile syntax
|
| 177 |
+
- Ensure all files are uploaded
|
| 178 |
+
- Wait for LibreOffice installation to complete
|
| 179 |
+
|
| 180 |
+
### Conversion Failed?
|
| 181 |
+
- Check file format is supported
|
| 182 |
+
- Verify file is not corrupted
|
| 183 |
+
- Check logs in Hugging Face dashboard
|
| 184 |
+
|
| 185 |
+
### Timeout?
|
| 186 |
+
- Large files (>10MB) may take longer
|
| 187 |
+
- Consider increasing timeout in Dockerfile
|
| 188 |
+
- Split large documents
|
| 189 |
+
|
| 190 |
+
## 📝 Notes
|
| 191 |
+
|
| 192 |
+
- **First conversion** may take 5-10 seconds (LibreOffice startup)
|
| 193 |
+
- **Subsequent conversions** are much faster (~1-2 seconds)
|
| 194 |
+
- **Maximum file size**: 50MB (configurable)
|
| 195 |
+
- **Concurrent requests**: Supported with workers
|
| 196 |
+
|
| 197 |
+
## 🔗 Integration with NexTools
|
| 198 |
+
|
| 199 |
+
Update your `app/api/pdf-convert/route.ts`:
|
| 200 |
+
|
| 201 |
+
```typescript
|
| 202 |
+
// Use Hugging Face API for Word to PDF
|
| 203 |
+
async function wordToPdf(fileBuffer: Buffer) {
|
| 204 |
+
const apiUrl = process.env.DOC_CONVERSION_API_URL;
|
| 205 |
+
|
| 206 |
+
if (!apiUrl) {
|
| 207 |
+
throw new Error('DOC_CONVERSION_API_URL not configured');
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
const formData = new FormData();
|
| 211 |
+
formData.append('file', new Blob([fileBuffer]), 'document.docx');
|
| 212 |
+
|
| 213 |
+
const response = await fetch(`${apiUrl}/convert`, {
|
| 214 |
+
method: 'POST',
|
| 215 |
+
body: formData,
|
| 216 |
+
});
|
| 217 |
+
|
| 218 |
+
if (!response.ok) {
|
| 219 |
+
throw new Error('Conversion failed');
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
const pdfBuffer = Buffer.from(await response.arrayBuffer());
|
| 223 |
+
|
| 224 |
+
return {
|
| 225 |
+
content: pdfBuffer.toString('base64'),
|
| 226 |
+
mimeType: 'application/pdf',
|
| 227 |
+
fileName: 'converted.pdf',
|
| 228 |
+
fileType: 'PDF',
|
| 229 |
+
pages: 1, // Calculate if needed
|
| 230 |
+
};
|
| 231 |
+
}
|
| 232 |
+
```
|
| 233 |
+
|
| 234 |
+
## 📞 Support
|
| 235 |
+
|
| 236 |
+
- **Issues**: Report on GitHub
|
| 237 |
+
- **Questions**: Ask in Hugging Face discussions
|
| 238 |
+
- **Updates**: Watch this repository
|
| 239 |
+
|
| 240 |
+
## 📜 License
|
| 241 |
+
|
| 242 |
+
Apache 2.0 License - Free for commercial and personal use
|
| 243 |
+
|
| 244 |
+
---
|
| 245 |
+
|
| 246 |
+
Made with ❤️ for NexTools - Your All-in-One SaaS Platform
|
app.py
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Document Conversion API for Hugging Face Spaces
|
| 3 |
+
Handles ALL PDF operations: Word↔PDF, Image↔Text, PDF Merge/Split
|
| 4 |
+
Self-hosted, FREE forever with unlimited usage!
|
| 5 |
+
"""
|
| 6 |
+
from flask import Flask, request, send_file, jsonify
|
| 7 |
+
from flask_cors import CORS
|
| 8 |
+
import subprocess
|
| 9 |
+
import os
|
| 10 |
+
import tempfile
|
| 11 |
+
import uuid
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
import logging
|
| 14 |
+
from PyPDF2 import PdfReader, PdfWriter, PdfMerger
|
| 15 |
+
import pytesseract
|
| 16 |
+
from PIL import Image
|
| 17 |
+
from pdf2docx import Converter
|
| 18 |
+
from io import BytesIO
|
| 19 |
+
import zipfile
|
| 20 |
+
|
| 21 |
+
app = Flask(__name__)
|
| 22 |
+
CORS(app, origins=["*"]) # In production, replace * with your Vercel domain
|
| 23 |
+
|
| 24 |
+
# Configure logging
|
| 25 |
+
logging.basicConfig(level=logging.INFO)
|
| 26 |
+
logger = logging.getLogger(__name__)
|
| 27 |
+
|
| 28 |
+
# Ensure LibreOffice is available
|
| 29 |
+
def check_libreoffice():
|
| 30 |
+
"""Check if LibreOffice is installed"""
|
| 31 |
+
try:
|
| 32 |
+
result = subprocess.run(
|
| 33 |
+
['libreoffice', '--version'],
|
| 34 |
+
capture_output=True,
|
| 35 |
+
text=True,
|
| 36 |
+
timeout=5
|
| 37 |
+
)
|
| 38 |
+
logger.info(f"LibreOffice version: {result.stdout.strip()}")
|
| 39 |
+
return True
|
| 40 |
+
except Exception as e:
|
| 41 |
+
logger.error(f"LibreOffice not found: {e}")
|
| 42 |
+
return False
|
| 43 |
+
|
| 44 |
+
# Ensure Tesseract is available
|
| 45 |
+
def check_tesseract():
|
| 46 |
+
"""Check if Tesseract OCR is installed"""
|
| 47 |
+
try:
|
| 48 |
+
result = subprocess.run(
|
| 49 |
+
['tesseract', '--version'],
|
| 50 |
+
capture_output=True,
|
| 51 |
+
text=True,
|
| 52 |
+
timeout=5
|
| 53 |
+
)
|
| 54 |
+
logger.info(f"Tesseract version: {result.stdout.strip()}")
|
| 55 |
+
return True
|
| 56 |
+
except Exception as e:
|
| 57 |
+
logger.error(f"Tesseract not found: {e}")
|
| 58 |
+
return False
|
| 59 |
+
|
| 60 |
+
@app.route('/')
|
| 61 |
+
def root():
|
| 62 |
+
"""Root endpoint with API info"""
|
| 63 |
+
lo_status = "Available ✅" if check_libreoffice() else "Not Found ❌"
|
| 64 |
+
tesseract_status = "Available ✅" if check_tesseract() else "Not Found ❌"
|
| 65 |
+
|
| 66 |
+
return {
|
| 67 |
+
'name': 'Document Conversion API',
|
| 68 |
+
'version': '2.0.0',
|
| 69 |
+
'backend': {
|
| 70 |
+
'LibreOffice': lo_status,
|
| 71 |
+
'Tesseract OCR': tesseract_status,
|
| 72 |
+
'PyPDF2': 'Available ✅'
|
| 73 |
+
},
|
| 74 |
+
'platform': 'Hugging Face Spaces',
|
| 75 |
+
'features': '100% FREE forever, Unlimited usage, No API keys needed',
|
| 76 |
+
'operations': {
|
| 77 |
+
'word-to-pdf': 'Convert Word/DOCX to PDF',
|
| 78 |
+
'pdf-to-word': 'Convert PDF to Word/DOCX (OCR)',
|
| 79 |
+
'image-to-text': 'Extract text from images (OCR)',
|
| 80 |
+
'pdf-split': 'Split PDF into separate pages',
|
| 81 |
+
'pdf-merge': 'Merge multiple PDFs'
|
| 82 |
+
},
|
| 83 |
+
'endpoints': {
|
| 84 |
+
'POST /convert': 'Word to PDF conversion',
|
| 85 |
+
'POST /pdf-to-word': 'PDF to Word conversion',
|
| 86 |
+
'POST /image-to-text': 'Image OCR to text',
|
| 87 |
+
'POST /pdf-split': 'Split PDF pages',
|
| 88 |
+
'POST /pdf-merge': 'Merge multiple PDFs',
|
| 89 |
+
'GET /health': 'Health check'
|
| 90 |
+
}
|
| 91 |
+
}, 200
|
| 92 |
+
|
| 93 |
+
@app.route('/health')
|
| 94 |
+
def health_check():
|
| 95 |
+
"""Health check endpoint"""
|
| 96 |
+
lo_available = check_libreoffice()
|
| 97 |
+
tesseract_available = check_tesseract()
|
| 98 |
+
|
| 99 |
+
return {
|
| 100 |
+
'status': 'healthy' if (lo_available and tesseract_available) else 'degraded',
|
| 101 |
+
'services': {
|
| 102 |
+
'libreoffice': lo_available,
|
| 103 |
+
'tesseract': tesseract_available,
|
| 104 |
+
'pypdf2': True
|
| 105 |
+
},
|
| 106 |
+
'message': 'All services running' if (lo_available and tesseract_available) else 'Some services unavailable'
|
| 107 |
+
}, 200 if (lo_available and tesseract_available) else 503
|
| 108 |
+
|
| 109 |
+
@app.route('/convert', methods=['POST'])
|
| 110 |
+
def word_to_pdf():
|
| 111 |
+
"""Convert Word/Document to PDF using LibreOffice"""
|
| 112 |
+
|
| 113 |
+
if 'file' not in request.files:
|
| 114 |
+
return jsonify({'error': 'No file provided'}), 400
|
| 115 |
+
|
| 116 |
+
file = request.files['file']
|
| 117 |
+
|
| 118 |
+
if file.filename == '':
|
| 119 |
+
return jsonify({'error': 'Empty filename'}), 400
|
| 120 |
+
|
| 121 |
+
# Get file extension
|
| 122 |
+
file_ext = Path(file.filename).suffix.lower()
|
| 123 |
+
supported_exts = ['.docx', '.doc', '.odt', '.rtf', '.txt']
|
| 124 |
+
|
| 125 |
+
if file_ext not in supported_exts:
|
| 126 |
+
return jsonify({
|
| 127 |
+
'error': f'Unsupported file format: {file_ext}',
|
| 128 |
+
'supported': supported_exts
|
| 129 |
+
}), 400
|
| 130 |
+
|
| 131 |
+
# Create unique temporary directory
|
| 132 |
+
temp_dir = tempfile.mkdtemp()
|
| 133 |
+
unique_id = str(uuid.uuid4())
|
| 134 |
+
|
| 135 |
+
try:
|
| 136 |
+
input_filename = f"input_{unique_id}{file_ext}"
|
| 137 |
+
input_path = os.path.join(temp_dir, input_filename)
|
| 138 |
+
file.save(input_path)
|
| 139 |
+
|
| 140 |
+
logger.info(f"Converting {input_filename} to PDF...")
|
| 141 |
+
|
| 142 |
+
# Convert using LibreOffice
|
| 143 |
+
cmd = [
|
| 144 |
+
'libreoffice',
|
| 145 |
+
'--headless',
|
| 146 |
+
'--convert-to', 'pdf',
|
| 147 |
+
'--outdir', temp_dir,
|
| 148 |
+
input_path
|
| 149 |
+
]
|
| 150 |
+
|
| 151 |
+
result = subprocess.run(
|
| 152 |
+
cmd,
|
| 153 |
+
capture_output=True,
|
| 154 |
+
text=True,
|
| 155 |
+
timeout=30,
|
| 156 |
+
cwd=temp_dir
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
if result.returncode != 0:
|
| 160 |
+
logger.error(f"LibreOffice error: {result.stderr}")
|
| 161 |
+
return jsonify({
|
| 162 |
+
'error': 'Conversion failed',
|
| 163 |
+
'details': result.stderr
|
| 164 |
+
}), 500
|
| 165 |
+
|
| 166 |
+
# Find output PDF
|
| 167 |
+
output_filename = input_filename.rsplit('.', 1)[0] + '.pdf'
|
| 168 |
+
output_path = os.path.join(temp_dir, output_filename)
|
| 169 |
+
|
| 170 |
+
if not os.path.exists(output_path):
|
| 171 |
+
return jsonify({
|
| 172 |
+
'error': 'PDF file not created',
|
| 173 |
+
'details': 'LibreOffice did not produce output file'
|
| 174 |
+
}), 500
|
| 175 |
+
|
| 176 |
+
file_size = os.path.getsize(output_path)
|
| 177 |
+
logger.info(f"Conversion successful! Output: {output_filename} ({file_size} bytes)")
|
| 178 |
+
|
| 179 |
+
return send_file(
|
| 180 |
+
output_path,
|
| 181 |
+
mimetype='application/pdf',
|
| 182 |
+
as_attachment=True,
|
| 183 |
+
download_name='converted.pdf'
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
except subprocess.TimeoutExpired:
|
| 187 |
+
logger.error("Conversion timeout")
|
| 188 |
+
return jsonify({'error': 'Conversion timeout (>30s)'}), 504
|
| 189 |
+
|
| 190 |
+
except Exception as e:
|
| 191 |
+
logger.error(f"Conversion error: {str(e)}")
|
| 192 |
+
return jsonify({
|
| 193 |
+
'error': 'Conversion failed',
|
| 194 |
+
'details': str(e)
|
| 195 |
+
}), 500
|
| 196 |
+
|
| 197 |
+
finally:
|
| 198 |
+
try:
|
| 199 |
+
import shutil
|
| 200 |
+
shutil.rmtree(temp_dir)
|
| 201 |
+
except Exception as e:
|
| 202 |
+
logger.warning(f"Cleanup warning: {e}")
|
| 203 |
+
|
| 204 |
+
@app.route('/pdf-to-word', methods=['POST'])
|
| 205 |
+
def pdf_to_word():
|
| 206 |
+
"""Convert PDF to Word/DOCX using pdf2docx"""
|
| 207 |
+
|
| 208 |
+
if 'file' not in request.files:
|
| 209 |
+
return jsonify({'error': 'No file provided'}), 400
|
| 210 |
+
|
| 211 |
+
file = request.files['file']
|
| 212 |
+
|
| 213 |
+
if file.filename == '':
|
| 214 |
+
return jsonify({'error': 'Empty filename'}), 400
|
| 215 |
+
|
| 216 |
+
temp_dir = tempfile.mkdtemp()
|
| 217 |
+
|
| 218 |
+
try:
|
| 219 |
+
input_path = os.path.join(temp_dir, 'input.pdf')
|
| 220 |
+
output_path = os.path.join(temp_dir, 'output.docx')
|
| 221 |
+
|
| 222 |
+
file.save(input_path)
|
| 223 |
+
|
| 224 |
+
logger.info("Converting PDF to DOCX...")
|
| 225 |
+
|
| 226 |
+
# Convert PDF to DOCX
|
| 227 |
+
cv = Converter(input_path)
|
| 228 |
+
cv.convert(output_path)
|
| 229 |
+
cv.close()
|
| 230 |
+
|
| 231 |
+
if not os.path.exists(output_path):
|
| 232 |
+
return jsonify({'error': 'DOCX file not created'}), 500
|
| 233 |
+
|
| 234 |
+
file_size = os.path.getsize(output_path)
|
| 235 |
+
logger.info(f"PDF to Word conversion successful! ({file_size} bytes)")
|
| 236 |
+
|
| 237 |
+
return send_file(
|
| 238 |
+
output_path,
|
| 239 |
+
mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
| 240 |
+
as_attachment=True,
|
| 241 |
+
download_name='converted.docx'
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
except Exception as e:
|
| 245 |
+
logger.error(f"PDF to Word error: {str(e)}")
|
| 246 |
+
return jsonify({
|
| 247 |
+
'error': 'Conversion failed',
|
| 248 |
+
'details': str(e)
|
| 249 |
+
}), 500
|
| 250 |
+
|
| 251 |
+
finally:
|
| 252 |
+
try:
|
| 253 |
+
import shutil
|
| 254 |
+
shutil.rmtree(temp_dir)
|
| 255 |
+
except Exception as e:
|
| 256 |
+
logger.warning(f"Cleanup warning: {e}")
|
| 257 |
+
|
| 258 |
+
@app.route('/image-to-text', methods=['POST'])
|
| 259 |
+
def image_to_text():
|
| 260 |
+
"""Extract text from image using Tesseract OCR"""
|
| 261 |
+
|
| 262 |
+
if 'file' not in request.files:
|
| 263 |
+
return jsonify({'error': 'No file provided'}), 400
|
| 264 |
+
|
| 265 |
+
file = request.files['file']
|
| 266 |
+
|
| 267 |
+
if file.filename == '':
|
| 268 |
+
return jsonify({'error': 'Empty filename'}), 400
|
| 269 |
+
|
| 270 |
+
try:
|
| 271 |
+
# Read image
|
| 272 |
+
image = Image.open(file.stream)
|
| 273 |
+
|
| 274 |
+
logger.info(f"Extracting text from image ({image.size})...")
|
| 275 |
+
|
| 276 |
+
# Perform OCR
|
| 277 |
+
text = pytesseract.image_to_string(image)
|
| 278 |
+
|
| 279 |
+
logger.info(f"OCR successful! Extracted {len(text)} characters")
|
| 280 |
+
|
| 281 |
+
# Create text file
|
| 282 |
+
text_content = f"Extracted Text from {file.filename}\n\n{text.strip()}"
|
| 283 |
+
|
| 284 |
+
# Return as downloadable text file
|
| 285 |
+
buffer = BytesIO()
|
| 286 |
+
buffer.write(text_content.encode('utf-8'))
|
| 287 |
+
buffer.seek(0)
|
| 288 |
+
|
| 289 |
+
return send_file(
|
| 290 |
+
buffer,
|
| 291 |
+
mimetype='text/plain',
|
| 292 |
+
as_attachment=True,
|
| 293 |
+
download_name='extracted-text.txt'
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
except Exception as e:
|
| 297 |
+
logger.error(f"OCR error: {str(e)}")
|
| 298 |
+
return jsonify({
|
| 299 |
+
'error': 'Text extraction failed',
|
| 300 |
+
'details': str(e)
|
| 301 |
+
}), 500
|
| 302 |
+
|
| 303 |
+
@app.route('/pdf-split', methods=['POST'])
|
| 304 |
+
def pdf_split():
|
| 305 |
+
"""Split PDF into separate pages"""
|
| 306 |
+
|
| 307 |
+
if 'file' not in request.files:
|
| 308 |
+
return jsonify({'error': 'No file provided'}), 400
|
| 309 |
+
|
| 310 |
+
file = request.files['file']
|
| 311 |
+
|
| 312 |
+
if file.filename == '':
|
| 313 |
+
return jsonify({'error': 'Empty filename'}), 400
|
| 314 |
+
|
| 315 |
+
temp_dir = tempfile.mkdtemp()
|
| 316 |
+
|
| 317 |
+
try:
|
| 318 |
+
# Read PDF
|
| 319 |
+
pdf_reader = PdfReader(file.stream)
|
| 320 |
+
total_pages = len(pdf_reader.pages)
|
| 321 |
+
|
| 322 |
+
logger.info(f"Splitting PDF ({total_pages} pages)...")
|
| 323 |
+
|
| 324 |
+
# Create ZIP file for all pages
|
| 325 |
+
zip_path = os.path.join(temp_dir, 'split_pages.zip')
|
| 326 |
+
|
| 327 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 328 |
+
for page_num in range(total_pages):
|
| 329 |
+
# Create new PDF for each page
|
| 330 |
+
pdf_writer = PdfWriter()
|
| 331 |
+
pdf_writer.add_page(pdf_reader.pages[page_num])
|
| 332 |
+
|
| 333 |
+
# Write to buffer
|
| 334 |
+
page_buffer = BytesIO()
|
| 335 |
+
pdf_writer.write(page_buffer)
|
| 336 |
+
page_buffer.seek(0)
|
| 337 |
+
|
| 338 |
+
# Add to ZIP
|
| 339 |
+
zipf.writestr(f'page_{page_num + 1}.pdf', page_buffer.read())
|
| 340 |
+
|
| 341 |
+
logger.info(f"Split successful! Created {total_pages} page files")
|
| 342 |
+
|
| 343 |
+
return send_file(
|
| 344 |
+
zip_path,
|
| 345 |
+
mimetype='application/zip',
|
| 346 |
+
as_attachment=True,
|
| 347 |
+
download_name='split_pages.zip'
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
except Exception as e:
|
| 351 |
+
logger.error(f"PDF split error: {str(e)}")
|
| 352 |
+
return jsonify({
|
| 353 |
+
'error': 'PDF split failed',
|
| 354 |
+
'details': str(e)
|
| 355 |
+
}), 500
|
| 356 |
+
|
| 357 |
+
finally:
|
| 358 |
+
try:
|
| 359 |
+
import shutil
|
| 360 |
+
shutil.rmtree(temp_dir)
|
| 361 |
+
except Exception as e:
|
| 362 |
+
logger.warning(f"Cleanup warning: {e}")
|
| 363 |
+
|
| 364 |
+
@app.route('/pdf-merge', methods=['POST'])
|
| 365 |
+
def pdf_merge():
|
| 366 |
+
"""Merge multiple PDFs into one"""
|
| 367 |
+
|
| 368 |
+
if 'files' not in request.files:
|
| 369 |
+
return jsonify({'error': 'No files provided'}), 400
|
| 370 |
+
|
| 371 |
+
files = request.files.getlist('files')
|
| 372 |
+
|
| 373 |
+
if len(files) < 2:
|
| 374 |
+
return jsonify({'error': 'At least 2 PDF files required'}), 400
|
| 375 |
+
|
| 376 |
+
temp_dir = tempfile.mkdtemp()
|
| 377 |
+
|
| 378 |
+
try:
|
| 379 |
+
logger.info(f"Merging {len(files)} PDF files...")
|
| 380 |
+
|
| 381 |
+
# Merge PDFs
|
| 382 |
+
merger = PdfMerger()
|
| 383 |
+
|
| 384 |
+
for file in files:
|
| 385 |
+
if file.filename.lower().endswith('.pdf'):
|
| 386 |
+
merger.append(file.stream)
|
| 387 |
+
|
| 388 |
+
# Write merged PDF
|
| 389 |
+
output_path = os.path.join(temp_dir, 'merged.pdf')
|
| 390 |
+
merger.write(output_path)
|
| 391 |
+
merger.close()
|
| 392 |
+
|
| 393 |
+
file_size = os.path.getsize(output_path)
|
| 394 |
+
logger.info(f"Merge successful! Output: {file_size} bytes")
|
| 395 |
+
|
| 396 |
+
return send_file(
|
| 397 |
+
output_path,
|
| 398 |
+
mimetype='application/pdf',
|
| 399 |
+
as_attachment=True,
|
| 400 |
+
download_name='merged.pdf'
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
except Exception as e:
|
| 404 |
+
logger.error(f"PDF merge error: {str(e)}")
|
| 405 |
+
return jsonify({
|
| 406 |
+
'error': 'PDF merge failed',
|
| 407 |
+
'details': str(e)
|
| 408 |
+
}), 500
|
| 409 |
+
|
| 410 |
+
finally:
|
| 411 |
+
try:
|
| 412 |
+
import shutil
|
| 413 |
+
shutil.rmtree(temp_dir)
|
| 414 |
+
except Exception as e:
|
| 415 |
+
logger.warning(f"Cleanup warning: {e}")
|
| 416 |
+
|
| 417 |
+
if __name__ == '__main__':
|
| 418 |
+
logger.info("🚀 Starting Document Conversion API...")
|
| 419 |
+
|
| 420 |
+
# Check dependencies on startup
|
| 421 |
+
if check_libreoffice():
|
| 422 |
+
logger.info("✅ LibreOffice is ready!")
|
| 423 |
+
else:
|
| 424 |
+
logger.warning("⚠️ LibreOffice not found")
|
| 425 |
+
|
| 426 |
+
if check_tesseract():
|
| 427 |
+
logger.info("✅ Tesseract OCR is ready!")
|
| 428 |
+
else:
|
| 429 |
+
logger.warning("⚠️ Tesseract not found")
|
| 430 |
+
|
| 431 |
+
# Run Flask app
|
| 432 |
+
port = int(os.environ.get('PORT', 7860))
|
| 433 |
+
app.run(host='0.0.0.0', port=port, debug=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==3.0.0
|
| 2 |
+
flask-cors==4.0.0
|
| 3 |
+
Werkzeug==3.0.1
|
| 4 |
+
gunicorn==21.2.0
|
| 5 |
+
PyPDF2==3.0.1
|
| 6 |
+
pytesseract==0.3.10
|
| 7 |
+
Pillow==10.2.0
|
| 8 |
+
pdf2docx==0.5.8
|