Spaces:
Running
Running
Upload main.py
Browse files- backend/main.py +45 -0
backend/main.py
CHANGED
|
@@ -289,6 +289,51 @@ async def api_translate(file: UploadFile = File(...), lang: str = Form(...)):
|
|
| 289 |
except Exception as e:
|
| 290 |
raise HTTPException(status_code=500, detail=str(e))
|
| 291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
# Monta o frontend estático na raiz (útil para deploy unificado)
|
| 294 |
import os
|
|
|
|
| 289 |
except Exception as e:
|
| 290 |
raise HTTPException(status_code=500, detail=str(e))
|
| 291 |
|
| 292 |
+
@app.post("/api/export-pdf")
|
| 293 |
+
async def export_pdf(html_content: str = Form(...)):
|
| 294 |
+
try:
|
| 295 |
+
from xhtml2pdf import pisa
|
| 296 |
+
import io
|
| 297 |
+
|
| 298 |
+
styled_html = f"""
|
| 299 |
+
<html>
|
| 300 |
+
<head>
|
| 301 |
+
<style>
|
| 302 |
+
@page {{
|
| 303 |
+
size: A4 portrait;
|
| 304 |
+
margin: 2cm;
|
| 305 |
+
}}
|
| 306 |
+
body {{
|
| 307 |
+
font-family: Helvetica, sans-serif;
|
| 308 |
+
font-size: 14px;
|
| 309 |
+
line-height: 1.5;
|
| 310 |
+
color: #000000;
|
| 311 |
+
}}
|
| 312 |
+
</style>
|
| 313 |
+
</head>
|
| 314 |
+
<body>
|
| 315 |
+
{html_content}
|
| 316 |
+
</body>
|
| 317 |
+
</html>
|
| 318 |
+
"""
|
| 319 |
+
|
| 320 |
+
result = io.BytesIO()
|
| 321 |
+
pdf = pisa.CreatePDF(io.StringIO(styled_html), dest=result)
|
| 322 |
+
|
| 323 |
+
if pdf.err:
|
| 324 |
+
raise HTTPException(status_code=500, detail="Erro ao converter HTML para PDF no backend.")
|
| 325 |
+
|
| 326 |
+
result.seek(0)
|
| 327 |
+
|
| 328 |
+
headers = {{
|
| 329 |
+
'Content-Disposition': 'attachment; filename="Export.pdf"'
|
| 330 |
+
}}
|
| 331 |
+
return Response(content=result.read(), media_type="application/pdf", headers=headers)
|
| 332 |
+
|
| 333 |
+
except Exception as e:
|
| 334 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 335 |
+
|
| 336 |
+
|
| 337 |
|
| 338 |
# Monta o frontend estático na raiz (útil para deploy unificado)
|
| 339 |
import os
|