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
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!
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