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