Java程序调用libreoffice转换pdf

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;

  }

 

 

posted @ 2020-08-26 14:45  景、  阅读(3855)  评论(0编辑  收藏  举报