1

I'm currently uploading a DOC or DOCX file via DropzoneJS and then convert it to PDF in the server using LibreOffice PHP exec. If I typed it manually in the server then it will convert but if I run it via browser then it is not converting. I'm trying to run the soffice without a sudo but the command run via browser is not working. How can I convert it via browser run?

Manual Command in Terminal:

/opt/libreoffice5.2/program/soffice --convert-to pdf /var/www/html/my_system/public/msword.doc --outdir /var/www/html/my_system/storage/app/quotations

Command ls -liah results for folders:

my_system, storage, app, quotations = drwxrwxrwx apache apache

My sudo visudo results (pasted lines with no comments in order):

Defaults    requiretty
Defaults   !visiblepw
Defaults    always_set_home

www-data ALL=NOPASSWD:ALL
robert ALL=NOPASSWD:ALL
apache ALL=NOPASSWD:ALL

PHP File:

$command = '/opt/libreoffice5.2/program/soffice --convert-to pdf /var/www/html/my_system/public/msword.doc --outdir /var/www/html/my_system/storage/app/quotations';

if (!$return) {
    echo "PDF Created Successfully";
} else {
    echo 'PDF not created. Command = ' . $command . '=' . $return;
}

Thanks.

2
  • How are you running it? There is no $return in your sample code, so I preume that's abbreviated. And wjat is the command exit code? Commented Oct 17, 2016 at 0:49
  • @bishop I'm running it via upload, drag and drop in browser then dropzone ajax call which is the PHP file. I'm thinking if after the upload line in PHP then the 2nd line is convert the uploaded DOC file to PDF is too fast so it is not converted? My AJAX returned result is: PDF not created. Command = /opt/libreoffice5.2/program/soffice --convert-to pdf /var/www/html/my_system /public/MSWord_testing_DOC.doc --outdir /var/www/html/my_system/storage/app/quotations=77 Commented Oct 17, 2016 at 0:53

1 Answer 1

0

Found the correct answer via https://superuser.com/questions/627266/convert-file-to-pdf-using-libreoffice-under-user-apache-i-e-when-using-php

Quoting Correct Answer:

So, you need to i) give apache's user a home and ii) give it a directory it has access to to write in. So, create a tmp directory in the same folder where you store your webpage and then run the following php code:

<?php
  shell_exec('export HOME=/tmp && libreoffice --headless -convert-to pdf --outdir ./tmp /tmp/ayb/document_34.doc');
?>

I just tested and it works perfectly on my machine. Make sure your ./tmp has its permissions set to 777. Also, you may need to restart apache if you play aroud with it too much. It stopped working for me after a while when I made changes and I needed to restart it.

Sign up to request clarification or add additional context in comments.

1 Comment

If you ever share this server consider Apache mpm-itk or php-fpm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.