Forums

OSError

wkhtmltopdf exited with non-zero code -6. error: QXcbConnection: Could not connect to display

Please help

Please add virtualization

Your account already has the new virtualisation system. What code are you using that generates that error?

def generate_PDF(request, id, customer, invoice_no):

pdf = pdfkit.from_url(request.build_absolute_uri(reverse('invoice-detail', args=[id])), False)

response = HttpResponse(pdf, content_type='application/pdf')
filename = customer + "_" + invoice_no + ".pdf"
response['Content-Disposition'] = 'attachment; filename="%s"' % filename

return response

Above code is working perfectly in local environment , not working in pythonanywhere

have you seen something like this?

Error running WSGI application 2021-04-03 19:58:54,611: ModuleNotFoundError: No module named 'main' 2021-04-03 19:58:54,611: File "/var/www/bharatbezawada_pythonanywhere_com_wsgi.py", line 18, in <module> 2021-04-03 19:58:54,611: application = get_wsgi_application() 2021-04-03 19:58:54,611: 2021-04-03 19:58:54,611: File "/home/BharatBezawada/.virtualenvs/myenv/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application 2021-04-03 19:58:54,611: django.setup(set_prefix=False) 2021-04-03 19:58:54,611: 2021-04-03 19:58:54,611: File "/home/BharatBezawada/.virtualenvs/myenv/lib/python3.8/site-packages/django/init.py", line 24, in setup 2021-04-03 19:58:54,611: apps.populate(settings.INSTALLED_APPS) 2021-04-03 19:58:54,611: 2021-04-03 19:58:54,612: File "/home/BharatBezawada/.virtualenvs/myenv/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate 2021-04-03 19:58:54,612: app_config = AppConfig.create(entry) 2021-04-03 19:58:54,612: 2021-04-03 19:58:54,612: File "/home/BharatBezawada/.virtualenvs/myenv/lib/python3.8/site-packages/django/apps/config.py", line 90, in create 2021-04-03 19:58:54,612: module = import_module(entry) 2021-04-03 19:58:54,612: 2021-04-03 19:58:54,612: File "/home/BharatBezawada/.virtualenvs/myenv/lib/python3.8/site-packages/wkhtmltopdf/init.py", line 1, in <module> 2021-04-03 19:58:54,612: from main import WKhtmlToPdf, wkhtmltopdf 2021-04-03 19:58:54,612: ********* 2021-04-03 19:58:54,612: If you're seeing an import error and don't know why, 2021-04-03 19:58:54,612: we have a dedicated help page to help you debug: 2021-04-03 19:58:54,613: https://help.pythonanywhere.com/pages/DebuggingImportError/

above is the error log now

def generate_PDF(request, id, customer, invoice_no): # Use False instead of output path to save pdf to a variable

display = Display(visible=0, size=(1024, 768))
try:
   display.start()
   pdf = pdfkit.from_url(request.build_absolute_uri(reverse('invoice-detail', args=[id])), False)
   response = HttpResponse(pdf, content_type='application/pdf')
   filename = customer + "_" + invoice_no + ".pdf"
   response['Content-Disposition'] = 'attachment; filename="%s"' % filename
finally:
   display.stop()
return response

above is my code

it seems i installed wrong one , now installed pip install django-wkhtmltopdf

but getting error

No module named 'wkhtmltopdf'

i added in settings.py too

khtmltopdf reported an error: Loading page (1/2) [> ] 0% [======> ] 10% Error: Failed loading page https://bharatbezawada.pythonanywhere.com/invoice-detail/8/ (sometimes it will work just to ignore this error with --load-error-handling ignore) Exit with code 1 due to network error: ConnectionRefusedError

wkhtmltopdf reported an error: Loading page (1/2) [> ] 0% [======> ] 10% Error: Failed loading page https://bharatbezawada.pythonanywhere.com (sometimes it will work just to ignore this error with --load-error-handling ignore) Exit with code 1 due to network error: ConnectionRefusedError

I am getting connnection refused error for my own site , working well with google.com

where is this code running? is it running also from your webapp?

All code is running from webapp , i need to convert one html page to pdf , that needs connection to my own site .

I see that you posted in another forum thread about this, so let's continue the conversation there.