Skip to content Skip to sidebar Skip to footer

How Can I Convert A Html Page To Pdf Using Django

I have a web app in Django. It's a plataform to store bills and invoices. Now i'm trying to export those bills un PDF. I'm using xhtml2pdf but it's not working. I'm using this code

Solution 1:

Try using this code. It works for me. Change "template_testing.html" for your template and add your data to render on "data = {}"

views.py:

import os
from django.conf import settings
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
import datetime
from xhtml2pdf import pisa 


defgenerate_PDF(request):
    data = {}

    template = get_template('template_testing.html')
    html  = template.render(Context(data))

    file = open('test.pdf', "w+b")
    pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file,
            encoding='utf-8')

    file.seek(0)
    pdf = file.read()
    file.close()            
    return HttpResponse(pdf, 'application/pdf')

Post a Comment for "How Can I Convert A Html Page To Pdf Using Django"