当前位置:   article > 正文

openoffice 转换pdf_openoffice4 集成转pdf

openoffice4 集成转pdf
  1. 首先, 感谢一位大神的博客https://www.cnblogs.com/bb1008/p/10019576.html
  2. 官网下载 openoffice,下载后,启动,
  1. 进入安装路径下的openoffice安装目录内的program目录
  2. 进入cmd模式,执行 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
  3. 如果能够启动成功,那么java程序调用的时候大部分情况下也可以成功; 如果启动不起来,报错可能是缺少依赖包,根据报错提示,安装一下就好了。

之后,可以配置项目了

  1. pom 文件用引入相关依赖如下,之后,会有一部分包(两个包)找不到,如下面两个图,需要copy进去,之后,还需要在jar包的当前文件夹创建对应jar包的.pom文件,空文件即可,如果idea报错,在idea中,多刷新maven即可
  2. 准备完毕之后,粘贴代码,即可生成pdf了

 

  1. <dependency>
  2. <groupId>com.artofsolving</groupId>
  3. <artifactId>jodconverter</artifactId>
  4. <version>2.2.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.openoffice</groupId>
  8. <artifactId>jurt</artifactId>
  9. <version>3.0.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.openoffice</groupId>
  13. <artifactId>ridl</artifactId>
  14. <version>3.0.1</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.openoffice</groupId>
  18. <artifactId>juh</artifactId>
  19. <version>3.0.1</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.openoffice</groupId>
  23. <artifactId>unoil</artifactId>
  24. <version>3.0.1</version>
  25. </dependency>
  26. <!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core -->
  27. <dependency>
  28. <groupId>org.artofsolving.jodconverter</groupId>
  29. <artifactId>jodconverter-core</artifactId>
  30. <version>3.0-beta-4</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>log4j</groupId>
  34. <artifactId>log4j</artifactId>
  35. <version>1.2.17</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>commons-io</groupId>
  39. <artifactId>commons-io</artifactId>
  40. <version>2.8.0</version>
  41. </dependency>

 

 

 


  1. package com.example.demo.controller;
  2. import org.apache.commons.io.FilenameUtils;
  3. import org.apache.log4j.Logger;
  4. import org.artofsolving.jodconverter.OfficeDocumentConverter;
  5. import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
  6. import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;
  7. import org.artofsolving.jodconverter.office.OfficeException;
  8. import org.artofsolving.jodconverter.office.OfficeManager;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import java.io.File;
  12. /**
  13. * 如果报错连接错误,那么尝试一下注释操作,运行命令
  14. * 首先手动启动一下openoffice,进入安装路径下的program目录
  15. * 执行 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
  16. * 如果能够启动成功,那么java程序调用的时候大部分情况下也可以成功; 如果启动不起来,报错可能是缺少依赖包,根据报错提示,安装一下就好了。
  17. */
  18. @RestController
  19. @RequestMapping(value = "demo_controller")
  20. public class demoController {
  21. private static final Logger logger = Logger.getLogger(demoController.class.getName());
  22. @SuppressWarnings("static-access")
  23. private static String officeHome = "C:/Program Files (x86)/OpenOffice 4/"; //这里写的是你的openoffice的安装地址,如果你在安装openOffice 的时候选择的是默认安装,那么地址是:C:/Program Files (x86)/OpenOffice 4/。//如果是自定义的安装方式,请填写自定义安装路径。
  24. @SuppressWarnings("static-access")
  25. private static int port = 8100;//这里的内容是根据你的系统选择不同的端口号,windows系统的端口号是8100
  26. private static OfficeManager officeManager;
  27. private static String filePath = "C:/Users/hp/Desktop/";
  28. private static boolean reconnect() {
  29. try {
  30. // 尝试连接openoffice的已存在的服务器
  31. ExternalOfficeManagerConfiguration externalProcessOfficeManager;
  32. externalProcessOfficeManager = new ExternalOfficeManagerConfiguration();
  33. externalProcessOfficeManager.setConnectOnStart(true);
  34. externalProcessOfficeManager.setPortNumber(8100);
  35. officeManager = externalProcessOfficeManager.buildOfficeManager();
  36. officeManager.start();
  37. return true;
  38. } catch (OfficeException e) {
  39. e.printStackTrace();
  40. return false;
  41. }
  42. }
  43. // 开启新的openoffice的进程
  44. private static void start() {
  45. logger.debug("启动OpenOffice服务");
  46. try {
  47. DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
  48. configuration.setOfficeHome(officeHome);// 安装地址
  49. configuration.setPortNumbers(port);// 端口号
  50. configuration.setTaskExecutionTimeout(1000 * 60 * 5);// 设置任务执行超时为5分钟
  51. configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24);// 设置任务队列超时为24小时
  52. officeManager = configuration.buildOfficeManager();
  53. officeManager.start(); // 启动服务
  54. } catch (Exception e) {
  55. logger.error("启动OpenOffice服务出错" + e);
  56. }
  57. }
  58. // 使用完需要关闭该进程
  59. private static void stop() {
  60. logger.debug("关闭OpenOffice服务");
  61. try {
  62. if (officeManager != null) {
  63. officeManager.stop();
  64. }
  65. } catch (Exception e) {
  66. logger.error("关闭OpenOffice服务出错" + e);
  67. }
  68. }
  69. public static File convertToPdf(String input) {
  70. File inputFile = null;
  71. File outFile = null;
  72. try {// 如果已存在的服务不能连接或者不存在服务,那么开启新的服务
  73. if (!reconnect()) {
  74. start();// 开启服务
  75. }
  76. // filenameUtils是Apache对java io的封装。FilenameUtils.separatorsToSystem:转换分隔符为当前系统分隔符/ FilenameUtils.getFullPath:获取文件的完整目录// FilenameUtils.getBaseName:取出文件目录和后缀名的文件名
  77. String output = FilenameUtils.separatorsToSystem(FilenameUtils.getFullPath(input) + FilenameUtils.getBaseName(input) + ".pdf");
  78. inputFile = new File(input);
  79. outFile = new File(output);
  80. logger.info("开始转换文档:" + input + "=>" + output);
  81. OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
  82. converter.convert(inputFile, outFile); // 转换文档
  83. } catch (Exception e) {
  84. logger.error("转换文档出错" + e);
  85. outFile = null;
  86. } finally {
  87. logger.info("结束转换文档");
  88. stop();
  89. }
  90. return outFile;
  91. }
  92. private static String getPath(String fileName) {
  93. return filePath + fileName;
  94. }
  95. // 测试工具类是否成功
  96. public static void main(String[] args) {
  97. File file = demoController.convertToPdf(getPath("openOffice使用文档.docx"));
  98. System.out.println(file.getPath());
  99. /*
  100. File sf = new File("C:\\Users\\hp\\Desktop\\abc.doc");
  101. System.out.println(sf.getPath());*/
  102. }
  103. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/825766
推荐阅读
  

闽ICP备14008679号