(using 'imgkit' on Ubuntu server) wkhtmltopdf: could not connect to any X display

2k Views Asked by At

I have a python script running on a remote Ubuntu server. At some point in my code, I create an HTML file which I then convert to a png. I therefore elected to use imgkit, which does the job beautifully well (I tested my code on my Mac OS before uploading the python script to the remote Ubuntu server):

import imgkit

imgkit.from_url('MyFile.html', 'MyFile.png')

Now, the problem is that the Ubuntu server did not have imgkit and wkhtmltopdf installed. So I ran the following command:

sudo pip install imgkit

Then:

sudo apt-get install wkhtmltopdf

Then it returned the error:

QXcbConnection: Could not connect to display 
Could not connect to any X display.


You need to install xvfb(sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc), 
then add option: {"xvfb": ""}.

So far so good. I therefore ran the following command:

sudo apt-get install xvfb

Which worked. But the Ubuntu server is still returning the same error. Now, for one thing, I have no idea what: then add option: {"xvfb": ""}. even means. I tried to look for answers online and people quoted that before, without explaining what one should actually do or write in the terminal. I also feel that I am missing extensions for wkhtmltopdf but I am not sure which (again, I ran an online search but I am more confused than anything else at this point. I found similar threads on stack overflow, but nothing quite like the problem I am experiencing). Any help would be deeply appreciated.

Thank you so very much Best regards, Berti

2

There are 2 best solutions below

0
On

I was able to find the workaround.

Turns out the solution was pretty clear in the message, but we somehow managed to ignore that.

So after using this command sudo apt-get install xvfb,

All we have to do is add {"xvfb": ""} in options dict and pass that to the method.

For Eg:

options={'xvfb': ''}
imgkit.from_url('http://google.com', 'out.png', options=options)

I hope that helps!

0
On

I did not find the solution to my question: then add option: {"xvfb": ""}

However I was able to get my code to work. There are two solutions.

1) Use wkhtmltopdf:

import os

os.system("xvfb-run -a wkhtmltopdf %s %s"%('filenameIn.html','filenaneOut.png'))

That produces a PDF file, without the need to setup a virtual display.

2) Setup a virtual display using pyvirtualdisplay:

import imgkit
from pyvirtualdisplay import Display

display = Display(visible=0, size=(600,600))
display.start()
imgkit.from_file("filenameIn.html", "filenameOut.png")
display.stop()

Actually, 2) was answered before and I missed it while searching StackOverflow: "Could not connect to display" on EC2 Server

Note: you can combine 1) and 2): use wkhtmltopdf to convert to png directly, but with a virtual display (otherwise the png will not show the resulting image, at least it was the case for me).

I thought it could help. :) Berti

------------------------------------EDIT-----------------------------------

I had a mistake (now fixed) in my solution: for 2): it should have been "filenameOut.png" instead of "filenameOut.html