繁体   English   中英

'unoconv'脚本在终端中有效,但在我的laravel控制器中通过exec()函数调用时不起作用

[英]'unoconv' script works in terminal but not when called through an exec() function in my laravel controller

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

unoconv -f pdf tempfile.docx

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

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

        $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用户则不能! :(

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

问题是在命令行中,当我被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

暂无
暂无

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

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