实现Linux下word转pdf
This commit is contained in:
parent
9a2a99e073
commit
7450f41a9d
|
|
@ -2,9 +2,13 @@ package com.electromagnetic.industry.software.common.util;
|
|||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.text.StrFormatter;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.RuntimeUtil;
|
||||
import com.documents4j.api.DocumentType;
|
||||
import com.documents4j.api.IConverter;
|
||||
import com.documents4j.job.LocalConverter;
|
||||
import com.electromagnetic.industry.software.common.exception.BizException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.text.PDFTextStripper;
|
||||
|
|
@ -46,9 +50,29 @@ public class OfficeFileUtil {
|
|||
CONVERT.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);
|
||||
}
|
||||
} else {
|
||||
|
||||
try {
|
||||
String pdfParentDir = FileUtil.getParent(pdfPath, 1) + File.separatorChar + IdUtil.fastSimpleUUID() + File.separatorChar;
|
||||
StringBuilder command = new StringBuilder();
|
||||
command.append("libreoffice").append(" ").append("--headless")
|
||||
.append(" ").append("--convert-to")
|
||||
.append(" ").append("pdf")
|
||||
.append(" ").append(wordPath)
|
||||
.append(" ").append("--outdir")
|
||||
.append(" ").append(pdfParentDir);
|
||||
Process process = RuntimeUtil.exec(command.toString());
|
||||
process.waitFor();
|
||||
if (process.exitValue() != 0) {
|
||||
String info = StrFormatter.format("word文档{}转换成pdf文档{}失败", wordPath, pdfPath);
|
||||
throw new BizException(info);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new BizException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue