LibreOffice工具使用

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;
    }
}
6. 相关源码
  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值