赞
踩
- 首先, 感谢一位大神的博客https://www.cnblogs.com/bb1008/p/10019576.html
-
- 官网下载 openoffice,下载后,启动,
- 进入安装路径下的openoffice安装目录内的program目录
- 进入cmd模式,执行 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
- 如果能够启动成功,那么java程序调用的时候大部分情况下也可以成功; 如果启动不起来,报错可能是缺少依赖包,根据报错提示,安装一下就好了。
之后,可以配置项目了
- pom 文件用引入相关依赖如下,之后,会有一部分包(两个包)找不到,如下面两个图,需要copy进去,之后,还需要在jar包的当前文件夹创建对应jar包的.pom文件,空文件即可,如果idea报错,在idea中,多刷新maven即可
-
- 准备完毕之后,粘贴代码,即可生成pdf了
-
- <dependency>
- <groupId>com.artofsolving</groupId>
- <artifactId>jodconverter</artifactId>
- <version>2.2.2</version>
- </dependency>
- <dependency>
- <groupId>org.openoffice</groupId>
- <artifactId>jurt</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.openoffice</groupId>
- <artifactId>ridl</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.openoffice</groupId>
- <artifactId>juh</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.openoffice</groupId>
- <artifactId>unoil</artifactId>
- <version>3.0.1</version>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core -->
- <dependency>
- <groupId>org.artofsolving.jodconverter</groupId>
- <artifactId>jodconverter-core</artifactId>
- <version>3.0-beta-4</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.8.0</version>
- </dependency>
package com.example.demo.controller; import org.apache.commons.io.FilenameUtils; import org.apache.log4j.Logger; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeException; import org.artofsolving.jodconverter.office.OfficeManager; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.File; /** * 如果报错连接错误,那么尝试一下注释操作,运行命令 * 首先手动启动一下openoffice,进入安装路径下的program目录 * 执行 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard * 如果能够启动成功,那么java程序调用的时候大部分情况下也可以成功; 如果启动不起来,报错可能是缺少依赖包,根据报错提示,安装一下就好了。 */ @RestController @RequestMapping(value = "demo_controller") public class demoController { private static final Logger logger = Logger.getLogger(demoController.class.getName()); @SuppressWarnings("static-access") private static String officeHome = "C:/Program Files (x86)/OpenOffice 4/"; //这里写的是你的openoffice的安装地址,如果你在安装openOffice 的时候选择的是默认安装,那么地址是:C:/Program Files (x86)/OpenOffice 4/。//如果是自定义的安装方式,请填写自定义安装路径。 @SuppressWarnings("static-access") private static int port = 8100;//这里的内容是根据你的系统选择不同的端口号,windows系统的端口号是8100 private static OfficeManager officeManager; private static String filePath = "C:/Users/hp/Desktop/"; private static boolean reconnect() { try { // 尝试连接openoffice的已存在的服务器 ExternalOfficeManagerConfiguration externalProcessOfficeManager; externalProcessOfficeManager = new ExternalOfficeManagerConfiguration(); externalProcessOfficeManager.setConnectOnStart(true); externalProcessOfficeManager.setPortNumber(8100); officeManager = externalProcessOfficeManager.buildOfficeManager(); officeManager.start(); return true; } catch (OfficeException e) { e.printStackTrace(); return false; } } // 开启新的openoffice的进程 private static void start() { logger.debug("启动OpenOffice服务"); try { DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); configuration.setOfficeHome(officeHome);// 安装地址 configuration.setPortNumbers(port);// 端口号 configuration.setTaskExecutionTimeout(1000 * 60 * 5);// 设置任务执行超时为5分钟 configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24);// 设置任务队列超时为24小时 officeManager = configuration.buildOfficeManager(); officeManager.start(); // 启动服务 } catch (Exception e) { logger.error("启动OpenOffice服务出错" + e); } } // 使用完需要关闭该进程 private static void stop() { logger.debug("关闭OpenOffice服务"); try { if (officeManager != null) { officeManager.stop(); } } catch (Exception e) { logger.error("关闭OpenOffice服务出错" + e); } } public static File convertToPdf(String input) { File inputFile = null; File outFile = null; try {// 如果已存在的服务不能连接或者不存在服务,那么开启新的服务 if (!reconnect()) { start();// 开启服务 } // filenameUtils是Apache对java io的封装。FilenameUtils.separatorsToSystem:转换分隔符为当前系统分隔符/ FilenameUtils.getFullPath:获取文件的完整目录// FilenameUtils.getBaseName:取出文件目录和后缀名的文件名 String output = FilenameUtils.separatorsToSystem(FilenameUtils.getFullPath(input) + FilenameUtils.getBaseName(input) + ".pdf"); inputFile = new File(input); outFile = new File(output); logger.info("开始转换文档:" + input + "=>" + output); OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); converter.convert(inputFile, outFile); // 转换文档 } catch (Exception e) { logger.error("转换文档出错" + e); outFile = null; } finally { logger.info("结束转换文档"); stop(); } return outFile; } private static String getPath(String fileName) { return filePath + fileName; } // 测试工具类是否成功 public static void main(String[] args) { File file = demoController.convertToPdf(getPath("openOffice使用文档.docx")); System.out.println(file.getPath()); /* File sf = new File("C:\\Users\\hp\\Desktop\\abc.doc"); System.out.println(sf.getPath());*/ } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。