Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -227,6 +227,7 @@ def html_to_pdf_playwright(html_path: str, pdf_path: str) -> bool:
|
|
| 227 |
"""Convert HTML to PDF using Playwright with Windows compatibility"""
|
| 228 |
import asyncio
|
| 229 |
import sys
|
|
|
|
| 230 |
|
| 231 |
async def create_pdf():
|
| 232 |
from playwright.async_api import async_playwright
|
|
@@ -255,17 +256,49 @@ def html_to_pdf_playwright(html_path: str, pdf_path: str) -> bool:
|
|
| 255 |
|
| 256 |
await browser.close()
|
| 257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
try:
|
| 259 |
# Set event loop policy for Windows compatibility
|
| 260 |
if sys.platform == "win32":
|
| 261 |
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
return True
|
| 266 |
|
| 267 |
except Exception as e:
|
| 268 |
print(f"Playwright conversion failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
# Additional debugging for Windows issues
|
| 270 |
if "NotImplementedError" in str(e):
|
| 271 |
print("Windows asyncio compatibility issue detected. Trying alternative approach...")
|
|
|
|
| 227 |
"""Convert HTML to PDF using Playwright with Windows compatibility"""
|
| 228 |
import asyncio
|
| 229 |
import sys
|
| 230 |
+
import traceback
|
| 231 |
|
| 232 |
async def create_pdf():
|
| 233 |
from playwright.async_api import async_playwright
|
|
|
|
| 256 |
|
| 257 |
await browser.close()
|
| 258 |
|
| 259 |
+
def _try_generate() -> bool:
|
| 260 |
+
try:
|
| 261 |
+
# Set event loop policy for Windows compatibility
|
| 262 |
+
if sys.platform == "win32":
|
| 263 |
+
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
| 264 |
+
asyncio.run(create_pdf())
|
| 265 |
+
return True
|
| 266 |
+
except Exception as ex:
|
| 267 |
+
raise ex
|
| 268 |
+
|
| 269 |
+
def _install_playwright_browser_once() -> bool:
|
| 270 |
+
flag = os.path.join(tempfile.gettempdir(), 'playwright_browser_installed')
|
| 271 |
+
if os.path.exists(flag):
|
| 272 |
+
return True
|
| 273 |
+
try:
|
| 274 |
+
subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"], check=True)
|
| 275 |
+
with open(flag, 'w') as f:
|
| 276 |
+
f.write('installed')
|
| 277 |
+
print("✅ Playwright Chromium downloaded at runtime.")
|
| 278 |
+
return True
|
| 279 |
+
except Exception as ex:
|
| 280 |
+
print(f"❌ Runtime Playwright browser install failed: {ex}")
|
| 281 |
+
return False
|
| 282 |
+
|
| 283 |
try:
|
| 284 |
# Set event loop policy for Windows compatibility
|
| 285 |
if sys.platform == "win32":
|
| 286 |
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
| 287 |
+
# First attempt
|
| 288 |
+
if _try_generate():
|
| 289 |
+
return True
|
|
|
|
| 290 |
|
| 291 |
except Exception as e:
|
| 292 |
print(f"Playwright conversion failed: {e}")
|
| 293 |
+
err_text = f"{e}\n{traceback.format_exc()}"
|
| 294 |
+
# If browsers are not downloaded, try to fetch them once and retry
|
| 295 |
+
if "Executable doesn't exist" in err_text or "playwright install" in err_text:
|
| 296 |
+
if _install_playwright_browser_once():
|
| 297 |
+
try:
|
| 298 |
+
if _try_generate():
|
| 299 |
+
return True
|
| 300 |
+
except Exception as e2:
|
| 301 |
+
print(f"Retry after browser install failed: {e2}")
|
| 302 |
# Additional debugging for Windows issues
|
| 303 |
if "NotImplementedError" in str(e):
|
| 304 |
print("Windows asyncio compatibility issue detected. Trying alternative approach...")
|