import fitz # PyMuPDF
import html
import zlib
import base64
import re
import requests
from pathlib import Path
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import HTMLResponse, FileResponse
import tempfile
import os
app = FastAPI()
def get_color(color_int):
if color_int is None: return "#000"
r, g, b = (color_int >> 16) & 0xFF, (color_int >> 8) & 0xFF, color_int & 0xFF
if r % 17 == 0 and g % 17 == 0 and b % 17 == 0:
return f"#{r//17:x}{g//17:x}{b//17:x}"
return f"#{r:02x}{g:02x}{b:02x}"
def perform_conversion(pdf_path):
"""Logique de conversion originale conservée à 100%"""
doc = fitz.open(pdf_path)
raw = '
'
for page in doc:
w, h = int(page.rect.width), int(page.rect.height)
raw += f'
'
raw += '
'
raw_min = re.sub(r'\s+', ' ', raw).replace('> <', '><').replace(': ', ':').replace('; ', ';')
z_data = base64.b64encode(zlib.compress(raw_min.encode('utf-8'), level=9)).decode('utf-8')
doc.close()
return f''
@app.post("/convert-file")
async def convert_file(file: UploadFile = File(...)):
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
tmp.write(await file.read())
tmp_path = tmp.name
try:
result = perform_conversion(tmp_path)
os.unlink(tmp_path)
return HTMLResponse(content=result)
except Exception as e:
return {"error": str(e)}
@app.get("/convert-url")
async def convert_url(url: str):
try:
response = requests.get(url)
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
tmp.write(response.content)
tmp_path = tmp.name
result = perform_conversion(tmp_path)
os.unlink(tmp_path)
return HTMLResponse(content=result)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)