Question

I'm trying to execute xvfb-run via py.sh but I'm getting sh.ErrorReturnCode_1 and no resulting pdf being created.

I created a small html file:

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

And I then ran xvfb_run via sh.py in Python:

$ 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:

I then check back in the shell to see if anything has been created and nothing has:

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

So I then copy out the xvfb-run command from above and it works fine:

$ /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                                                           

And there is the PDF file I was trying to create:

$ 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

I then tried with the call method from the standard library:

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

I then thought that the DISPLAY environment var isn't set but I'm not having any joy with that either:

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

Any idea why py.sh and call couldn't run that command? Is there something I'm missing here?

Was it helpful?

Solution

I needed to run /usr/bin/Xvfb :1 -screen 0 1024x768x24 separately and keep it running in the background. Then there was no need for xvfb-run, I could execute wkhtmltopdf un-aided.

>>> import os
>>> os.environ["DISPLAY"]=":1"
>>> from sh import wkhtmltopdf
>>> wkhtmltopdf('--ignore-load-errors', 'test.html', 'test.pdf')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top