springboot集成openoffice实现office转PDF在线预览

一、 安装及启动服务
百度网盘:链接:https://pan.baidu.com/s/1j_VZPs4auoCpzpRA-UV-WQ
提取码:aomm

进入openoffice安装目录:/program/
开启服务:cmd命令

soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
二、pom文件
注:2.2.2版本以下不支持docx等文件类型装换
2.2.2:链接同上

 		<dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>4.1.2</version>
        </dependency>

三、yml配置

jodconverter:
local:
office-home: 127.0.0.1
port-numbers: 8100
在这里插入图片描述

四、工具类

packagecom.tsll.smart.common.openoffice;

importcom.artofsolving.jodconverter.DocumentConverter;
importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
importcom.tsll.smart.config.OpenOfficeConfig;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;

importjava.io.File;
importjava.net.ConnectException;

/**
*持txtdocxpptxxlsxdocxlsppt转pdf
*@authorys
*/
@Component
@Slf4j
publicclassOffice4Pdf{
@Autowired
privateOpenOfficeConfigopenOfficeConfig;

publicFileOffice2Pdf(Filefile,FileoutputFile){
//判断源文件是否存在
if(!file.exists()){
thrownewRuntimeException("源文件不存在!");
}

//创建连接
OpenOfficeConnectionconnection=newSocketOpenOfficeConnection(openOfficeConfig.getOfficehome(),openOfficeConfig.getPortnumbers());
try{

//远程连接OpenOffice服务
connection.connect();

//创建文件转换器
DocumentConverterconverter=newStreamOpenOfficeDocumentConverter(connection);

//开始转换
converter.convert(file,outputFile);
if(outputFile.exists()){
log.info(file.getName()+"文件转换成功");
}else{
log.info(file.getName()+"文件转换失败");
}
}catch(ConnectExceptione){
e.printStackTrace();
log.info("OpenOffice服务启动失败");
}
returnoutputFile;
}

}

五、测试类

packagecom.tsll.smart.controller.erp;

importcom.tsll.smart.common.openoffice.Office4Pdf;
importcom.tsll.smart.common.utils.file.FileUtils;
importio.swagger.annotations.Api;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.util.FileCopyUtils;
importorg.springframework.web.bind.annotation.CrossOrigin;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.bind.annotation.RestController;

importjavax.servlet.http.HttpServletResponse;
importjava.io.File;

@Slf4j
@CrossOrigin(allowCredentials="true",allowedHeaders="*",origins="*",maxAge=3600)
@Api(description="在线预览",tags={"在线预览"})
@RestController
@RequestMapping("/Online")
publicclassTestController{

@Autowired
privateOffice4Pdfoffice4Pdf;

@ResponseBody
@RequestMapping("testPreview")
publicvoidtoPdfFile(HttpServletResponseresponse){
Stringwordpath="D:/download/b887d7bdcb614ff6b400074ca128142b.docx";
Stringpdfpath="D:/download/";
//需要装换的文件
Filefile=newFile(wordpath);
try{

//装换后的文件地址
Filenewfile=newFile(pdfpath);
if(!newfile.exists()){
newfile.mkdirs();
}
//输出文件
FileoutputFile=newFile(pdfpath+file.getName().substring(0,file.getName().lastIndexOf("."))+".pdf");

Filefile1=office4Pdf.Office2Pdf(file,outputFile);
//FileCopyUtils.copy(FileUtils.fileConvertToByteArray(file1),newFile(pdfpath+file1.getName()));
}catch(Exceptione){
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值