Update app.py
Browse files
app.py
CHANGED
|
@@ -149,10 +149,6 @@ Your goal is to create a Recruitment Management Application HTML layout that fol
|
|
| 149 |
|
| 150 |
@app.post("/generate_pdf")
|
| 151 |
async def generate_pdf(request: Request):
|
| 152 |
-
"""
|
| 153 |
-
Generate screenshots (screens + workflows) from generated HTML mockup
|
| 154 |
-
and compile them into a downloadable PDF.
|
| 155 |
-
"""
|
| 156 |
data = await request.json()
|
| 157 |
html = data.get("html", "")
|
| 158 |
if not html.strip():
|
|
@@ -165,14 +161,19 @@ async def generate_pdf(request: Request):
|
|
| 165 |
f.write(html)
|
| 166 |
|
| 167 |
screenshots = []
|
| 168 |
-
|
| 169 |
try:
|
|
|
|
| 170 |
with sync_playwright() as p:
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
page = browser.new_page()
|
| 173 |
page.goto(f"file://{tmp_html_path}", wait_until="networkidle")
|
|
|
|
| 174 |
|
| 175 |
-
# Detect clickable screens from nav/sidebar
|
| 176 |
selectors = ["nav a", ".sidebar a", ".menu-item a", "a[href^='#']"]
|
| 177 |
screen_links = []
|
| 178 |
for sel in selectors:
|
|
@@ -197,17 +198,27 @@ async def generate_pdf(request: Request):
|
|
| 197 |
|
| 198 |
browser.close()
|
| 199 |
except Exception as e:
|
|
|
|
|
|
|
| 200 |
return JSONResponse({"error": f"Browser capture error: {str(e)}"}, status_code=500)
|
| 201 |
|
| 202 |
# Build PDF
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
return JSONResponse({"pdf_url": f"/static/outputs/{os.path.basename(pdf_path)}"})
|
|
|
|
| 149 |
|
| 150 |
@app.post("/generate_pdf")
|
| 151 |
async def generate_pdf(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
data = await request.json()
|
| 153 |
html = data.get("html", "")
|
| 154 |
if not html.strip():
|
|
|
|
| 161 |
f.write(html)
|
| 162 |
|
| 163 |
screenshots = []
|
|
|
|
| 164 |
try:
|
| 165 |
+
from playwright.sync_api import sync_playwright
|
| 166 |
with sync_playwright() as p:
|
| 167 |
+
# 👇 Add this logging
|
| 168 |
+
print("✅ Launching Chromium headless browser...")
|
| 169 |
+
browser = p.chromium.launch(
|
| 170 |
+
headless=True,
|
| 171 |
+
args=["--no-sandbox", "--disable-setuid-sandbox"]
|
| 172 |
+
)
|
| 173 |
page = browser.new_page()
|
| 174 |
page.goto(f"file://{tmp_html_path}", wait_until="networkidle")
|
| 175 |
+
print("✅ Page loaded successfully")
|
| 176 |
|
|
|
|
| 177 |
selectors = ["nav a", ".sidebar a", ".menu-item a", "a[href^='#']"]
|
| 178 |
screen_links = []
|
| 179 |
for sel in selectors:
|
|
|
|
| 198 |
|
| 199 |
browser.close()
|
| 200 |
except Exception as e:
|
| 201 |
+
import traceback
|
| 202 |
+
print("❌ Browser capture error:", traceback.format_exc())
|
| 203 |
return JSONResponse({"error": f"Browser capture error: {str(e)}"}, status_code=500)
|
| 204 |
|
| 205 |
# Build PDF
|
| 206 |
+
try:
|
| 207 |
+
from reportlab.lib.pagesizes import A4
|
| 208 |
+
from reportlab.pdfgen import canvas
|
| 209 |
+
from reportlab.lib.utils import ImageReader
|
| 210 |
+
|
| 211 |
+
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 212 |
+
width, height = A4
|
| 213 |
+
for image_path in screenshots:
|
| 214 |
+
img = ImageReader(image_path)
|
| 215 |
+
iw, ih = img.getSize()
|
| 216 |
+
scale = min(width / iw, height / ih)
|
| 217 |
+
c.drawImage(img, 0, 0, iw * scale, ih * scale)
|
| 218 |
+
c.showPage()
|
| 219 |
+
c.save()
|
| 220 |
+
except Exception as e:
|
| 221 |
+
print("❌ PDF generation error:", e)
|
| 222 |
+
return JSONResponse({"error": f"PDF generation failed: {str(e)}"}, status_code=500)
|
| 223 |
|
| 224 |
return JSONResponse({"pdf_url": f"/static/outputs/{os.path.basename(pdf_path)}"})
|