Spaces:
Running
Running
Commit ·
7b384e1
0
Parent(s):
Initial commit for Hugging Face Space
Browse files- Dockerfile +33 -0
- README.md +17 -0
- app.py +58 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Use Python 3.9 as base (stable for WeasyPrint)
|
| 3 |
+
FROM python:3.9-slim
|
| 4 |
+
|
| 5 |
+
# 1. Install System Dependencies (The "Construction Team")
|
| 6 |
+
# We install Pango, Cairo, and fonts for CJK support
|
| 7 |
+
RUN apt-get update && apt-get install -y \
|
| 8 |
+
libpango-1.0-0 \
|
| 9 |
+
libpangoft2-1.0-0 \
|
| 10 |
+
libharfbuzz-subset0 \
|
| 11 |
+
libjpeg-dev \
|
| 12 |
+
libopenjp2-7-dev \
|
| 13 |
+
libxcb1 \
|
| 14 |
+
fontconfig \
|
| 15 |
+
fonts-noto-cjk \
|
| 16 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
+
|
| 18 |
+
# 2. Set working directory
|
| 19 |
+
WORKDIR /app
|
| 20 |
+
|
| 21 |
+
# 3. Install Python Dependencies
|
| 22 |
+
COPY requirements.txt .
|
| 23 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 24 |
+
|
| 25 |
+
# 4. Copy Application Code
|
| 26 |
+
COPY . .
|
| 27 |
+
|
| 28 |
+
# 5. Expose Port (Hugging Face expects port 7860)
|
| 29 |
+
EXPOSE 7860
|
| 30 |
+
|
| 31 |
+
# 6. Run the Application
|
| 32 |
+
# Using Gunicorn for production-grade performance
|
| 33 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
|
README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
---
|
| 3 |
+
title: Backend Service
|
| 4 |
+
emoji: 🚀
|
| 5 |
+
colorFrom: blue
|
| 6 |
+
colorTo: purple
|
| 7 |
+
sdk: docker
|
| 8 |
+
app_port: 7860
|
| 9 |
+
---
|
| 10 |
+
# Universal Backend Service for Chrome Extensions
|
| 11 |
+
|
| 12 |
+
This generic backend service provides:
|
| 13 |
+
- **PDF Generation**: High-fidelity HTML-to-PDF conversion using WeasyPrint (with CJK font support).
|
| 14 |
+
- **Extensible**: Ready to add more Python-based utilities.
|
| 15 |
+
|
| 16 |
+
## Deployment
|
| 17 |
+
Deployed via Docker on Hugging Face Spaces.
|
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from flask import Flask, request, send_file
|
| 3 |
+
from weasyprint import HTML, CSS
|
| 4 |
+
from weasyprint.text.fonts import FontConfiguration
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Basic Health Check
|
| 10 |
+
@app.route('/')
|
| 11 |
+
def home():
|
| 12 |
+
return "Backend Service Running"
|
| 13 |
+
|
| 14 |
+
@app.route('/api/generate_pdf', methods=['POST'])
|
| 15 |
+
def generate_pdf():
|
| 16 |
+
try:
|
| 17 |
+
data = request.json
|
| 18 |
+
if not data or 'html' not in data:
|
| 19 |
+
return {"error": "Missing 'html' field"}, 400
|
| 20 |
+
|
| 21 |
+
html_content = data['html']
|
| 22 |
+
|
| 23 |
+
# Prepare buffer
|
| 24 |
+
pdf_buffer = io.BytesIO()
|
| 25 |
+
|
| 26 |
+
# Font Config (We rely on system fonts installed via Dockerfile)
|
| 27 |
+
# fonts-noto-cjk provides "Noto Sans CJK SC"
|
| 28 |
+
font_config = FontConfiguration()
|
| 29 |
+
|
| 30 |
+
# CSS to enforce fonts
|
| 31 |
+
css = CSS(string='''
|
| 32 |
+
@page { margin: 20mm; }
|
| 33 |
+
body { font-family: "Noto Sans CJK SC", sans-serif !important; }
|
| 34 |
+
img { max-width: 100%; height: auto; }
|
| 35 |
+
pre { white-space: pre-wrap; background: #f5f5f5; padding: 10px; }
|
| 36 |
+
''')
|
| 37 |
+
|
| 38 |
+
HTML(string=html_content).write_pdf(
|
| 39 |
+
pdf_buffer,
|
| 40 |
+
stylesheets=[css],
|
| 41 |
+
font_config=font_config
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
pdf_buffer.seek(0)
|
| 45 |
+
|
| 46 |
+
return send_file(
|
| 47 |
+
pdf_buffer,
|
| 48 |
+
mimetype='application/pdf',
|
| 49 |
+
as_attachment=True,
|
| 50 |
+
download_name='export.pdf'
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error: {e}")
|
| 55 |
+
return {"error": str(e)}, 500
|
| 56 |
+
|
| 57 |
+
if __name__ == '__main__':
|
| 58 |
+
app.run(host='0.0.0.0', port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Flask==3.0.0
|
| 3 |
+
WeasyPrint==60.1
|
| 4 |
+
gunicorn==21.2.0
|