0

我正在尝试xvfb-run通过 py.sh 执行,但我得到sh.ErrorReturnCode_1并且没有创建任何结果 pdf。

我创建了一个小 html 文件:

$ echo '<h1>Hello, World.</h1>' > test.html

然后我xvfb_run在 Python 中通过 sh.py 运行:

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sh import xvfb_run
>>> xvfb_run('--server-num=1 --server-args="-screen 0, 1024x768x24" '
...     '/usr/bin/wkhtmltopdf --ignore-load-errors', 'test.html', 'test.pdf')
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/mark/.virtualenvs/reports/local/lib/python2.7/site-packages/sh.py", line 726, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/home/mark/.virtualenvs/reports/local/lib/python2.7/site-packages/sh.py", line 291, in __init__
    self.wait()
  File "/home/mark/.virtualenvs/reports/local/lib/python2.7/site-packages/sh.py", line 295, in wait
    self._handle_exit_code(self.process.wait())
  File "/home/mark/.virtualenvs/reports/local/lib/python2.7/site-packages/sh.py", line 309, in _handle_exit_code
    self.process.stderr
sh.ErrorReturnCode_1: 

  RAN: '/usr/bin/xvfb-run --server-num=1 --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf --ignore-load-errors test.html test.pdf'

  STDOUT:


  STDERR:

然后,我在 shell 中检查是否已创建任何内容,但没有任何内容:

$ ls -l
total 4
-rw-rw-r-- 1 mark mark 23 May 22 07:54 test.html

所以我然后xvfb-run从上面复制命令,它工作正常:

$ /usr/bin/xvfb-run --server-num=1 --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf --ignore-load-errors test.html test.pdf
Loading page (1/2)
Printing pages (2/2)                                               
Done                                                           

还有我试图创建的 PDF 文件:

$ ls -l
total 12
-rw-rw-r-- 1 mark mark   23 May 22 07:54 test.html
-rw-rw-r-- 1 mark mark 7091 May 22 07:55 test.pdf

然后我尝试使用call标准库中的方法:

>>> from subprocess import call
>>> call(['/usr/bin/xvfb-run', '--server-num=1', '--server-args="-screen 0, 1024x768x24"', '/usr/bin/wkhtmltopdf', '--ignore-load-errors', 'test.html', 'test.pdf'])
xvfb-run: error: Xvfb failed to start
1

然后我认为DISPLAY没有设置环境变量,但我对此也不满意:

>>> import os
>>> os.environ["DISPLAY"]=":99"
>>> call(['/usr/bin/xvfb-run', '--server-num=1', '--server-args="-screen 0, 1024x768x24"', '/usr/bin/wkhtmltopdf', '--ignore-load-errors', 'test.html', 'test.pdf'])
xvfb-run: error: Xvfb failed to start
1

>>> call(['/usr/bin/wkhtmltopdf', '--ignore-load-errors', 'test.html', 'test.pdf'])
wkhtmltopdf: cannot connect to X server :99
1

>>> os.environ["DISPLAY"]=":1"
>>> call(['/usr/bin/wkhtmltopdf', '--ignore-load-errors', 'test.html', 'test.pdf'])
wkhtmltopdf: cannot connect to X server :1

知道为什么 py.shcall无法运行该命令吗?我在这里缺少什么吗?

4

1 回答 1

0

我需要/usr/bin/Xvfb :1 -screen 0 1024x768x24单独运行并让它在后台运行。然后就不需要了xvfb-run,我可以在wkhtmltopdf没有帮助的情况下执行。

>>> import os
>>> os.environ["DISPLAY"]=":1"
>>> from sh import wkhtmltopdf
>>> wkhtmltopdf('--ignore-load-errors', 'test.html', 'test.pdf')
于 2013-05-23T07:37:33.653 回答