zoukankan      html  css  js  c++  java
  • Java代码实现WORD转PDF

    第一步:

    安装OpenOffice   在此良心提供windows版本安装文件

    链接:https://pan.baidu.com/s/17pPCkcS1C46VtLhevqSgPw  密码:vmlu

    安装就一直点下一步即可。

    安装完成后,进入OpenOffice安装目录

    安装目录一般为C:ProgramFiles(x86)/OpenOffice 4/program/

    执行下面命令:

    soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

    第二步:

    JAVA代码实现

    这里在SpringBoot基础上写了个接口,可以直接通过项目调用

    package com.test.controller;
     
    import com.artofsolving.jodconverter.DocumentConverter;
    import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
     
    import java.io.File;
    import java.net.ConnectException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    /**
     * Created by jlm on 2019-05-07 11:46
     */
    @RestController
    @RequestMapping("/test")
    public class WordToPdfController {
     
        @RequestMapping(value = "toPdf.do", method = RequestMethod.POST)
        public boolean pauseToDeal(String sourceFile, String destFile) {
            try {
                File inputFile = new File(sourceFile);
                if (!inputFile.exists()) {
                    // 找不到源文件, 则返回false
                    return false;
                }
                // 如果目标路径不存在, 则新建该路径
                File outputFile = new File(destFile);
                if (!outputFile.getParentFile().exists()) {
                    outputFile.getParentFile().mkdirs();
                }
                //如果目标文件存在,则删除
                if (outputFile.exists()) {
                    outputFile.delete();
                }
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
                connection.connect();
                //用于测试openOffice连接时间
                System.out.println("连接时间:" + df.format(new Date()));
                DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
                        connection);
                converter.convert(inputFile, outputFile);
                //测试word转PDF的转换时间
                System.out.println("转换时间:" + df.format(new Date()));
                connection.disconnect();
                return true;
            } catch (ConnectException e) {
                e.printStackTrace();
                System.err.println("openOffice连接失败!请检查IP,端口");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
     
    }

    Maven POM文件配置

    <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>

    此方法适用于windos服务器下的项目,如需linux,请安装liunx版本OpenOffice

    原文:https://blog.csdn.net/Rice_kil/article/details/89921849#10006-weixin-1-52626-6b3bffd01fdde4900130bc5a2751b6d1

  • 相关阅读:
    模拟赛12-10
    uva-11235
    中国剩余定理
    待学算法
    A
    动态逆序对
    [BZOJ3011][Usaco2012 Dec]Running Away From the Barn
    题目1007:奥运排序问题(结构体排序)
    题目1006:ZOJ问题(字符串处理)
    题目1005:Graduate Admission(结构体排序)
  • 原文地址:https://www.cnblogs.com/xiaoshen666/p/10897752.html
Copyright © 2011-2022 走看看