38156-vm/core/utils.py
2026-02-04 17:36:56 +00:00

21 lines
531 B
Python

from io import BytesIO
from django.template.loader import get_template
from xhtml2pdf import pisa
from django.conf import settings
import os
def render_to_pdf(template_src, 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