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

pb3s4cty  于 6个月前  发布在  PHP
关注(0)|答案(1)|浏览(70)

我正试图转换.docx文件到.pdf使用libreoffice。
我的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);


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

cigdeys3

cigdeys31#

有关更多文档,请单击here

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

字符串

相关问题