html转word,word转pdf
1. 简介
LibreOffice 是一款功能强大的办公软件,默认使用开放文档格式 (OpenDocument Format , ODF), 并支持 *.docx, *.xlsx, *.pptx 等其他格式。它包含了 Writer, Calc, Impress, Draw, Base 以及 Math 等组件,可用于处理文本文档、电子表格、演示文稿、绘图以及公式编辑。它可以运行于 Windows, GNU/Linux 以及 macOS 等操作系统上,并具有一致的用户体验。
2. 安装LibreOffice
-
选择版本进行安装
-
我安装的版本是macOS版本
输入以下指令查看安装状态,输出版本基表示安装成功
/Applications/LibreOffice.app/Contents/MacOS/soffice -version
- 到此使用终端即可进行文档操作。
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf:writer_pdf_Export --outdir /Users/liyadong/Desktop/ /Users/liyadong/Desktop/sharding-sphere.docx
执行终端命令后,LibreOffice 将word转成pdf,并且在指定目录生成pdf文件。警告为macOS系统的警告,请忽略。
LibreOffice主要是通过命令来执行文件操作,程序也只是对指令进行封装调用。
3. LibreOffice word 转 pdf
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf:writer_pdf_Export --outdir pdfFilePath wordFilePath
注意:pdfFilePath为生成的pdf路径(如:/Users/liyadong/Desktop),wordFilePath为word文件地址(/Users/liyadong/Desktop/测试html转word.docx)
/**
* 程序位置(linux 可以直接 soffice 替换 )
*/
private static final String COMMAND_PATH = "/Applications/LibreOffice.app/Contents/MacOS/soffice ";
public void wordToPdf(String wordFilePath, String pdfFilePath) {
String command = COMMAND_PATH + " --headless --convert-to pdf:writer_pdf_Export --outdir " + pdfFilePath + " " + wordFilePath;
log.info("执行的指令:{}", command);
Integer execResult = ProcessUtils.exec(command);
log.info("返回代码:{}", execResult);
}
@Test
public void wordToPdf() {
String wordPath = "/Users/liyadong/Desktop/sharding-sphere.docx";
String pdfPath = "/Users/liyadong/Desktop";
fileService.wordToPdf(wordPath, pdfPath);
}
4. LibreOffice html 转 word
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to docx:"MS Word 2007 XML" htmlpath --outdir wordpath
注意:htmlpath 为html的全路径地址,–outdir 可不填,不填则生成到系统更目录
/**
* 程序位置(linux 可以直接 soffice 替换 )
*/
private static final String COMMAND_PATH = "/Applications/LibreOffice.app/Contents/MacOS/soffice ";
public void htmlToWord(String htmlPath, String wordPath) {
String command = COMMAND_PATH + " --headless --convert-to docx:\"MS Word 2007 XML\" " + htmlPath + " --outdir " + wordPath;
log.info("执行的指令:{}", command);
Integer exec = ProcessUtils.exec(command);
log.info("返回代码:{}", exec);
}
@Test
public void htmlToWord() {
String htmlPath = "/Users/liyadong/Desktop/测试html转word.html";
String wordPath = "/Users/liyadong/Desktop/";
fileService.htmlToWord(htmlPath, wordPath);
}
5.工具类
@Slf4j
public class ProcessUtils {
/**
* 描述 执行命令
* @param command 命令
* @author liyadong
* @date 2024/5/10 17:45
* @return java.lang.Integer
*/
public static Integer exec(String command) {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("bash", "-c", command);
try {
Process process = processBuilder.start();
// 读取输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
log.info(line);
}
// 等待命令执行完成
process.waitFor();
// 获取退出值
int exitValue = process.exitValue();
log.info("命令执行结束,退出值: " + exitValue);
process.destroy();
return exitValue;
} catch (IOException | InterruptedException e) {
log.error("命令:{},执行失败:{}", command, e.getMessage());
}
return 0;
}
}