[Answered ]-Generate PDF from http request in Django

0👍

As an alternative solution, you could try wkhtmltopdf https://github.com/incuna/django-wkhtmltopdf. Usage is fairly simple. You should build the desired layout as a simple HTML template and give to PDFTemplateView which will then render pdf file as response.

from django.conf.urls.defaults import url
from wkhtmltopdf.views import PDFTemplateView

urlpatterns = patterns('',
    url(r'^pdf/$', PDFTemplateView.as_view(template_name='my_template.html',
        filename='my_pdf.pdf'), name='pdf'),
)

2👍

I think you actually have two questions: 1-how to handle json data in python, and 2-how to generate a pdf out of a template (correct me if I’m wrong)

I’m gonna answer the second question here. Search the web for the first one 😉

After json conversion, you can send the converted data to your html template and generate a pdf using pdfkit.

template = get_template("app_name/template.html")
context = Context({'data1':value1, 'data2':valu2})
html = template.render(context)
pdf = pdfkit.from_string(html, False)
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=output.pdf'
return response
👤S_M

0👍

For generating PDF at request, you can also use WeasyPrint. You can follow this Tutorial

👤AR7

Leave a comment