Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions apps/backend/app/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ async def _render_page_to_pdf(
pdf_format: str,
pdf_margins: dict,
) -> bytes:
await page.goto(url, wait_until="networkidle")
await page.wait_for_selector(selector)
await page.evaluate("document.fonts.ready")
await page.goto(url, wait_until="domcontentloaded", timeout=15000)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix! The domcontentloaded strategy is the right approach for static PDF rendering.

One suggestion: the bare except Exception: pass on document.fonts.ready silently swallows font loading failures, which makes production debugging difficult. Consider adding a logger.warning(...) so operators can see when fonts fail to load without blocking PDF generation.

Example:

try:
    await page.evaluate("document.fonts.ready", timeout=10000)
except Exception as e:
    logger.warning(f"Font loading failed for PDF generation (non-critical): {e}")

This is a small addition but significantly improves observability in production environments.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review and the suggestion!

You're absolutely right, silently swallowing the exception reduces visibility into font-loading issues in production. Adding a non-blocking warning log is a good balance between reliability and observability

await page.wait_for_selector(selector, timeout=10000)
try:
await page.evaluate("document.fonts.ready", timeout=10000)
except Exception:
# Font loading is not critical for PDF rendering
pass
return await page.pdf(
format=pdf_format,
print_background=True,
Expand Down