""" PDF generation service for executive reports. Renders the executive PDF template without requiring an HttpRequest. """ from django.conf import settings from django.template.loader import render_to_string from django.utils import timezone from weasyprint import HTML def generate_executive_pdf(report, user=None) -> bytes: """ Generate a PDF byte stream for an ExecutiveReport. Args: report: ExecutiveReport instance user: User requesting the PDF (optional) Returns: PDF file contents as bytes """ from apps.core.pdf_utils import _get_logo_data_uri context = { "report": report, "narrative": report.narrative_en, "highlights": report.highlights_en, "concerns": report.concerns_en, "metrics": report.metrics_snapshot, "generated_at": timezone.now().strftime("%Y-%m-%d %H:%M"), "user": user, "logo_path": _get_logo_data_uri(), } html_string = render_to_string("executive/pdf_report.html", context) return HTML(string=html_string, base_url=str(settings.BASE_DIR / "static")).write_pdf()