简体   繁体   中英

'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:

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.

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! 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

        $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

I've also tried it using the $filename variable above that calls the path using public_path() and also no dice.

is there some syntax issue with the way that I'm getting the path of my .docx file within my exec() function? 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. Potentially might also be some kind of permissions issue because root user can run the command but www-data user can't! :(

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

The solution was to set www-data's home directory before running the command

 $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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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