简体   繁体   English

如何从php文件启动unoconv

[英]How to launch unoconv from php file

I want to launch the command "unoconv" from a script php. 我想从脚本php启动命令“unoconv”。

$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. 我认为访问www-data和unoconv是一个问题。

When I'm launching the command in shell, the file is created. 当我在shell中启动命令时,会创建该文件。

Any idea? 任何的想法?

You can add command unoconv to sudoers. 您可以将命令unoconv添加到sudoers。 I do this in this way: 我这样做:

I create wrapper bash script in for example /usr/local/bin where I have command unoconv . 我在例如/ usr / local / bin中创建了包装器bash脚本,其中我有命令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 : 在此之后我在/etc/sudoers.d添加了一个条目:

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

And now you can call script in php: 现在你可以在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. 我在运行Apache时找到了解决这个问题的方法。 You have to create the home folder for the www-data user 您必须为www-data用户创建主文件夹

 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 最后,我们必须编辑www-data用户的主目录和默认shell

sudo vim /etc/passwd

For the entry of www-data the last two strings have to be replaced respectively with 对于www-data的输入,最后两个字符串必须分别替换

/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): 当然你以前必须这样做(如果你从网上发布你的PHP脚本):

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. 您可能遇到LibreOffice,OpenOffice或soffice无法写入当前用户的$HOME目录的问题。

By running the command below I was able to identify the correct $HOME directory and see the error that was being generated. 通过运行下面的命令,我能够识别正确的$HOME目录并查看正在生成的错误。

$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 . $cmd的详细输出将生成写入文件: /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 . 命令运行似乎很好,如root, 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. 感谢Dag Wieers的帮助 - 我希望这可以帮助其他unoconv开发人员进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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