I am trying to render an HTML page into PDF in Django by following this GitHub tutorial link.
My views.py:
def fetch_resources(uri, rel):
path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
return path
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')
return None
class ViewPDF(View):
def get(self, request, *args, **kwargs):
pdf = render_to_pdf('app/pdfpage.html', data)
return HttpResponse(pdf, content_type='application/pdf')
#Automaticly downloads to PDF file
class DownloadPDF(View):
def get(self, request, *args, **kwargs):
pdf = render_to_pdf('app/pdfpage.html', data)
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Invoice_%s.pdf" %("12341233")
content = "attachment; filename='%s'" %(filename)
response['Content-Disposition'] = content
return response
However I almost copy all the code from that tutorial link except pdfpage.html, I put there my custom made HTML page also with bootstrap and CSS styling. Now my problem is after converting and saving this pdf, it lost all the styling and bootstrap styling stuff, but those are also important for me. how can I solve this problem?