Linux系統,安裝libreoffice 並在Linux下利用java調用 完成轉換
a.下載如下三個文件,可根據文件跟新程度選擇合適版本

網盤鏈接:https://pan.baidu.com/s/11YLixYjyUG66cCNbtblplg 提取碼:ljnw
b.將下載好的文件,放到服務器對應位置,文件夾libreoffice中(名字自己起)
[root libreoffice]# ll
-rw-r--r-- 1 root root 798421 Oct 25 12:13 LibreOffice_6.1.6_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
-rw-r--r-- 1 root root 36919386 Oct 25 12:24 LibreOffice_6.1.6_Linux_x86-64_rpm_sdk.tar.gz
-rw-r--r-- 1 root root 213845646 Oct 25 10:33 LibreOffice_6.1.6_Linux_x86-64_rpm.tar.gz
c.然后解壓文件 [root libreoffice]# tar -xvf xxx.tar.gz 將3個文件都解壓 得到如下文件:
-rw-r--r-- 1 root root 798421 Oct 25 12:13 LibreOffice_6.1.6_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
-rw-r--r-- 1 root root 36919386 Oct 25 12:24 LibreOffice_6.1.6_Linux_x86-64_rpm_sdk.tar.gz
-rw-r--r-- 1 root root 213845646 Oct 25 10:33 LibreOffice_6.1.6_Linux_x86-64_rpm.tar.gz
drwxr-xr-x 4 root root 4096 Jul 28 06:07 LibreOffice_6.1.6.2_Linux_x86-64_rpm
drwxr-xr-x 3 root root 4096 Jul 28 07:32 LibreOffice_6.1.6.2_Linux_x86-64_rpm_langpack_zh-CN
drwxr-xr-x 3 root root 4096 Jul 28 06:27 LibreOffice_6.1.6.2_Linux_x86-64_rpm_sdk
d.在解壓好的文件中 找到對應的目錄RPMS(3個文件夾都執行一次)
[root@VM_0_12_centos RPMS]# pwd /opt/libreoffice/LibreOffice_6.1.6.2_Linux_x86-64_rpm/RPMS [root@VM_0_12_centos RPMS]# yum localinstall *.rpm
e.測試是否解壓安裝完成
[root@VM_0_12_centos RPMS]# libreoffice6.1 -help(會得到很長一串信息------上邊安裝的是6.1版本)
f.安裝完成 使用
在文件夾中放入要轉換的文件
[root@VM_0_12_centos tmpFile]# ls tt.docx
執行:
[root@VM_0_12_centos tmpFile]# libreoffice6.0 --convert-to pdf:writer_pdf_Export ./tt.docx func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi
compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26 convert /root/tmpFile/tt.docx -> /root/tmpFile/tt.pdf using filter : writer_pdf_Export
得到:
[root@VM_0_12_centos tmpFile]# ls tt.docx tt.pdf
g.編寫java代碼完成轉換
主要代碼:
//定義map 綁定傳給前台參數
Map<String, Object> jsonMap = new HashMap<String, Object>();// 定義map
String localFi=SystemPath.getDocFolderPath()+"/"+foldName+"/"+fileName;//文件路徑
String outFileString=SystemPath.getDocFolderPath()+"/pdfDocument/";//輸出pdf文件路徑
String osName = System.getProperty("os.name");
long curtimeName = System.currentTimeMillis();
String pdfName =curtimeName+".pdf";//用當前毫秒值定義的文件名字
//利用cmd命令 使用libreoffice6.1轉換pdf文件
String cmd = "libreoffice6.1 --convert-to pdf:writer_pdf_Export " + localFi + " --outdir " + outFileString;
System.out.println(cmd);
try {
Process process = Runtime.getRuntime().exec(cmd);
try {
// 獲取返回狀態
int status = process.waitFor();
// 銷毀process
process.destroy();
process = null;
System.out.println("status -> " + status);
} catch (InterruptedException e) {
System.err.println(e.getMessage());
}
} catch (IOException e) {
System.err.println(e.getMessage());
}
//修改轉換后的pdf文件名,前端使用pdfjs展示pdf 文件名不能有中文 處理麻煩 改名即可
String pdfFile=outFileString+"/"+fileName.substring(0,fileName.lastIndexOf("."))+".pdf";//轉換后的文件地址
String finallUrl = outFileString+"/"+pdfName;//用當前毫秒值定義的文件名字
if(new File(pdfFile).isFile()){
File oldName = new File(pdfFile);
File newName = new File(finallUrl);
if(oldName.renameTo(newName)) {
System.out.println("已重命名");
}else {
System.out.println("重命名Error");
}
}
jsonMap.put("finallUrl",finallUrl);
jsonMap.put("filname", fileName);
jsonMap.put("pdfname", pdfName);
return jsonMap;
前台頁面展示:
//返回ajax調用的參數
success: function(data){
var getdata = eval(data);
var curUrls=getdata.finallUrl;
var fileName=getdata.filname;
var pdfname=getdata.pdfname;
var obj={
text:fileName
};
var url="${ctxStatic}/pdfjs/web/viewer.html?file=" + curUrls;
parent.parent.addTabInfo(obj,url);//可直接轉到連接 local.href="${ctxStatic}/pdfjs/web/viewer.html?file=" + curUrls;
}