[英]unoconv works from terminal using www-data but not from php script also as www-data
[英]'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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.