简体   繁体   English

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

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

I'm trying to use the following shell script to convert a docx file into a pdf: 我正在尝试使用以下Shell脚本将docx文件转换为pdf:

unoconv -f pdf tempfile.docx

In Laravel, I have a controller function which I create a temp docx file using phpWord that I need to convert into a pdf and return the url of. 在Laravel中,我有一个控制器函数,该函数使用phpWord创建一个临时docx文件,需要将其转换为pdf并返回其url。

I installed unoconv using apt-get on my ubuntu 16.04 server, and when I ssh into the server and run unoconv via my terminal it works 100% correctly, however when I run it using an exec() in my controller nothing happens! 我在ubuntu 16.04服务器上使用apt-get安装了unoconv ,当我SSH进入服务器并通过终端运行unoconv时,它可以100%正确地工作,但是当我在控制器中使用exec()运行它时,什么也没发生! I think that it might be the path of the docx is incorrect for some reason but I'm not sure why, as I'm using the exact method to get the filepath that will correctly return the location of the .docx 我认为这可能是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

The docx (and the eventual pdf) file exists in my laravel public folder at the path: /public/store/tempfile.docx docx(以及最终的pdf)文件存在于我的laravel公共文件夹中的以下位置: /public/store/tempfile.docx

I've also tried it using the $filename variable above that calls the path using public_path() and also no dice. 我也使用上面的$ filename变量尝试了它,它使用public_path()调用了路径,并且也没有骰子。

is there some syntax issue with the way that I'm getting the path of my .docx file within my exec() function? 我在exec()函数中获取.docx文件路径的方式是否存在语法问题? Is there some other issue? 还有其他问题吗?

Thanks! 谢谢!

Edit: am now using Symfony to run my shell script but same issue, looking into updating versions and seeing if there's any specific paths of packages i need to change in the unoconv executable. 编辑:现在正在使用Symfony运行我的shell脚本,但存在相同的问题,查看更新版本,并查看unoconv可执行文件中是否需要更改软件包的任何特定路径。 Potentially might also be some kind of permissions issue because root user can run the command but www-data user can't! 可能也可能是某种权限问题,因为root用户可以运行命令,而www-data用户则不能! :( :(

Answering my own question because this took me three years to figure out: 回答我自己的问题,因为这花了我三年时间来弄清楚:

The issue was that in command line when i'm ssh-ed into my server, i'm running off "root" and the server runs exec() / Symfony commands as www-data 问题是在命令行中,当我被SSH到服务器中时,我正在运行“ root”,并且服务器将exec()/ Symfony命令作为www-data运行

The solution was to set www-data's home directory before running the command 解决方案是在运行命令之前设置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;

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

暂无
暂无

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

相关问题 "unoconv 从终端使用 www-data 工作,但不是从 php 脚本也作为 www-data" - unoconv works from terminal using www-data but not from php script also as www-data 通过exec()调用时,长时间运行的PHP脚本会停止,但在通过CLI调用时会结束 - Long running PHP script stops when called through exec(), but finishes when called through the CLI 从终端运行shell脚本有效,但从PHP shell_exec运行时不起作用 - Running shell script from terminal works but doesn't work when running from PHP shell_exec 使用shell_exec从php脚本运行unoconv的问题 - Issue running unoconv from php script with shell_exec 调用控制器的 Laravel 索引函数时的自动调用函数 - Autocall function when laravel index function of a controller is called 命令通过终端正常工作,但不能通过shell_exec php运行 - command works fine through terminal but not shell_exec php exec / system()-脚本被调用直到从PHP调用为止 - exec/system() - script being called works until called from PHP 如果通过AJAX调用脚本,则TCPDF Output()函数方法不起作用,但是如果在浏览器中调用脚本,则TCPDF Output()函数方法有效 - TCPDF Output() function method not working if script is called through AJAX, but works if script is called in browser 具有exec功能的PHP脚本无法在浏览器中使用,但是如果我使用“ php5 script_name.php”将其运行到终端中,它将可以使用 - Php script with exec function doesn't work into browser but it works if I run it into a terminal with “php5 script_name.php” 使用zenity的bash脚本在终端中工作但不在php shell_exec中工作 - bash script using zenity works in terminal but not in php shell_exec
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM