How to create pdf file using Django template with proper CSS?

Viewed 73

I am new to Django and stack overflow as well. I want to create Django template as a pdf file with all CSS styles .I used following code to create pdf file it renders file content properly but not CSS styles.

Here is the code I have used :

from io import BytesIO
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.http import HttpResponse
from django.shortcuts import render


def home(request):
    pdf = render_to_pdf("abc.html")
    return HttpResponse(pdf, content_type='application/pdf')


def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    else
        return None
0 Answers
Related