一、 安装及启动服务
百度网盘:链接: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();
}
}
}