ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java代码实现WORD转PDF

2019-05-21 08:54:59  阅读:1435  来源: 互联网

标签:PDF outputFile Java org new openoffice WORD import com


第一步:

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

标签:PDF,outputFile,Java,org,new,openoffice,WORD,import,com
来源: https://www.cnblogs.com/xiaoshen666/p/10897752.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有