当前位置:   article > 正文

java使用openoffice/libreoffice进行office转pdf_openoffice 升级为libreoffice pdf行间距

openoffice 升级为libreoffice pdf行间距

    OpenOffice.org 是一套跨平台的办公室软件套件,能在 Windows、Linux、MacOS X (X11)、和 Solaris 等操作系统上执行。

它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用、及推广它。官方文档:http://www.openoffice.org/zh-cn/

    LibreOffice 是一款功能强大的办公软件,默认使用开放文档格式 (OpenDocument Format , ODF), 并支持 *.docx, *.xlsx, *.pptx 等其他格式。
它包含了 Writer, Calc, Impress, Draw, Base 以及 Math 等组件,可用于处理文本文档、电子表格、演示文稿、绘图以及公式编辑。官方文档:https://zh-cn.libreoffice.org/

    JODConverter,是一个Java的OpenDocument文件转换器,可以进行许多文件格式的转换。它依赖于OpenOffice.org或者LibreOffice提供的服务来进行转换,它能将Microsoft Office文档(Word,Excel,PowerPoint)转换为PDF格式。

你可以将JODConverter内嵌在Java应用程序里,也可以单独作为命令行由脚本调用,更可以应用为网页程序或者Web Service以供网络应用。下载地址:https://sourceforge.net/projects/jodconverter/files/JODConverter/

 

office转换pdf

启动OpenOffice/LibreOffice的服务(本是同根生,启动服务方法一样;转换pdf都使用JODConverter,切换服务可以不用改代码)

进入openoffice/libreoffice安装目录,通过cmd启动一个soffice服务,启动的命令是soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

  1. package com.feng.util;
  2. import com.artofsolving.jodconverter.DocumentConverter;
  3. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  4. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
  5. import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
  6. import com.feng.bean.ResultCode;
  7. import com.feng.date.DateTools;
  8. import com.feng.error.MyException;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.artofsolving.jodconverter.OfficeDocumentConverter;
  11. import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
  12. import org.artofsolving.jodconverter.office.OfficeManager;
  13. import java.io.File;
  14. import java.util.regex.Pattern;
  15. /**
  16. * OpenOffice相关操作
  17. * Created by feng on 2020/10/22.
  18. */
  19. @Slf4j
  20. public class OpenOfficeUtil {
  21. /**
  22. * office转pdf (手动启服务soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;")
  23. *
  24. * @param sourceFile 源文件 office
  25. * @param destFile 目标文件 pdf
  26. * @return
  27. */
  28. public static void officeToPDF(String sourceFile, String destFile, String ip, Integer port) {
  29. try {
  30. File inputFile = new File(sourceFile);
  31. if (!inputFile.exists()) {
  32. // 找不到源文件, 则返回
  33. return;
  34. }
  35. // 如果目标路径不存在, 则新建该路径
  36. File outputFile = new File(destFile);
  37. if (!outputFile.getParentFile().exists()) {
  38. outputFile.getParentFile().mkdirs();
  39. }
  40. //如果目标文件存在,则删除
  41. if (outputFile.exists()) {
  42. outputFile.delete();
  43. }
  44. OpenOfficeConnection connection = new SocketOpenOfficeConnection(ip, port);//"127.0.0.1", 8100
  45. connection.connect();
  46. log.info("OpenOffice连接时间:" + DateTools.now());
  47. long t1 = System.currentTimeMillis();
  48. DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
  49. connection);
  50. converter.convert(inputFile, outputFile);
  51. long t2 = System.currentTimeMillis();
  52. log.info("OpenOffice转换完成时间:{},耗时:{}ms", DateTools.now(), t2 - t1);
  53. connection.disconnect();
  54. } catch (Exception e) {
  55. log.error("officeToPDF错误:", e);
  56. throw new MyException(ResultCode.FAILURE, "officeToPDF错误");
  57. }
  58. }
  59. /**
  60. * 将Office文档转换为PDF. 需要安装OpenOffice (自动启服务)
  61. *
  62. * @param inputFilePath 源文件,绝对路径. 可以是Office2003-2007全部格式的文档, 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx等.
  63. * @param outputFilePath 目标文件.绝对路径.
  64. */
  65. public static void office2pdf(String inputFilePath, String outputFilePath) {
  66. DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
  67. String officeHome = getOfficeHome();
  68. //设置OpenOffice.org安装目录
  69. config.setOfficeHome(officeHome);
  70. //设置转换端口,默认为8100
  71. config.setPortNumbers(8100);
  72. //设置任务执行超时为60分钟
  73. config.setTaskExecutionTimeout(1000 * 60 * 60L);
  74. //设置任务队列超时为24小时
  75. config.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
  76. OfficeManager officeManager = config.buildOfficeManager();
  77. officeManager.start();
  78. log.info("office转换服务启动成功!{}", DateTools.now());
  79. OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
  80. File inputFile = new File(inputFilePath);
  81. if (inputFile.exists()) {// 找不到源文件, 则返回
  82. File outputFile = new File(outputFilePath);
  83. if (!outputFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径
  84. outputFile.getParentFile().mkdirs();
  85. }
  86. converter.convert(inputFile, outputFile);
  87. }
  88. if (null != officeManager) {
  89. officeManager.stop();
  90. log.info("office转换服务完成。{}", DateTools.now());
  91. }
  92. }
  93. /**
  94. * 获取OpenOffice安装目录
  95. *
  96. * @return
  97. */
  98. public static String getOfficeHome() {
  99. String osName = System.getProperty("os.name");
  100. if (Pattern.matches("Linux.*", osName)) {
  101. return "/opt/openoffice";
  102. } else if (Pattern.matches("Windows.*", osName)) {
  103. return "C:/Program Files (x86)/OpenOffice 4";
  104. } else if (Pattern.matches("Mac.*", osName)) {
  105. return "/Application/OpenOffice.org.app/Contents";
  106. }
  107. return null;
  108. }
  109. public static void main(String[] args) {
  110. officeToPDF("d:/tmp/1.docx", "d:/tmp/1.pdf");
  111. // office2pdf("d:/tmp/1.docx", "d:/tmp/1.pdf");
  112. }
  113. }

office转换html

只需要将后缀名从.pdf改为.html即可。

  1. public static void main(String[] args) {
  2. officeToPDF("d:/tmp/1.docx", "d:/tmp/1.html");
  3. // office2pdf("d:/tmp/1.docx", "d:/tmp/1.html");
  4. }

maven配置

  1. <dependency>
  2. <groupId>com.artofsolving</groupId>
  3. <artifactId>jodconverter</artifactId>
  4. <version>2.2.1</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. <dependency>
  27. <groupId>org.slf4j</groupId>
  28. <artifactId>slf4j-jdk14</artifactId>
  29. <version>1.4.3</version>
  30. </dependency>

Maven中只有 2.2.1版本,2.2.1版本有一个问题,那就是不兼容docx和pptx,想要使用2.2.1版本,需要修改一下 BasicDocumentFormatRegistry 类中的 getFormatByFileExtension方法:

  1. 1、新建包 com.artofsolving.jodconverter
  2. 2、新建类BasicDocumentFormatRegistry,复制下面代码
  3. package com.artofsolving.jodconverter;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
  8. private List documentFormats = new ArrayList();
  9. public BasicDocumentFormatRegistry() {
  10. }
  11. public void addDocumentFormat(DocumentFormat documentFormat) {
  12. this.documentFormats.add(documentFormat);
  13. }
  14. protected List getDocumentFormats() {
  15. return this.documentFormats;
  16. }
  17. public DocumentFormat getFormatByFileExtension(String extension) {
  18. if (extension == null) {
  19. return null;
  20. } else {
  21. if (extension.indexOf("doc") >= 0) {
  22. extension = "doc";
  23. }
  24. if (extension.indexOf("ppt") >= 0) {
  25. extension = "ppt";
  26. }
  27. if (extension.indexOf("xls") >= 0) {
  28. extension = "xls";
  29. }
  30. String lowerExtension = extension.toLowerCase();
  31. Iterator it = this.documentFormats.iterator();
  32. DocumentFormat format;
  33. do {
  34. if (!it.hasNext()) {
  35. return null;
  36. }
  37. format = (DocumentFormat)it.next();
  38. } while(!format.getFileExtension().equals(lowerExtension));
  39. return format;
  40. }
  41. }
  42. public DocumentFormat getFormatByMimeType(String mimeType) {
  43. Iterator it = this.documentFormats.iterator();
  44. DocumentFormat format;
  45. do {
  46. if (!it.hasNext()) {
  47. return null;
  48. }
  49. format = (DocumentFormat)it.next();
  50. } while(!format.getMimeType().equals(mimeType));
  51. return format;
  52. }
  53. }

最后我使用了LibreOffice;先用了OpenOffice,发现ppt转换pdf会出现文字颜色不对的情况,白色变黑色,黑色变白色,其他颜色也成了黑白;用LibreOffice文字样式正常,只是一些绘图不正常,文档优先保证文字正常,所以最后用了LibreOffice。

excel转pdf会出现折行问题,请移步:解决excel转pdf出现的折行问题

安装及其他问题可看更多资料。

更多资料:

JODConverter 简单介绍

Java使用Openoffice将word、ppt转换为PDF

记录libreoffice实现office转pdf(适用于windows、linux)

【libreoffice】libreoffice实现office转pdf、html、jpg等格式数据

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/825739
推荐阅读
相关标签
  

闽ICP备14008679号