0

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
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
kirankumar
  • 37
  • 7
  • Have you seen this? https://unix.stackexchange.com/questions/192642/wkhtmltopdf-qxcbconnection-could-not-connect-to-display – Samantha Dec 04 '19 at 05:13
  • Does this answer your question? [Render HTML to PDF in Django site](https://stackoverflow.com/questions/1377446/render-html-to-pdf-in-django-site) – funnydman Dec 04 '19 at 07:31

1 Answers1

0

You can try https://pypi.org/project/django-wkhtmltopdf/. It works without issue and the pdf has no issue in terms of layout. It is very easy to implement.

Deep
  • 64
  • 6