from django.views.generic.base import ContextMixin from django.template.loader import render_to_string from django.template.exceptions import TemplateDoesNotExist from django.core.mail import EmailMultiAlternatives from django.conf import settings from html.parser import HTMLParser from bs4 import BeautifulSoup import pdfkit class PDFView(ContextMixin): template_name = None def render_to_html(self, **kwargs): context = self.get_context_data(**kwargs) return render_to_string(self.template_name, context) def render(self, **kwargs): html = self.render_to_html(**kwargs) pdf = pdfkit.from_string(html, False) return pdf