java代码转pdf_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文件配置

com.artofsolving

jodconverter

2.2.2

org.openoffice

jurt

3.0.1

org.openoffice

ridl

3.0.1

org.openoffice

juh

3.0.1

org.openoffice

unoil

3.0.1

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

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

来源: https://www.cnblogs.com/xiaoshen666/p/10897752.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值