首页 > 解决方案 > 'unoconv' 脚本在终端中有效,但在我的 laravel 控制器中通过 exec() 函数调用时无效

问题描述

我正在尝试使用以下 shell 脚本将 docx 文件转换为 pdf:

unoconv -f pdf tempfile.docx

在 Laravel 中,我有一个控制器函数,我使用 phpWord 创建一个临时 docx 文件,我需要将其转换为 pdf 并返回其 url。

我在我的服务器上安装了unoconvusing ,当我 ssh 进入服务器并通过终端运行 unoconv 时,它可以 100% 正确工作,但是当我在控制器中使用 exec() 运行它时,什么也没有发生!我认为它可能是 docx 的路径由于某种原因不正确,但我不知道为什么,因为我正在使用确切的方法来获取将正确返回 .docx 位置的文件路径apt-getubuntu 16.04

        $path_copy = 'store/' . $email . '_higher_results.docx';
        $path_copy_pdf = 'store/' . $email . '_higher_results.pdf';
        $filename = public_path($path_copy);

        $the_download->saveAs($filename); //saves my .docx created by phpWord

        exec('unoconv -f pdf ' . $path_copy); //should take the .docx created above and convert it into a pdf

        //return $path_copy; //this returns the correct path of the .docx, and adds it to a link in my .vue component which allows the user to download the .docx
        return $path_copy_pdf; //this SHOULD return the correct path of the PDF but the PDF is never created at the exec() step

docx(以及最终的 pdf)文件存在于我的 laravel 公用文件夹中,路径为:/public/store/tempfile.docx

我也尝试过使用上面的 $filename 变量,它使用 public_path() 调用路径,也没有骰子。

我在 exec() 函数中获取 .docx 文件路径的方式是否存在语法问题?还有其他问题吗?

谢谢!

编辑:我现在使用 Symfony 运行我的 shell 脚本但同样的问题,查看更新版本并查看我需要在 unoconv 可执行文件中更改的包的任何特定路径。可能也可能是某种权限问题,因为 root 用户可以运行命令但 www-data 用户不能!:(

标签: phplaravelshellphpofficeunoconv

解决方案


回答我自己的问题,因为这花了我三年的时间才弄清楚:

问题是,当我通过 ssh 进入服务器时,在命令行中,我正在运行“root”并且服务器将 exec() / Symfony 命令作为 www-data 运行

解决方案是在运行命令之前设置 www-data 的主目录

 $process = new Process("HOME=".getCWD()." && export HOME && libreoffice --headless --convert-to pdf store/mar_maribov_higher_results.docx --outdir store");
        $process->run();
        return $path_copy_pdf;

信用:https ://stackoverflow.com/a/37666818/3812918


推荐阅读