ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

利用libreoffice转换word成pdf

2022-03-30 12:04:03  阅读:273  来源: 互联网

标签:12 word libreoffice -- 6.1 Linux pdf root


 

Linux系统,安装libreoffice 并在Linux下利用java调用 完成转换

  注:可以用  yum install libreoffice  -y

复制代码
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;

  }

 

1111

标签:12,word,libreoffice,--,6.1,Linux,pdf,root
来源: https://www.cnblogs.com/wq-9/p/16076294.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有