在代码执行前,一定要确保可以正常访问LibreOffice Service,并启动了LibreOffice服务。同时,需要确保PHP安装了LibreOffice相关依赖,比如LibreOffice PHP SDK和LibreOffice核心组件。如果以上步骤都正确无误,可以在代码中增加如下内容:
putenv('UNO_PATH=/usr/lib/libreoffice/program'); //指定LibreOffice安装目录路径
putenv('LD_LIBRARY_PATH=/usr/lib/libreoffice/program'); //指定LibreOffice库文件路径
$localSocket = getenv('HOME') . '/.config/libreoffice/4/user/uno_socket';
putenv("UNO_PATH=$libreofficePath");
putenv("SOFFICE_BIN=$sofficeBinPath");
putenv("SOFFICE_CONVERT_FILTER=Microsoft Word 97/2000/XP/2003|MS Word 2007 XML|Adobe PDF|Text");
$inputFileName = "input.docx"; //需要转换的文件名
$outputFileName = "converted.pdf";//转换后的文件名
exec("display $inputFileName");
exec("libreoffice --headless --nologo --nodefault --nofirststartwizard --invisible --convert-to pdf $inputFileName --outdir $(dirname $inputFileName)", $output, $return_var);
exec("display $outputFileName");
以上代码会启动LibreOffice服务并实现将.docx转换为.pdf的功能。如果还是无法解决,可以检查一下LibreOffice依赖是否正确安装。