最近項目中遇到一個需求,將word、excel文件轉換成pdf,並且打上水印,我利用的是libreoffice,這個需要Java 的jdk環境.廢話不多說,開擼
1.在linux上搭建jdk環境
文章鏈接 https://www.cnblogs.com/houss/p/11694260.html
2.安裝libreoffice所需的包(官網)
我下載的鏡像下載網址
mkdir /usr/libreoffice
cd /usr/libreoffice
wget https://mirrors.tuna.tsinghua.edu.cn/libreoffice/libreoffice/stable/6.3.2/rpm/x86_64/LibreOffice_6.3.2_Linux_x86-64_rpm.tar.gz wget https://mirrors.tuna.tsinghua.edu.cn/libreoffice/libreoffice/stable/6.3.2/rpm/x86_64/LibreOffice_6.3.2_Linux_x86-64_rpm_sdk.tar.gz wget https://mirrors.tuna.tsinghua.edu.cn/libreoffice/libreoffice/stable/6.3.2/rpm/x86_64/LibreOffice_6.3.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz tar -zxvf LibreOffice_6.3.2_Linux_x86-64_rpm.tar.gz tar -zxvf LibreOffice_6.3.2_Linux_x86-64_rpm_sdk.tar.gz
tar -zxvf LibreOffice_6.3.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
cd /usr/libreoffice/LibreOffice_6.3.2.2_Linux_x86-64_rpm/RPMS
yum install *.rpm
cd /usr/libreoffice/LibreOffice_6.3.2.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS
yum install *.rpm
cd /usr/libreoffice/LibreOffice_6.3.2.2_Linux_x86-64_rpm_sdk/RPMS
yum install *.rpm
#測試是否安裝成功:
libreoffice6.3 -help
出現上圖證明安裝成功
報錯 :
報錯
/opt/libreoffice6.0/program/soffice.bin: error while loading shared libraries: libcairo.so.2: cannot open shared object file: No such file or directory
執行命令:
yum install cairo
后再次執行命令:
libreoffice6.3 -help
再次報錯
/opt/libreoffice6.0/program/soffice.bin: error while loading shared libraries: libcups.so.2: cannot open shared object file: No such file or directory
執行命令:
yum install cups-libs
后,再次執行命令:
libreoffice6.3 -help
報錯:
/opt/libreoffice6.0/program/soffice.bin: error while loading shared libraries: libSM.so.6: cannot open shared object file: No such file or directory
執行命令:
yum install libSM
轉換的命令
libreoffice6.3 --convert-to pdf:writer_pdf_Export 需轉換的文件的地址 --outdir 輸出地址
例如 將當前文件夾下 家長如何幫助孩子.pptx 轉換成pdf到當前文件夾下(word,excel也是如此,更多用法請自行研究)
libreoffice6.3 --convert-to pdf:writer_pdf_Export ./家長如何幫助孩子.pptx --outdir ./
PHP操作
<?php exec("libreoffice6.3 --convert-to pdf:writer_pdf_Export ./家長如何幫助孩子.pptx --outdir ./",$log,$status); if($status == 0){ echo '執行成功'; printf($status); }
關於word轉pdf中文亂碼問題處理
(1):查看fonts目錄:cat /etc/fonts/fonts.conf | grep fon
得知字體存放位置:/usr/share/fonts
(2): 把Windows下的字體C:\Windows\Fonts下的宋體,即simsun.ttc上傳到linux服務器
在fonts下新建Fonts文件 把字體上傳到該路徑下即可
參考地址 https://www.cnblogs.com/chenpt/p/11096265.html
3.pdf加水印,以及中文亂碼問題處理