21 lines
531 B
Python
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
|