我有一个在远程 Ubuntu 服务器上运行的 python 脚本。在我的代码中的某个时刻,我创建了一个 HTML 文件,然后将其转换为 png。因此,我选择使用 imgkit,它可以很好地完成这项工作(在将 python 脚本上传到远程 Ubuntu 服务器之前,我在 Mac 操作系统上测试了我的代码):
import imgkit
imgkit.from_url('MyFile.html', 'MyFile.png')
现在的问题是Ubuntu服务器没有安装imgkit和wkhtmltopdf。所以我运行了以下命令:
sudo pip install imgkit
然后:
sudo apt-get install wkhtmltopdf
然后它返回错误:
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": ""}.
到目前为止一切顺利。因此,我运行了以下命令:
sudo apt-get install xvfb
这有效。但 Ubuntu 服务器仍然返回相同的错误。现在,一方面,我不知道: then add option: {"xvfb": ""}.
甚至意味着什么。我试图在网上寻找答案,人们之前引用过这一点,但没有解释人们应该在终端中实际做什么或写什么。我还觉得我缺少 wkhtmltopdf 的扩展,但我不确定是哪个(再次,我进行了在线搜索,但此时我比其他任何事情都更困惑。我在堆栈溢出上发现了类似的线程,但没有什么像问题一样)我正在经历)。任何帮助将不胜感激。
非常感谢 此致, 贝尔蒂
最佳答案
我找到了解决方法。
事实证明,消息中的解决方案非常明确,但我们不知何故设法忽略了这一点。
所以使用此命令后sudo apt-get install xvfb
,
我们所要做的就是在选项字典中添加 {"xvfb": ""}
并将其传递给方法。
例如:
options={'xvfb': ''}
imgkit.from_url('http://google.com', 'out.png', options=options)
希望对您有所帮助!
关于python - (在Ubuntu服务器上使用 'imgkit')wkhtmltopdf : could not connect to any X display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556282/