Exporting HTML Page into PDF In Django
Views.py
from django.shortcuts import render
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
from django.views import generic
import pandas as pd
# Create your views here.
# defining the function to convert an HTML file to a PDF file
def html_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')
return None
class GeneratePdf(generic.View):
def get(self, request, *args, **kwargs):
# getting the template
pdf = html_to_pdf('app/dashboard/climate_risk.html')
# rendering the template
return HttpResponse(pdf, content_type='application/pdf')
urls.py
path('generate_PDF', views.generate_PDF),
Comments
Post a Comment