Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| from pathlib import Path | |
| from playwright.sync_api import sync_playwright | |
| from ..core.config import get_settings | |
| def render_pdf(session_id: str, output_path: Path) -> Path: | |
| settings = get_settings() | |
| base_url = settings.frontend_base_url.rstrip("/") | |
| url = f"{base_url}/print/report?session={session_id}" | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| with sync_playwright() as playwright: | |
| browser = playwright.chromium.launch(headless=True, args=["--no-sandbox"]) | |
| context = browser.new_context() | |
| page = context.new_page() | |
| page.goto(url, wait_until="networkidle", timeout=settings.pdf_timeout_ms) | |
| page.wait_for_selector( | |
| '[data-print-ready="true"]', | |
| timeout=settings.pdf_timeout_ms, | |
| ) | |
| page.wait_for_function("document.fonts && document.fonts.ready") | |
| page.wait_for_function( | |
| "Array.from(document.images || []).every(img => img.complete)" | |
| ) | |
| page.pdf( | |
| path=str(output_path), | |
| format="A4", | |
| print_background=True, | |
| prefer_css_page_size=True, | |
| margin={"top": "10mm", "right": "10mm", "bottom": "10mm", "left": "10mm"}, | |
| ) | |
| context.close() | |
| browser.close() | |
| return output_path | |