jodconverter转换带密码office文件

This commit is contained in:
陈精华 2022-12-19 16:38:50 +08:00
parent d761d0cc88
commit f52169ec27
No known key found for this signature in database
GPG Key ID: 30BDC970902B755D
5 changed files with 93 additions and 17 deletions

View File

@ -1,12 +1,17 @@
package cn.keking.service;
import cn.keking.model.FileAttribute;
import com.sun.star.document.UpdateDocMode;
import org.apache.commons.lang3.StringUtils;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.local.JodConverter;
import org.jodconverter.local.LocalConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* @author yudian-it
@ -16,22 +21,33 @@ public class OfficeToPdfService {
private final static Logger logger = LoggerFactory.getLogger(OfficeToPdfService.class);
public void openOfficeToPDF(String inputFilePath, String outputFilePath) throws OfficeException {
office2pdf(inputFilePath, outputFilePath);
public void openOfficeToPDF(String inputFilePath, String outputFilePath, FileAttribute fileAttribute) throws OfficeException {
office2pdf(inputFilePath, outputFilePath, fileAttribute);
}
public static void converterFile(File inputFile, String outputFilePath_end) throws OfficeException {
public static void converterFile(File inputFile, String outputFilePath_end, FileAttribute fileAttribute) throws OfficeException {
File outputFile = new File(outputFilePath_end);
// 假如目标路径不存在,则新建该路径
if (!outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs()) {
logger.error("创建目录【{}】失败,请检查目录权限!",outputFilePath_end);
}
JodConverter.convert(inputFile).to(outputFile).execute();
LocalConverter.Builder builder;
if (StringUtils.isNotBlank(fileAttribute.getFilePassword())) {
Map<String, Object> loadProperties = new HashMap<>();
loadProperties.put("Hidden", true);
loadProperties.put("ReadOnly", true);
loadProperties.put("UpdateDocMode", UpdateDocMode.NO_UPDATE);
loadProperties.put("Password", fileAttribute.getFilePassword());
builder = LocalConverter.builder().loadProperties(loadProperties);
} else {
builder = LocalConverter.builder();
}
builder.build().convert(inputFile).to(outputFile).execute();
}
public void office2pdf(String inputFilePath, String outputFilePath) throws OfficeException {
public void office2pdf(String inputFilePath, String outputFilePath, FileAttribute fileAttribute) throws OfficeException {
if (null != inputFilePath) {
File inputFile = new File(inputFilePath);
// 判断目标文件路径是否为空
@ -40,12 +56,12 @@ public class OfficeToPdfService {
String outputFilePath_end = getOutputFilePath(inputFilePath);
if (inputFile.exists()) {
// 找不到源文件, 则返回
converterFile(inputFile, outputFilePath_end);
converterFile(inputFile, outputFilePath_end, fileAttribute);
}
} else {
if (inputFile.exists()) {
// 找不到源文件, 则返回
converterFile(inputFile, outputFilePath);
converterFile(inputFile, outputFilePath, fileAttribute);
}
}
}

View File

@ -92,7 +92,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
} else {
if (StringUtils.hasText(outFilePath)) {
try {
officeToPdfService.openOfficeToPDF(filePath, outFilePath);
officeToPdfService.openOfficeToPDF(filePath, outFilePath, fileAttribute);
} catch (OfficeException e) {
if (isPwdProtectedOffice && !OfficeUtils.isCompatible(filePath, filePassword)) {
// 加密文件密码错误提示重新输入

View File

@ -1,12 +1,12 @@
package cn.keking.utils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.springframework.lang.Nullable;
import java.io.FileInputStream;
import java.io.IOException;
/**
* Office工具类
@ -16,8 +16,6 @@ import java.io.IOException;
*/
public class OfficeUtils {
private static final String POI_INVALID_PASSWORD_MSG = "Invalid password specified";
/**
* 判断officeword,excel,ppt文件是否受密码保护
*
@ -27,14 +25,12 @@ public class OfficeUtils {
public static boolean isPwdProtected(String path) {
try {
ExtractorFactory.createExtractor(new FileInputStream(path));
} catch (IOException e) {
if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) {
return true;
}
} catch (EncryptedDocumentException e) {
return true;
} catch (Exception e) {
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
for (Throwable throwable : throwableArray) {
if (throwable instanceof IOException && POI_INVALID_PASSWORD_MSG.equals(throwable.getMessage())) {
if (throwable instanceof EncryptedDocumentException) {
return true;
}
}

View File

@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
<title>文件预览</title>
<#include "*/commonHeader.ftl">
<#include "*/needFilePasswordHeader.ftl">
</head>
<body>
<iframe src="${pdfUrl}" width="100%" frameborder="0"></iframe>

View File

@ -0,0 +1,63 @@
<#setting classic_compatible=true>
<link rel="icon" href="./favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
<script src="js/jquery-3.6.1.min.js" type="text/javascript"></script>
<script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="js/bootbox.min.js" type="text/javascript"></script>
<script>
// 中文环境
var locale_zh_CN = {
OK: '确定',
CONFIRM: '确认',
CANCEL: '取消'
};
bootbox.addLocale('locale_zh_CN', locale_zh_CN);
/**
* 需要文件密码
*/
function needFilePassword() {
if ('${needFilePassword}' == 'true') {
let promptTitle = "你正在预览加密文件,请输入文件密码。";
if ('${filePasswordError}' == 'true') {
promptTitle = "密码错误,请重新输入密码。";
}
bootbox.prompt({
title: promptTitle,
inputType: 'password',
centerVertical: true,
locale: 'locale_zh_CN',
callback: function (filePassword) {
if (filePassword != null) {
const locationHref = window.location.href;
const isInclude = locationHref.includes("filePassword=");
let redirectUrl = null;
if (isInclude) {
const url = new URL(locationHref);
url.searchParams.set("filePassword", filePassword);
redirectUrl = url.href;
} else {
redirectUrl = locationHref + '&filePassword=' + filePassword;
}
window.location.replace(redirectUrl);
} else {
location.reload();
}
}
});
}
}
</script>
<style>
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
width: 100%;
}
</style>