使用 php 通过 libreoffice 将 .docx 转换为 pdf

问题描述 投票:0回答:1

我正在尝试使用 libreoffice 将 .docx 文件转换为 .pdf。

我的libreoffice安装目录是

C:\Program Files\LibreOffice 4

使用命令行我得到输出

C:\Program Files\LibreOffice 4\program>soffice.exe -headless -convert-to pdf c:\temp\test.docx -outdir C:\temp

但是在 php 中没有输出

$command = '"C:\Program Files\LibreOffice 4\program\soffice.exe" -headless -convert-to pdf c:\temp\test.docx -outdir C:\temp';
exec($command, $output);

如何解决这个问题?提前致谢。

php pdf docx libreoffice
1个回答
0
投票

欲了解更多文档,请点击此处

    function convert_any_file_to_pdf($file) {
        $file_path =  $file->getRealPath();
        $outputPdfPath = storage_path('app/public/pdf_save_dir_name_here');

        $output=null;
        $retval=null;

            // LibreOffice command to convert DOCX to PDF
            $command = "libreoffice --headless --convert-to pdf --outdir {$outputPdfPath} {$file_path}";

            $status = exec($command,$output,$retval);

            if($retval === 0){    
                return ['message' => 'success'];
            }
            else{
                return ['message' => 'failed'];
            }
    }

    convert_any_file_to_pdf('example.docx'); // callback function
© www.soinside.com 2019 - 2024. All rights reserved.