1

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?

Kanchon Gharami
  • 777
  • 1
  • 11
  • 30
  • 1
    Check out https://stackoverflow.com/questions/8986831/css-not-rendered-by-pisas-pdf-generation-in-django which should help! – Steve Jalim Dec 29 '20 at 16:00
  • I tried that technique, but actually, I cannot understand how that method works. should I just add **fetch_resources** function? (edited in the question) – Kanchon Gharami Dec 29 '20 at 17:11

0 Answers0