启动LibreOffice服务
soffice --headless --accept="socket,host=0.0.0.0,port=8100;urp;" --nofirststartwizard &
测试能否转换成功(文件夹下面有个这个word文档)
soffice --invisible --convert-to pdf 导出模板.docx
执行完之后文件夹下面出现一个同名的pdf文件即可说明服务启动
<!--libreoffice文档在线预览-->
<jodconverter.version>4.2.0</jodconverter.version>
<libreoffice.version>6.4.3</libreoffice.version>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>${jodconverter.version}</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>${jodconverter.version}</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>${jodconverter.version}</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>ridl</artifactId>
<version>${libreoffice.version}</version>
</dependency>
3.2 yml配置文件中配置
jodconverter:
local:
#暂时关闭预览,启动时会有点慢
enabled: true
#设置libreoffice主目录 linux地址如:/usr/lib64/libreoffice
# office-home: /usr/lib64/libreoffice
office-home: C:/Program Files/LibreOffice
#开启多个libreoffice进程,每个端口对应一个进程
port-numbers: 8100
#libreoffice进程重启前的最大进程数
max-tasks-per-process: 100
3.3 转换工具类
/*
Copyright [2020] [https://www.xiaonuo.vip]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
*/
package vip.xiaonuo.core.util;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.log.Log;
import org.jodconverter.DocumentConverter;
import org.jodconverter.document.DocumentFormat;
import org.jodconverter.office.OfficeException;
import org.springframework.http.MediaType;
import vip.xiaonuo.core.consts.MediaTypeConstant;
import vip.xiaonuo.core.enums.DocumentFormatEnum;
import vip.xiaonuo.core.exception.LibreOfficeException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* LibreOffice工具类,用于将word,excel,ppt等格式文件转为pdf预览
*
* @author xuyuxiang
* @date 2020/7/6 14:55
*/
public class LibreOfficeUtil {
private static final Log log = Log.get();
private static DocumentConverter documentConverter;
private static void init() {
try {
documentConverter = SpringUtil.getBean(DocumentConverter.class);
} catch (Exception e) {
throw new LibreOfficeException();
}
}
/**
* 将文件流转换为PDF流
*
* @param inputStream:输入流
* @param outputStream:输入pdf流
* @param fileSuffix:源文件后缀
* @return 目标类型的contentType
* @author xuyuxiang
* @date 2020/7/6 15:02
*/
public static void convertToPdf(InputStream inputStream, OutputStream outputStream, String fileSuffix) {
if (!MediaTypeConstant.DOC_PDF.equals(fileSuffix)) {
init();
final DocumentFormatEnum documentFormatEnum = DocumentFormatEnum.valueOf(fileSuffix.toUpperCase());
final DocumentFormat format = documentFormatEnum.getFormFormat();
log.info(">>> 待转换的文档类型:{}", format);
final DocumentFormat targetFormat = documentFormatEnum.getTargetFormat();
log.info(">>> 转换的目标文档类型:{}", targetFormat);
try {
final InputStream is = documentFormatEnum.getInputStream(inputStream);
documentConverter.convert(is).as(format).to(outputStream).as(targetFormat).execute();
} catch (IOException | OfficeException e) {
e.printStackTrace();
}
log.info(">>> 文件转换结束");
}
}
/**
* 根据文件后缀判断是否图片
*
* @author xuyuxiang
* @date 2020/7/6 15:31
*/
public static boolean isPic(String fileSuffix) {
return MediaTypeConstant.IMG_JPG.equals(fileSuffix)
|| MediaTypeConstant.IMG_JPEG.equals(fileSuffix)
|| MediaTypeConstant.IMG_PNG.equals(fileSuffix)
|| MediaTypeConstant.IMG_GIF.equals(fileSuffix)
|| MediaTypeConstant.IMG_TIF.equals(fileSuffix)
|| MediaTypeConstant.IMG_BMP.equals(fileSuffix);
}
/**
* 根据文件后缀判断是否文档
*
* @author xuyuxiang
* @date 2020/7/6 15:31
*/
public static boolean isDoc(String fileSuffix) {
return MediaTypeConstant.DOC_TXT.equals(fileSuffix)
|| MediaTypeConstant.DOC_DOC.equals(fileSuffix)
|| MediaTypeConstant.DOC_DOCX.equals(fileSuffix)
|| MediaTypeConstant.DOC_XLS.equals(fileSuffix)
|| MediaTypeConstant.DOC_XLSX.equals(fileSuffix)
|| MediaTypeConstant.DOC_PPT.equals(fileSuffix)
|| MediaTypeConstant.DOC_PPTX.equals(fileSuffix)
|| MediaTypeConstant.DOC_PDF.equals(fileSuffix);
}
/**
* 根据文件后缀判断是否文档(不包含txt)
*
* @author xuyuxiang
* @date 2020/7/6 15:31
*/
public static boolean isDocNotTxt(String fileSuffix) {
return MediaTypeConstant.DOC_DOC.equals(fileSuffix)
|| MediaTypeConstant.DOC_DOCX.equals(fileSuffix)
|| MediaTypeConstant.DOC_XLS.equals(fileSuffix)
|| MediaTypeConstant.DOC_XLSX.equals(fileSuffix)
|| MediaTypeConstant.DOC_PPT.equals(fileSuffix)
|| MediaTypeConstant.DOC_PPTX.equals(fileSuffix)
|| MediaTypeConstant.DOC_PDF.equals(fileSuffix);
}
/**
* 根据文件后缀获取转换目标类型
*
* @author xuyuxiang
* @date 2020/7/6 17:03
*/
public static String getTargetContentTypeBySuffix(String fileSuffix) {
//如果目标类型是pdf
if (MediaTypeConstant.DOC_TXT.equals(fileSuffix)
|| MediaTypeConstant.DOC_DOC.equals(fileSuffix)
|| MediaTypeConstant.DOC_DOCX.equals(fileSuffix)
|| MediaTypeConstant.DOC_PPT.equals(fileSuffix)
|| MediaTypeConstant.DOC_PPTX.equals(fileSuffix)
|| MediaTypeConstant.DOC_PDF.equals(fileSuffix)) {
return MediaType.APPLICATION_PDF_VALUE;
} else {
//否则是html类型
return MediaType.TEXT_HTML_VALUE;
}
}
}
3.4 调用工具类转换文件
文件信息是我从数据库获取的,你们测试可以直接写死
public void preview(SysFileInfoParam sysFileInfoParam, HttpServletResponse response) {
byte[] fileBytes;
//根据文件id获取文件信息结果集
SysFileInfoResult sysFileInfoResult = this.getFileInfoResult(sysFileInfoParam.getId());
//获取文件后缀
String fileSuffix = sysFileInfoResult.getFileSuffix().toLowerCase();
//获取文件字节码
fileBytes = sysFileInfoResult.getFileBytes();
try {
//如果是文档类型,则使用libreoffice转换为pdf或html
InputStream inputStream = IoUtil.toStream(fileBytes);
//获取目标contentType(word和ppt和text转成pdf,excel转成html)
String targetContentType = LibreOfficeUtil.getTargetContentTypeBySuffix(fileSuffix);
//设置contentType
response.setContentType(targetContentType);
//获取outputStream
ServletOutputStream outputStream = response.getOutputStream();
//转换
LibreOfficeUtil.convertToPdf(inputStream, outputStream, fileSuffix);
//输出
IoUtil.write(outputStream, true, fileBytes);
} catch (IOException e) {
log.error(">>> 预览文件异常", e.getMessage());
throw new ServiceException(SysFileInfoExceptionEnum.PREVIEW_ERROR_NOT_SUPPORT);
} catch (LibreOfficeException e) {
log.error(">>> 初始化LibreOffice失败", e.getMessage());
throw new ServiceException(SysFileInfoExceptionEnum.PREVIEW_ERROR_LIBREOFFICE);
}
}
public SysFileInfoResult getFileInfoResult(Long fileId) {
byte[] fileBytes;
// 获取文件名
SysFileInfo sysFileInfo = this.getById(fileId);
if (sysFileInfo == null) {
throw new ServiceException(SysFileInfoExceptionEnum.NOT_EXISTED_FILE);
}
try {
// 返回文件字节码
fileBytes = fileOperator.getFileBytes(DEFAULT_BUCKET, sysFileInfo.getFileObjectName());
} catch (Exception e) {
log.error(">>> 获取文件流异常,请求号为:{},具体信息为:{}", RequestNoContext.get(), e.getMessage());
throw new ServiceException(SysFileInfoExceptionEnum.FILE_STREAM_ERROR);
}
SysFileInfoResult sysFileInfoResult = new SysFileInfoResult();
BeanUtil.copyProperties(sysFileInfo, sysFileInfoResult);
sysFileInfoResult.setFileBytes(fileBytes);
return sysFileInfoResult;
}
linux(CentOS) 上安装启动libreOffice服务
soffice --headless --accept="socket,host=0.0.0.0,port=8100;urp;" --nofirststartwizard &
jodconverter:
local:
#暂时关闭预览,启动时会有点慢
enabled: true
#设置libreoffice主目录 linux地址如:/usr/lib64/libreoffice
office-home: /usr/lib64/libreoffice
# office-home: C:/Program Files/LibreOffice
#开启多个libreoffice进程,每个端口对应一个进程
port-numbers: 8100
#libreoffice进程重启前的最大进程数
max-tasks-per-process: 100