HH/apps/executive_summary/pdf_service.py
2026-04-19 10:53:12 +03:00

33 lines
865 B
Python

"""
PDF generation service for executive reports.
Renders the executive PDF template without requiring an HttpRequest.
"""
from django.template.loader import render_to_string
from django.utils import timezone
from weasyprint import HTML
def generate_executive_pdf(report) -> bytes:
"""
Generate a PDF byte stream for an ExecutiveReport.
Args:
report: ExecutiveReport instance
Returns:
PDF file contents as bytes
"""
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"),
}
html_string = render_to_string("executive/pdf_report.html", context)
return HTML(string=html_string).write_pdf()