一、libreOffice

与openOffice类似,但比openOffice稳定。

优点:样式稳定

缺点:性能较差

调用方式:windows:1

2

3

4

5

6

7

8

9

10

11

12

13

14

15public static String (String docPath){

String libreOfficePath = Global.getConfig("libreOffice");

if (!libreOfficePath.endsWith(File.separator))

libreOfficePath += File.separator;

//soffice --convert-to pdf -outdir E:/test.docx

String command = libreOfficePath + "soffice --convert-to pdf -outdir " + new File(docPath).getParent() + " " + docPath;

// 执行转换

String result = commandExecutor.executeCommand(command, EXECUTE_COMMAND_TIME_OUT).getExecuteOut();

logger.info(result);

docPath = docPath.replace(".docx", ".pdf");

return docPath;

}linux:1

2

3

4

5

6

7

8

9

10

11public static String (String docPath){

String libreOfficePath = Global.getConfig("libreOffice");

String command = libreOfficePath + " --invisible --convert-to pdf:writer_pdf_Export --outdir "

+ new File(docPath).getParent() + " " + docPath;

// 执行转换

String result = commandExecutor.executeCommand(command, EXECUTE_COMMAND_TIME_OUT).getExecuteOut();

logger.debug("转换结果:{}", result);

docPath = docPath.replace(".docx", ".pdf");

return docPath;

}

二、docx4j

优点:性能比libreoffice稍好

缺点:性能差、容易出现PDF和Word样式不一致问题1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39private static Mapper fontMapper = new IdentityPlusMapper();

// 初始化字体

static {

fontMapper.put("隶书", PhysicalFonts.get("LiSu"));

fontMapper.put("宋体", PhysicalFonts.get("SimSun"));

fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));

fontMapper.put("黑体", PhysicalFonts.get("SimHei"));

fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));

fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));

fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));

fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));

fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));

fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));

fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));

fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));

fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));

fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));

}

public static String docxToPdf(String docxPath){

OutputStream outputStream = null;

String pdfPath = docxPath.replace(".docx", ".pdf");

try {

WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(new File(docxPath));

mlPackage.setFontMapper(fontMapper);

outputStream = new BufferedOutputStream(new FileOutputStream(pdfPath));

FOSettings foSettings = Docx4J.createFOSettings();

foSettings.setWmlPackage(mlPackage);

Docx4J.toFO(foSettings, outputStream, Docx4J.FLAG_EXPORT_PREFER_XSL);

} catch (Exception ex) {

logger.error("docx转PDF失败!", ex);

pdfPath = null;

} finally {

IOUtils.closeQuietly(outputStream);

}

return pdfPath;

}

三、documents4j

优点:样式稳定、性能高

缺点:要依赖本地的office软件做转换,在linux下要调远程服务来转换

调用方式:1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37public static String docxToPdf(String docxPath){

String pdfPath = docxPath.replace(".docx", ".pdf");

boolean success = getConverter()

.convert(new File(docxPath))

.as(DocumentType.DOCX)

.to(new File(pdfPath))

.as(DocumentType.PDF).execute();

logger.debug("Word转换PDF结果:{}", success);

return success ? pdfPath : null;

}

public static IConverter getConverter(){

if (converter == null) {

String conversionServerUrl = Global.getConfig("conversionServer.url");

// 如果配置了远程转换服务器地址,则初始化远程转换对象

if (StringUtils.isNotBlank(conversionServerUrl)) {

if (!conversionServerUrl.startsWith("http"))

conversionServerUrl = "http://" + conversionServerUrl;

converter = RemoteConverter.builder()

.baseFolder(new File(POfficeConstants.TEMP_SAVE_PATH))

.workerPool(20, 25, 2, TimeUnit.SECONDS)

.requestTimeout(120, TimeUnit.SECONDS)

.baseUri(conversionServerUrl)

.build();

} else {

// 创建本地转换对象

converter = LocalConverter.builder()

.baseFolder(new File(POfficeConstants.TEMP_SAVE_PATH))

.workerPool(20, 25, 2, TimeUnit.SECONDS)

.processTimeout(2L, TimeUnit.MINUTES)

.build();

}

}

return converter;

}

四、jacob

优点:样式稳定、性能高

缺点:只支持window系统且服务器要安装office软件,并发量大时会有瓶颈

可以单独部署一台windows服务器,提供文档转换服务

基于spring boot的转换服务器例子:converter

五、pageOffice

优点:兼容性好,性能高

缺点:收费,客户端需要安装office软件和卓正控件,偶尔会出现兼容性问题

具体实现是客户的浏览器利用卓正控件打开Word文档,调用卓正提供的js接口,将文档保存为PDF,上传到服务器,服务器将上传的PDF做处理(如添加水印等)后提供给客户下载。

因为Word转PDF的过程是在客户的电脑上实现的,所以服务器基本没什么压力,但客户的电脑需要安装office软件和卓正控件。

总结

实际应用中,前面四种方案都用过,踩了不少坑,比如libreoffice,要考虑生产环境低内核版本问题,docx4j的转换后样式错乱问题,documents4j不稳定,会出现进程阻塞,jacob只支持window服务器

目前实际采用的是卓正的pageOffice。

java word转pdf_java里实现Word转PDF的几种方案相关推荐

  1. java中流转pdf_Java中的PDX到PDF转换器

    以下代码不适用于Apache POI 3.16. 有人能提供正确的解决方案吗?在我的项目中,有一些仅用于 public void ConvertToPDF(String docPath, String ...

  2. java ppt转pdf_JAVA如何把word,excel,ppt转成PDF,已经过测试成功。

    我写了两种方式,都是利用Openoffice工具实现转化的. 下面看一下转化程的代码. package com.g; import java.io.File; import java.io.FileN ...

  3. Java 中文api pdf_Java API中文完整版.pdf

    Java API中文完整版.pdf SunGuide®: Staffing Plan SunGuide-SP-5.0.0-Draft Prepared for: Florida Department ...

  4. java实现ppt/pptx转图片,转pdf的两种方式之一 poi

    poi的实现方式是分步实现的,并不能直接将ppt,pptx转为pdf. 首先是maven依赖 1.pom.xm需要引入的依赖 <!--poi依赖--><dependency>& ...

  5. word转html报错,word转html方法调研

    word转html方法调研 最近有需求可能用到word转html,所以前期调研了一番,整理如下. 问题描述 在不明确问题的情况下谈解决方案是不明智的,所以先明确问题: 现在线下有一批word文档,后台 ...

  6. Java读取word文档里的复杂型表格(任免表)

    使用apache-poi读取word文档里的复杂型表格 这里使用的任免表编辑器产生的word文档. word模板:https://download.csdn.net/download/weixin_4 ...

  7. java docx转pdf_java word/doc/docx文档转PDF 加水印

    本文实例讲述了java实现word文档转pdf并添加水印的方法.分享给大家供大家参考,具体如下: 前段时间,项目需要将上传的Word文档在浏览器浏览,思来想去,把word文档转成pdf就好了,于是乎研 ...

  8. Java:使用Java调用打印机进行打印(JPG、PDF和Word三种文件格式)

    目录 一.Java的打印简介 二.Java打印实现 2.1 JPG图片文件格式打印实现 2.2 PDF文件格式打印实现 2.3 Word文件格式打印实现 2.3.1 Word文件采用jacob插件进行 ...

  9. java poi word换行符_poi读取word的换行符问题

    用Java的Poi插件读取word内容,类如下: package com.tw.word; import org.apache.poi.hwpf.HWPFDocument; import org.ap ...

最新文章

  1. 《评人工智能如何走向新阶段》后记(再续7)
  2. Python模块-创建和执行程序(或者脚本)
  3. java23中设计模式——结构模式——Composite(组合)
  4. Java Comparator 珍藏版
  5. oracle中的一些基本概念
  6. docker 启动 springboot 项目
  7. php超链接_一个纯PHP库,用于读写文字处理文档
  8. [蓝桥杯][2014年第五届真题]生物芯片(数论)
  9. 用gensim doc2vec计算文本相似度,Python可以跑通的代码
  10. (转)C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
  11. Mac 使用brew 安装adb
  12. springboot 集成 fastdfs
  13. UserAgent个人整理
  14. html5 css3在线工具,HTML5/CSS3开发辅助工具(TopStyle)
  15. 发那科机器人控制柜示教器不通电_FANUC发那科机器人示教器A05B-2301-C305液晶屏维修...
  16. 基于Matlab的汽车主动悬架控制器设计与仿真
  17. 加餐3 | 考博和读博
  18. 前端Vue制作日历插件FullCalendar
  19. Python字典遍历顺序问题记录
  20. 爬虫实战-北京链家,安居客二手房的爬取

热门文章

  1. 解决:SpringBoot 搭建聚合项目 报 “程序包XXX不存在”
  2. Log4j2 杀不死 Java
  3. 微信自动抢红包软件被判赔 475 万;日本科学家打破网速全球纪录;JavaScript蝉联最受欢迎编程语言|极客头条...
  4. 特斯拉已在中国建立数据中心
  5. 10 个内存引发的大坑,你能躲开几个?
  6. iPhone 的倒计时竟然会显示假时间?
  7. 推特大规模攻击幕后黑手竟是 17 岁少年?企业云安全迫在眉睫!
  8. 数据说话!UCloud「硬刚」腾讯云,高性能 AMD 云主机哪家强?
  9. 明晚 8 点,为你揭秘「AI 换脸术」:剧照 or 视频换脸背后的核心技术与应用
  10. Google 是如何做 Code Review 的?| 原力计划