""" PDF export utility using WeasyPrint. Generates PDF documents from rendered markdown HTML. Used by both the authenticated API and public share views. """ import logging from typing import Optional from weasyprint import HTML logger = logging.getLogger("obsigate.pdf") def generate_pdf(html_content: str, title: str = "document", base_url: Optional[str] = None) -> bytes: """Generate a PDF from HTML content. Args: html_content: Full HTML document string. title: Document title (used for metadata). base_url: Base URL for resolving relative URLs in the HTML. Returns: PDF file as bytes. """ html = HTML(string=html_content, base_url=base_url or "") return html.write_pdf() def build_pdf_html(body_html: str, title: str, theme: str = "light") -> str: """Wrap rendered markdown HTML in a complete print-friendly HTML document. Args: body_html: Rendered markdown HTML (without / wrappers). title: Document title. theme: 'light' or 'dark' — light is recommended for PDF. Returns: Complete HTML document string. """ return f""" {title}

{title}

{body_html} """