简体   繁体   中英

How to launch unoconv from php file

I want to launch the command "unoconv" from a script php.

$command = '/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf >/dev/null 2>/dev/null';
$rc = system( $command );
echo $rc;

The command return no result and the file is not created.

I think is a problem from access with www-data and unoconv.

When I'm launching the command in shell, the file is created.

Any idea?

You can add command unoconv to sudoers. I do this in this way:

I create wrapper bash script in for example /usr/local/bin where I have command unoconv .

#!/bin/bash

if [ -z "$1" ]; then
    echo "Must pass file";
    exit 10;
fi

/usr/bin/unoconv -f pdf $1.rtf

after this I adding entry in /etc/sudoers.d :

www-data    ALL=NOPASSWD: /usr/local/bin/unoconv.sh

And now you can call script in php:

exec('sudo /usr/local/bin/unoconv.sh '.$fileName);

Try to run

$output = `/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf`;

instead and see error messages.

I have found a solution to this problem when running Apache. You have to create the home folder for the www-data user

 sudo mkdir /home/www-data
 sudo chown www-data /home/www-data

Lastly we will have to edit the home directory and default shell for the www-data user

sudo vim /etc/passwd

For the entry of www-data the last two strings have to be replaced respectively with

/home/www-data
/bin/bash

Simple as this

$output = shell_exec('/opt/libreoffice5.0/program/python unoconv -f rtf test.html');

Edit the path to suite your configuration.

It just works!

For me works like this:

$cmd = "/usr/bin/unoconv -f docx files/thefile";
shell_exec($cmd);

of course you have to do this previously (if you lounch your php script from the web):

chown -R www-data:www-data files/

You may be running into an issue with LibreOffice, OpenOffice or soffice not being able to write to the current user's $HOME directory.

By running the command below I was able to identify the correct $HOME directory and see the error that was being generated.

$cmd = 'echo $HOME & unoconv -vvvv --format %s --output %s %s 2>/tmp/unoconv.debug.txt';
exec($cmd);

The verbose output of $cmd will be generated written to the file: /tmp/unoconv.debug.txt .

In my case the output was:

Verbosity set to level 5
DEBUG: Connection type: socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext
DEBUG: Existing listener not found.
DEBUG: Launching our own listener using /usr/lib64/libreoffice/program/soffice.bin.
Failed to connect to /usr/lib64/libreoffice/program/soffice.bin (pid=32012) in 6 seconds.
Connector : couldn't connect to socket (Success)
Error: Unable to connect or start own listener. Aborting.

The command ran seemed to fine as root, and as sudo -u nobody . On seeing this output I realized there was an issue with the home directory.

Kudos to Dag Wieers for his help - I'm hoping this helps other unoconv devs with their debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM