[英]'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.