A robust Python Flask API that converts PDF documents to HTML format using AWS Textract and advanced OCR technology.
- PDF Processing: Convert PDF documents to structured HTML
- AWS Integration: Utilizes AWS S3 for storage and Textract for OCR
- Table Detection: Advanced table extraction and HTML conversion
- Memory Optimization: Efficient processing for large documents
- CORS Support: Cross-origin requests enabled for web applications
- Error Handling: Comprehensive error management and logging
- File Cleanup: Automatic temporary file management
- RESTful API: Simple and intuitive API endpoints
Base URL: https://backend-pdf2html.onrender.com
Note: The service is currently configured with usage limits to manage AWS costs. Contact cho.yoonho023@gmail.com for access.
- Framework: Flask (Python)
- Document Processing: PyMuPDF (fitz) for PDF handling
- Cloud Services:
- AWS S3 for file storage
- AWS Textract for OCR and document analysis
- Server: Gunicorn WSGI server
- Deployment: Render.com
- Dependencies: See
requirements.txt
- Python 3.9 or higher
- AWS Account with S3 and Textract access
- AWS IAM credentials with appropriate permissions
git clone https://github.com/0GhOsTO/backend-pdf2html.git
cd backend-pdf2htmlpip install -r requirements.txtCreate a .env file or set the following environment variables:
AWS_ACCESS_KEY_ID=your_aws_access_key
AWS_SECRET_ACCESS_KEY=your_aws_secret_key
AWS_REGION=your_aws_region
BUCKET_NAME=your_s3_bucket_name- Create an S3 bucket for temporary file storage
- Configure appropriate access permissions
Your IAM user needs the following policies:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::your-bucket-name/*"
},
{
"Effect": "Allow",
"Action": [
"textract:AnalyzeDocument"
],
"Resource": "*"
}
]
}python app.pygunicorn -c gunicorn.conf.py app:appbackend-pdf2html/
βββ app.py # Main Flask application
βββ requirements.txt # Python dependencies
βββ gunicorn.conf.py # Gunicorn configuration
βββ uploads/ # Temporary file storage (auto-created)
βββ pages/ # Generated images (auto-created)
βββ README.md # This file
Converts a PDF file to HTML format.
- Method:
POST - Content-Type:
multipart/form-data - Body: PDF file with key
file - File Size Limit: 10MB maximum
- Page Limit: 10 pages maximum per PDF
{
"job_id": "uuid-string",
"html": "converted-html-content"
}{
"error": "Error description"
}cURL
curl -X POST \
https://backend-pdf2html.onrender.com/upload \
-F "file=@document.pdf"JavaScript (Fetch)
const formData = new FormData();
formData.append('file', pdfFile);
const response = await fetch('https://backend-pdf2html.onrender.com/upload', {
method: 'POST',
body: formData
});
const result = await response.json();Python (requests)
import requests
with open('document.pdf', 'rb') as f:
files = {'file': f}
response = requests.post(
'https://backend-pdf2html.onrender.com/upload',
files=files
)
result = response.json()The application includes several optimizations for memory-constrained environments:
- Page Limit: Processes maximum 10 pages per PDF
- DPI Reduction: Uses 150 DPI instead of 200 for smaller image sizes
- File Cleanup: Automatic cleanup of temporary files
- Gunicorn Settings: Optimized worker configuration
- Maximum File Size: 10MB per PDF
- Supported Format: PDF files only
- Page Limit: 10 pages per document
- Image Quality: 150 DPI for optimal performance
- File Upload: PDF file received via POST request
- Validation: Check file type, size, and page count
- PDF Conversion: Convert PDF pages to images using PyMuPDF
- S3 Upload: Upload images to AWS S3 bucket
- OCR Processing: Analyze images using AWS Textract
- Data Extraction: Extract text, tables, and document structure
- HTML Generation: Convert extracted data to structured HTML
- Cleanup: Remove temporary files and images from S3 and local storage
- Response: Return HTML content to client
# Install dependencies
pip install -r requirements.txt
# Set environment variables
export AWS_ACCESS_KEY_ID=your_key
export AWS_SECRET_ACCESS_KEY=your_secret
export AWS_REGION=us-east-1
export BUCKET_NAME=your-bucket
# Run development server
python app.py# Test with sample PDF
curl -X POST http://localhost:5000/upload \
-F "file=@sample.pdf"The API handles various error scenarios:
- Missing File: Returns 400 if no file is uploaded
- File Size: Returns 400 if file exceeds 10MB limit
- Invalid Format: Returns 400 if file is not a PDF
- AWS Errors: Returns 500 for AWS service issues
- Memory Errors: Returns 500 for memory-related issues (SIGKILL)
- Processing Errors: Returns 500 for document processing failures
- Permission Errors: Returns 500 for AWS permission issues
- File Validation: Only accepts PDF files with proper validation
- CORS Configuration: Restricted to frontend domain (
frontend-pdf2html.vercel.app) - AWS Credentials: Secure environment variable storage
- Temporary Files: Automatic cleanup to prevent storage issues
- File Size Limits: Prevents resource exhaustion attacks
- Input Sanitization: Secure filename handling
- Memory Management: Efficient image processing and cleanup
- Worker Configuration: Single worker to reduce memory usage (Render free tier)
- Request Timeouts: Extended timeouts for large file processing
- File Size Limits: Prevents excessive resource usage
- Page Limits: Reduces Textract API costs (~$50 per 1,000 pages)
- Image Cleanup: Minimizes S3 storage costs
- Processing Efficiency: Optimized API usage patterns
- Usage Monitoring: Service limits to control costs
- Average Processing Time: 10-30 seconds per PDF
- Memory Usage: ~400MB peak during processing
- Textract Costs: ~$0.05 per page analyzed
- Connect your GitHub repository to Render
- Set environment variables in Render dashboard
- Configure build and start commands:
- Build Command:
pip install -r requirements.txt - Start Command:
gunicorn -c gunicorn.conf.py app:app
- Build Command:
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_REGION=us-east-1
BUCKET_NAME=your-bucket-name
- Memory: Minimum 512MB (free tier), recommended 1GB+
- CPU: Single core sufficient for current load
- Storage: Minimal (temporary files only)
- β Added memory optimization for large files
- β Implemented automatic file cleanup
- β Enhanced error handling and logging
- β Added file size validation (10MB limit)
- β Optimized Gunicorn configuration
- β Improved AWS integration and error handling
- β Fixed SIGKILL memory issues
- β Added page limit restrictions (10 pages max)
- Async processing for large files using Celery
- Redis queue for job management and status tracking
- Additional file format support (DOCX, images)
- Enhanced table detection algorithms
- API rate limiting and authentication
- Webhook support for processing notifications
- Batch processing capabilities
- Custom HTML styling options
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow PEP 8 Python style guidelines
- Add tests for new features
- Update documentation for API changes
- Test with various PDF formats
Andrew Cho
- Email: cho.yoonho023@gmail.com
- GitHub: @0GhOsTO
- Frontend Demo: frontend-pdf2html.vercel.app
This project is licensed under the MIT License - see the LICENSE file for details.
- AWS Textract for advanced document analysis and OCR capabilities
- PyMuPDF for efficient PDF processing and image extraction
- Flask community for excellent web framework documentation
- Render.com for reliable and affordable hosting platform