mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-03-14 05:03:49 +08:00
79 lines
3.0 KiB
Java
79 lines
3.0 KiB
Java
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.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
|
|
*/
|
|
@Component
|
|
public class OfficeToPdfService {
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(OfficeToPdfService.class);
|
|
|
|
public void openOfficeToPDF(String inputFilePath, String outputFilePath, FileAttribute fileAttribute) throws OfficeException {
|
|
office2pdf(inputFilePath, outputFilePath, fileAttribute);
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
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, FileAttribute fileAttribute) throws OfficeException {
|
|
if (null != inputFilePath) {
|
|
File inputFile = new File(inputFilePath);
|
|
// 判断目标文件路径是否为空
|
|
if (null == outputFilePath) {
|
|
// 转换后的文件路径
|
|
String outputFilePath_end = getOutputFilePath(inputFilePath);
|
|
if (inputFile.exists()) {
|
|
// 找不到源文件, 则返回
|
|
converterFile(inputFile, outputFilePath_end, fileAttribute);
|
|
}
|
|
} else {
|
|
if (inputFile.exists()) {
|
|
// 找不到源文件, 则返回
|
|
converterFile(inputFile, outputFilePath, fileAttribute);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static String getOutputFilePath(String inputFilePath) {
|
|
return inputFilePath.replaceAll("."+ getPostfix(inputFilePath), ".pdf");
|
|
}
|
|
|
|
public static String getPostfix(String inputFilePath) {
|
|
return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
|
|
}
|
|
|
|
}
|