(在 Ubuntu 服务器上使用“imgkit")wkhtmltopdf:无法连接到任何 X 显示器 [英] (using 'imgkit' on Ubuntu server) wkhtmltopdf: could not connect to any X display

查看:58
本文介绍了(在 Ubuntu 服务器上使用“imgkit")wkhtmltopdf:无法连接到任何 X 显示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在远程 Ubuntu 服务器上运行的 python 脚本.在我的代码中的某个时刻,我创建了一个 HTML 文件,然后将其转换为 png.因此,我选择使用 imgkit,它可以很好地完成这项工作(在将 python 脚本上传到远程 Ubuntu 服务器之前,我在 Mac OS 上测试了我的代码):

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')

现在,问题是Ubuntu服务器没有安装imgkit和wkhtmltopdf.所以我运行了以下命令:

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

那么:

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": ""}.

到目前为止一切顺利.因此,我运行了以下命令:

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

sudo apt-get install xvfb

哪个有效.但是 Ubuntu 服务器仍然返回相同的错误.现在,一方面,我不知道是什么:then add option: {"xvfb": ""}. 甚至意味着.我试图在网上寻找答案,人们之前引用过,但没有解释实际应该做什么或在终端中写什么.我也觉得我缺少 wkhtmltopdf 的扩展,但我不确定是哪个(同样,我进行了在线搜索,但此时我比其他任何事情都更困惑.我在堆栈溢出时发现了类似的线程,但没有什么比问题更像我正在经历).任何帮助将不胜感激.

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.

非常感谢此致,贝尔蒂

推荐答案

我找到了解决方法.

原来消息中的解决方案非常清楚,但我们以某种方式设法忽略了这一点.

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

所以使用这个命令后sudo apt-get install xvfb,

我们所要做的就是在 options dict 中添加 {"xvfb": ""} 并将其传递给方法.

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

例如:

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

希望能帮到你!

这篇关于(在 Ubuntu 服务器上使用“imgkit")wkhtmltopdf:无法连接到任何 X 显示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆