Spaces:
Sleeping
Sleeping
Create save_report_to_s3.py
Browse files
report_generation/save_report_to_s3.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import base64
|
| 3 |
+
from fastapi import APIRouter, HTTPException
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from playwright.async_api import async_playwright
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# ------------------------------
|
| 9 |
+
# FastAPI Models
|
| 10 |
+
# ------------------------------
|
| 11 |
+
|
| 12 |
+
class ReportRequest(BaseModel):
|
| 13 |
+
html_code: str # <-- input from API
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class ReportResponse(BaseModel):
|
| 17 |
+
report_generation_status: str
|
| 18 |
+
report_url: str | None
|
| 19 |
+
pdf_base64: str # <-- output base64
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# ------------------------------
|
| 23 |
+
# Router
|
| 24 |
+
# ------------------------------
|
| 25 |
+
Report_save_router = APIRouter(prefix="/report_save_pdf", tags=["report_save_pdf"])
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# ------------------------------
|
| 29 |
+
# Playwright: Convert HTML → PDF
|
| 30 |
+
# ------------------------------
|
| 31 |
+
async def html_to_pdf(html_file_path, output_pdf_path):
|
| 32 |
+
async with async_playwright() as p:
|
| 33 |
+
browser = await p.chromium.launch()
|
| 34 |
+
page = await browser.new_page()
|
| 35 |
+
|
| 36 |
+
abs_path = os.path.abspath(html_file_path)
|
| 37 |
+
await page.goto(f"file:///{abs_path}")
|
| 38 |
+
|
| 39 |
+
await page.wait_for_load_state("networkidle")
|
| 40 |
+
|
| 41 |
+
await page.pdf(
|
| 42 |
+
path=output_pdf_path,
|
| 43 |
+
format="A4",
|
| 44 |
+
print_background=True,
|
| 45 |
+
margin={"top": "1cm", "right": "1cm", "bottom": "1cm", "left": "1cm"}
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
await browser.close()
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# ------------------------------
|
| 52 |
+
# API: HTML → PDF (Base64 Output)
|
| 53 |
+
# ------------------------------
|
| 54 |
+
@Report_save_router.post("/save_generated_report", response_model=ReportResponse)
|
| 55 |
+
async def save_generated_report(report_request: ReportRequest):
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
# 1. Get HTML from request
|
| 59 |
+
html_content = report_request.html_code
|
| 60 |
+
|
| 61 |
+
# 2. Save HTML to temporary file
|
| 62 |
+
temp_html_file = "temp_report.html"
|
| 63 |
+
with open(temp_html_file, "w", encoding="utf-8") as f:
|
| 64 |
+
f.write(html_content)
|
| 65 |
+
|
| 66 |
+
# 3. Output PDF path
|
| 67 |
+
output_pdf_path = "generated_report.pdf"
|
| 68 |
+
|
| 69 |
+
# 4. Convert HTML → PDF via Playwright
|
| 70 |
+
await html_to_pdf(temp_html_file, output_pdf_path)
|
| 71 |
+
|
| 72 |
+
# 5. Convert PDF to Base64
|
| 73 |
+
with open(output_pdf_path, "rb") as pdf:
|
| 74 |
+
pdf_base64 = base64.b64encode(pdf.read()).decode()
|
| 75 |
+
|
| 76 |
+
# 6. Send result
|
| 77 |
+
return ReportResponse(
|
| 78 |
+
report_generation_status="success",
|
| 79 |
+
report_url=None,
|
| 80 |
+
pdf_base64=pdf_base64
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
raise HTTPException(status_code=500, detail=f"PDF generation failed: {str(e)}")
|