38156-vm/core/utils.py
Konrad du Plessis c2c83a58ff Update CLAUDE.md with design conventions and fix mutable default arg in utils.py
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 01:33:48 +02:00

26 lines
615 B
Python

from io import BytesIO
from django.template.loader import get_template
from xhtml2pdf import pisa
from django.conf import settings
import os
# === PDF GENERATION ===
def render_to_pdf(template_src, context_dict=None):
if context_dict is None:
context_dict = {}
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
# Enable logging for debugging
# pisa.showLogging()
# Create the PDF
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result)
if not pdf.err:
return result.getvalue()
return None