I am trying to write an api which takes input as json and gives you response as pdf. For this I am using pdfkit library in python flask. I am having trouble deploying it on remote server as docker container. However it works fine on my local machine both with and without docker. I have almost tried all the given solutions online. Following is the error which I am receiving
wkhtmltopdf exited with non-zero code -6. error:\nQXcbConnection: Could not connect to display \n
So apparently when you run your application on a cloud machine, in reality, it doesn't have a UI per se i.e for wkhtml to work and convert to pdf it needs some UI. So for this what I did was to create a virtual display. Following is a raw code for it.
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
result = pdfkit.from_file(<file_name>)
display.stop()
This worked for me.