PHP Laravel LibreOffice DOC 到 PDF 无法通过浏览器转换 运行

PHP Laravel LibreOffice DOC to PDF not converting via browser run

我目前正在通过 DropzoneJS 上传 DOC 或 DOCX 文件,然后使用 LibreOffice PHP exec 在服务器中将其转换为 PDF。如果我在服务器中手动输入它然后它会转换但是如果我 运行 它通过浏览器然后它不会转换。我正在尝试 运行 没有 sudo 的办公室,但是通过浏览器的命令 运行 不起作用。如何通过浏览器转换它运行?

终端中的手动命令:

/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

命令 ls -liah 文件夹结果:

my_system、存储、应用程序、报价单 = drwxrwxrwx apache apache

我的 sudo visudo 结果(粘贴的行没有顺序注释):

Defaults    requiretty
Defaults   !visiblepw
Defaults    always_set_home

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

PHP 文件:

$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;
}

谢谢。

通过 https://superuser.com/questions/627266/convert-file-to-pdf-using-libreoffice-under-user-apache-i-e-when-using-php

找到了正确答案

引用正确答案:

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.