I'm trying to convert django template to pdf using pdfkit, wkhtmltopdf was installed but I'm getting error like
OSError: wkhtmltopdf exited with non-zero code 1.
error:QXcbConnection: Could not connect to display
How to solve this issue or suggest me any other better way to export django template to pdf?
from django.http import HttpResponse
from django.template.loader import get_template
import pdfkit
def generatepdf(request):
data={}
template = get_template('template_name.html')
html = template.render(data)
pdf = pdfkit.from_string(html, False)
filename = "sample_pdf.pdf"
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'inline; filename="' + filename + '"'
return response