javaWeb文档预览之office转pdf(附详细代码)

4 篇文章 0 订阅
3 篇文章 0 订阅

最近由于项目的需要一直在看文档转换的开源项目,包括officewebapp、OpenOffice、Libreoffice。后来发现officewebapp在各种在线文库的预览中比较常见,但是在实际的部署中需要的安装配置比较多,并且对系统有要求所以放弃。由于LibreOffice找到的参考资料比较少,最后选择了OpenOffice。下面就来详细介绍一下使用OpenOffice进行文档转换的开发过程:
1、OpenOffice下载安装:
下载地址:https://www.openoffice.org/download/index.html
我这里使用的是4.1.3版本的OpenOffice,下载完后是一个可执行程序,直接安装就好。
2、工程结构
开发环境:IDEA15+JDK1.8+Maven+SpringBoot

这里写图片描述
ConvertToPDF.java负责调用OpenOffice将Office转化成pdf文档
TestController.java负责调用转换的接口和之前PDF预览的相关接口
static包下的是pdf.js的相关包
3、需要依赖的Maven库

<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter-maven-plugin</artifactId>
    <version>2.2.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.aspose/aspose-words -->
  <dependency>
    <groupId>org.artofsolving.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>3.0-beta-4</version>
    <!--使用Maven导入不成功,这个库是手动导入的-->
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.openoffice/unoil -->
  <dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>unoil</artifactId>
    <version>3.2.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.openoffice/juh -->
  <dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>juh</artifactId>
    <version>3.2.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.openoffice/jurt -->
  <dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>jurt</artifactId>
    <version>3.2.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.openoffice/ridl -->
  <dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>ridl</artifactId>
    <version>3.2.1</version>
  </dependency>

4、相关代码
(1)OpenOffice配置

    private  OfficeManager officeManager;
    public static String OFFICE_HOME = "C:\\Program Files (x86)\\OpenOffice 4";//本机OpenOffice安装目录
    private int port = 8100;//默认使用端口

(2)启动服务:

 public  void startService(int i) {
        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
        try {
            //先关闭再启动
           // System.out.println("准备启动服务....");
           //设置OpenOffice.org安装目录
            configuration.setOfficeHome(OFFICE_HOME);
            //设置转换端口
            configuration.setPortNumbers(i); 
            //设置任务执行超时30分钟
            configuration.setTaskExecutionTimeout(1000 * 60 * 5L);
            //设置任务队列超时24小时
            configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
            officeManager = configuration.buildOfficeManager();
            officeManager.start();    //启动服务
            System.out.println("office转换服务启动成功!");
        } catch (Exception ce) {
            System.out.println("office转换服务启动失败!详细信息:" + ce);
        }
    }

(3)关闭服务:

public  void stopService() {
      //  System.out.println("关闭office转换服务....");
        if (officeManager != null) {
            officeManager.stop();
        }
        System.out.println("关闭office转换成功!");
   }

(4)文档转换

 public  void orgconvert2PDF(String inputFile) {
        //pdfFile是目标文件  odtFile是中间文件
        String pdfFile = inputFile.substring(0, inputFile.lastIndexOf("."))+"_" + System.currentTimeMillis() + ".pdf";
        String odtFile = inputFile.substring(0, inputFile.lastIndexOf("."))+"_" + System.currentTimeMillis() + ".odt";
        File odt = new File(odtFile);
        if (odt.exists()) {
            System.out.println("odt文件已存在!");
            inputFile = odtFile;
        } else {
            try {
                File input = new File(inputFile);
                FileUtils.copyFile(input, odt);
                inputFile = odtFile;
            } catch (Exception e) {
                System.out.println("文档不存在!");
                e.printStackTrace();
            }
        }
        try {
            long now = System.currentTimeMillis();
            startService(port);
            System.out.println( "  进行文档转换转换:" + inputFile + " --> " + pdfFile);
            OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
            converter.convert(new File(inputFile), new File(pdfFile));
            //转化完成关闭服务
            stopService();
            odt.delete();
            long old = System.currentTimeMillis();
            System.out.println("ConvertSuccess Start = "+ old + ", end = "+ now +  ", "+ " UsedTime = " + (old - now) / 1000.0 + " S");
            //System.out.println(" 文档转换成功....end");
        } catch (Exception e) {
            stopService();
            System.out.println(  "文档转换失败....error");
        }
    }

(5)多线程调用

        String str1 = "D:\\TestFiles\\1\\1.docx";
        String str2 = "D:\\TestFiles\\1\\2.docx";
        //多线程调用
        ConvertToPDF convert1 = new ConvertToPDF(str1,8100);
        Thread thread1 = new Thread( convert1 );
        thread1.start();
        ConvertToPDF convert2 = new ConvertToPDF(str2,8101);
        Thread thread2 = new Thread( convert2 );
        thread2.start();

在jodconverter3.0-beta-4里已经支持启动多个OpenOffice实例进行转码,这里的多线程是通过同时在多个线程里面启动多个不同端口的OpenOfiice实例进行转码实现。如果是一个端口,也可以进行多文件的转化,只是在添加多个文件的时候,OpenOffice会将任务文件添加到任务队列依次执行。

文档预览之PDF.js实现PDF文件跨域预览(附测试实例):
http://blog.csdn.net/coding13/article/details/76930672
文档预览之OpenOfiice踩过的坑:
http://blog.csdn.net/coding13/article/details/77246339

参考博文
https://segmentfault.com/a/1190000006789644(Java中常用的几种DOCX转PDF方法,包含Libreoffice的使用)
代码下载:
http://download.csdn.net/download/coding13/9936039

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding13

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值