我正在尝试通过py.sh执行xvfb-run
,但我得到sh.ErrorReturnCode_1
并且没有创建结果pdf。
我创建了一个小的html文件:
$ echo '<h1>Hello, World.</h1>' > test.html
然后我通过Python中的sh.py运行xvfb_run
:
$ 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
环境var没有设置,但我也没有任何喜悦:
>>> 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.sh和call
无法运行该命令?这里有什么我想念的吗?
答案 0 :(得分: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')