重写Linux下word转pdf

This commit is contained in:
chenxudong 2025-03-25 11:47:05 +08:00
parent 7e96ca171e
commit a5766b31d3
1 changed files with 1 additions and 10 deletions

View File

@ -57,14 +57,8 @@ public class OfficeFileUtil {
} }
} else { } else {
try { try {
String pdfParentDir = FileUtil.getParent(pdfPath, 1) + File.separatorChar + IdUtil.fastSimpleUUID() + File.separatorChar;
StringBuilder command = new StringBuilder(); StringBuilder command = new StringBuilder();
command.append("/usr/bin/libreoffice24.8").append(" ").append("--headless") command.append("unoconv -f pdf -o").append(" ").append(pdfPath).append(" ").append(wordPath);
.append(" ").append("--convert-to")
.append(" ").append("pdf")
.append(" ").append(wordPath)
.append(" ").append("--outdir")
.append(" ").append(pdfParentDir);
log.info("convert word file to pdf, command: {}", command); log.info("convert word file to pdf, command: {}", command);
Process process = RuntimeUtil.exec(command.toString()); Process process = RuntimeUtil.exec(command.toString());
process.waitFor(); process.waitFor();
@ -72,9 +66,6 @@ public class OfficeFileUtil {
String info = StrFormatter.format("word文档{}转换成pdf文档{}失败", wordPath, pdfPath); String info = StrFormatter.format("word文档{}转换成pdf文档{}失败", wordPath, pdfPath);
throw new BizException(info); throw new BizException(info);
} }
String outPdfPath = FileUtil.loopFiles(pdfParentDir).get(0).getAbsolutePath();
FileUtil.move(new File(outPdfPath), new File(pdfPath), true);
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
throw new BizException(e.getMessage(), e); throw new BizException(e.getMessage(), e);