实现在Windows下Word文件可预览
This commit is contained in:
parent
b69132541a
commit
617da85ead
|
|
@ -68,6 +68,7 @@ public class ElePropertyConfig {
|
||||||
@Value("${backup.remote.port}")
|
@Value("${backup.remote.port}")
|
||||||
private int remotePort;
|
private int remotePort;
|
||||||
|
|
||||||
|
@Getter
|
||||||
@Value("${winPrefix}")
|
@Value("${winPrefix}")
|
||||||
private String winPrefix = "";
|
private String winPrefix = "";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ public class ChatService {
|
||||||
} else {
|
} else {
|
||||||
FileUtil.writeFromStream(file.getInputStream(), srcPath);
|
FileUtil.writeFromStream(file.getInputStream(), srcPath);
|
||||||
if (Arrays.asList("doc", "docx").contains(fileType)) {
|
if (Arrays.asList("doc", "docx").contains(fileType)) {
|
||||||
OfficeFileUtil.doc2pdf(srcPath, pdfPath);
|
OfficeFileUtil.doc2pdf(srcPath, pdfPath, elePropertyConfig.getWinPrefix());
|
||||||
} else {
|
} else {
|
||||||
OfficeFileUtil.ppt2pdf(srcPath, pdfPath);
|
OfficeFileUtil.ppt2pdf(srcPath, pdfPath);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1344,18 +1344,20 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<InputStreamResource> preview(String id, HttpServletResponse response, int dataOwnCode) {
|
public ResponseEntity<InputStreamResource> preview(String id, HttpServletResponse response, int dataOwnCode) {
|
||||||
String fileSaveTmpPath = null;
|
String fileSaveTmpPath = null;
|
||||||
|
String wordPath = null;
|
||||||
try {
|
try {
|
||||||
EdFileInfo fileInfo = this.baseMapper.selectById(id);
|
EdFileInfo fileInfo = this.baseMapper.selectById(id);
|
||||||
Assert.isTrue(Objects.nonNull(fileInfo), "文件不存在");
|
Assert.isTrue(Objects.nonNull(fileInfo), "文件不存在");
|
||||||
String fileDbPath = commonService.getDbPath(fileInfo.getFilePath());
|
String fileDbPath = commonService.getDbPath(fileInfo.getFilePath());
|
||||||
String fileSysPath = commonService.getFileSysPath(fileInfo.getId());
|
String fileSysPath = commonService.getFileSysPath(fileInfo.getId());
|
||||||
fileSaveTmpPath = elePropertyConfig.getEleTmpPath() + File.separator + IdUtil.fastSimpleUUID() + "." + fileInfo.getFileType();
|
fileSaveTmpPath = elePropertyConfig.getEleTmpPath() + File.separator + IdUtil.fastSimpleUUID() + "." + fileInfo.getFileType();
|
||||||
|
wordPath = new String(fileSaveTmpPath);
|
||||||
FileUtil.copy(fileSysPath, fileSaveTmpPath, true);
|
FileUtil.copy(fileSysPath, fileSaveTmpPath, true);
|
||||||
EleCommonUtil.decryptFile(fileSaveTmpPath, SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes()));
|
EleCommonUtil.decryptFile(fileSaveTmpPath, SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes()));
|
||||||
|
|
||||||
if (Arrays.asList("doc", "docx").contains(fileInfo.getFileType())) {
|
if (Arrays.asList("doc", "docx").contains(fileInfo.getFileType())) {
|
||||||
String pdfTmpPath = elePropertyConfig.getEleTmpPath() + File.separator + fileInfo.getId() + ".pdf";
|
String pdfTmpPath = elePropertyConfig.getEleTmpPath() + File.separator + fileInfo.getId() + ".pdf";
|
||||||
OfficeFileUtil.doc2pdf(fileSaveTmpPath, pdfTmpPath);
|
OfficeFileUtil.doc2pdf(fileSaveTmpPath, pdfTmpPath, elePropertyConfig.getWinPrefix());
|
||||||
fileSaveTmpPath = pdfTmpPath;
|
fileSaveTmpPath = pdfTmpPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1385,6 +1387,9 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
throw new BizException("文件预览错误,文件id是 " + id);
|
throw new BizException("文件预览错误,文件id是 " + id);
|
||||||
} finally {
|
} finally {
|
||||||
FileUtil.del(fileSaveTmpPath);
|
FileUtil.del(fileSaveTmpPath);
|
||||||
|
if (StrUtil.isNotBlank(wordPath)) {
|
||||||
|
FileUtil.del(wordPath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,6 @@ import cn.hutool.core.text.StrFormatter;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.RuntimeUtil;
|
import cn.hutool.core.util.RuntimeUtil;
|
||||||
import com.benjaminwan.ocrlibrary.OcrResult;
|
import com.benjaminwan.ocrlibrary.OcrResult;
|
||||||
import com.documents4j.api.DocumentType;
|
|
||||||
import com.documents4j.api.IConverter;
|
|
||||||
import com.documents4j.job.LocalConverter;
|
|
||||||
import com.electromagnetic.industry.software.common.exception.BizException;
|
import com.electromagnetic.industry.software.common.exception.BizException;
|
||||||
import com.electromagnetic.industry.software.common.pojo.PageFile;
|
import com.electromagnetic.industry.software.common.pojo.PageFile;
|
||||||
import io.github.mymonstercat.Model;
|
import io.github.mymonstercat.Model;
|
||||||
|
|
@ -35,7 +32,6 @@ import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -45,37 +41,37 @@ import java.util.Objects;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class OfficeFileUtil {
|
public class OfficeFileUtil {
|
||||||
|
|
||||||
public static void doc2pdf(String wordPath, String pdfPath) {
|
public static void doc2pdf(String wordPath, String pdfPath, String installPath) {
|
||||||
if (FileUtil.exist(pdfPath)) {
|
if (FileUtil.exist(pdfPath)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log.info("Start convert word file to pdf, word path: {}, pdf path: {}", wordPath, pdfPath);
|
log.info("Start convert word file to pdf, word path: {}, pdf path: {}", wordPath, pdfPath);
|
||||||
if (EleCommonUtil.isWinOs()) {
|
if (EleCommonUtil.isWinOs()) {
|
||||||
File inputWord = new File(wordPath);
|
try {
|
||||||
File outputFile = new File(pdfPath);
|
String libOfficePath = installPath + "/LibreOffice/program/soffice.exe";
|
||||||
try (InputStream docxInputStream = Files.newInputStream(inputWord.toPath());
|
if (!FileUtil.exist(libOfficePath)) {
|
||||||
OutputStream outputStream = Files.newOutputStream(outputFile.toPath())) {
|
log.error("libOfficePath does not exist");
|
||||||
IConverter build = LocalConverter.builder().build();
|
throw new BizException("请联系管理员升级数据库组件");
|
||||||
boolean execute;
|
}
|
||||||
if (wordPath.endsWith(".docx")) {
|
String pdfParentPath = FileUtil.getParent(pdfPath, 1);
|
||||||
execute = build.convert(docxInputStream)
|
StringBuilder command = new StringBuilder();
|
||||||
.as(DocumentType.DOCX)
|
command.append(libOfficePath).append(" --headless --convert-to pdf --outdir")
|
||||||
.to(outputStream)
|
.append(" ").append(pdfParentPath).append(" ").append(wordPath);
|
||||||
.as(DocumentType.PDF).schedule().get();
|
Process process = RuntimeUtil.exec(command.toString());
|
||||||
|
process.waitFor();
|
||||||
|
if (process.exitValue() != 0) {
|
||||||
|
log.error("word文件 {} 转换失败", wordPath);
|
||||||
|
throw new BizException("word文件转换失败");
|
||||||
} else {
|
} else {
|
||||||
execute = build.convert(docxInputStream)
|
String wordMainName = FileUtil.mainName(wordPath);
|
||||||
.as(DocumentType.DOC)
|
String pdfTmpPath = pdfParentPath + "/" + wordMainName + ".pdf";
|
||||||
.to(outputStream)
|
FileUtil.move(new File(pdfTmpPath), new File(pdfPath), true);
|
||||||
.as(DocumentType.PDF).schedule().get();
|
|
||||||
}
|
}
|
||||||
Assert.isTrue(execute, "转换失败");
|
|
||||||
log.info("转换完毕 targetPath = {}", outputFile.getAbsolutePath());
|
|
||||||
build.shutDown();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[documents4J] word转pdf失败:{}", e.toString());
|
log.error(e.getMessage(), e);
|
||||||
String info = StrFormatter.format("word文档{}转换成pdf文档{}失败", wordPath, pdfPath);
|
throw new BizException(e.getMessage());
|
||||||
throw new BizException(info + ",原因" + e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
StringBuilder command = new StringBuilder();
|
StringBuilder command = new StringBuilder();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue