LibreOffice是OpenOffice辦公套件衍生版, 同樣免費開源,並且是跨平台的,支持windows和linux服務器安裝。
php調用命令實現文檔轉換:
$retval = 1;
// exec() might be disabled
$cmd = 'D:/soft/LibreOfficePortablePrevious/LibreOfficeWriterPortable.exe --headless --convert-to pdf 1.doc --outdir ./';
if (function_exists('exec')){
@exec($cmd, $output, $retval);
}
// Did it work?
if ($retval > 0){
exit('process_failed');
}
echo 'success';
Centos下的安裝及使用
直接使用yum安裝即可:
yum install libreoffice
yum install libreoffice-headless //該工具用來啟動Libreoffice
2、執行命令轉換pdf文檔
跟windows類似,執行以下命令即可轉換文檔:
soffice --headless --convert-to pdf {文檔路徑} --outdir {導出目錄路徑}
3.解決中文不顯示,變方框的問題
在linux下轉換pdf,可能會出現中文字體不被識別,變成小方框的問題,解決步驟:
1.把Windows下的字體C:\Windows\Fonts下的常用字體,即simsun.ttc等復制到usr/share/fonts目錄下。
2 修改字體權限
sudo chmod 644 simsun.ttc
3. 更新字體緩存:
sudo fc-cache -fv
【殺死子進程命令】
ps -ef|grep soffice | awk '{print $2}' | xargs kill -9
【重啟命令】
/usr/lib64/libreoffice/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
【重啟成功截圖】
參考:http://www.884358.com/php-libreoffice-word-pdf/