LibreOffice安装及Office文件转PDF

Demo地址

https://download.csdn.net/download/L_C_Baker/12316480

windows安装

  1. 下载安装包进行安装
    https://zh-cn.libreoffice.org

  2. 设置环境变量(可选)

    增加path,LibreOffice的安装目录。

    C:\Program Files\LibreOffice\program
    
  3. LibreOffice装换命令尝试(注意输入输出文件地址)

    # windows
    soffice.exe --headless --invisible --convert-to pdf e:\tmp\123.docx --outdir e:\tmp
    
    # linux
    /usr/bin/libreoffice6.0 --headless --invisible --convert-to pdf /tmp/123.docx --outdir /tmp
    
  4. 增加pom依赖

    <dependency>
      <groupId>org.jodconverter</groupId>
      <artifactId>jodconverter-core</artifactId>
      <version>4.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.jodconverter</groupId>
      <artifactId>jodconverter-local</artifactId>
      <version>4.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.jodconverter</groupId>
      <artifactId>jodconverter-spring-boot-starter</artifactId>
      <version>4.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.libreoffice</groupId>
      <artifactId>ridl</artifactId>
      <version>5.4.2</version>
    </dependency>
    

注意: 在这里说明特别一下,jodconverter自4.2开始,对LibreOffice相关功能从jodconverter-core中分离出来,封装到为jodconverter-local,另外新增了jodconverter-online,支持LibreOffice online server的远程调用。

  1. 配置 application.properties(windows配置未生效?)

    jodconverter.local.enabled=true
    # 设置LibreOffice主目录
    jodconverter.local.office-home=${pom.office.home}
    # 开启多个LibreOffice进程,每个端口对应一个进程
    jodconverter.local.portNumbers=8100,8101,8102
    # LibreOffice进程重启前的最大进程数
    jodconverter.local.maxTasksPerProcess=100
    

    ${pom.office.home}没有生效,暂时不知道是什么原因。

  2. 使用Maven的多环境配置(windows配置未生效?)

    <profiles>
      <profile>
        <!-- windows环境 -->
        <id>win</id>
        <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
          <pom.office.home>C:/Program Files/LibreOffice</pom.office.home>
        </properties>
      </profile>
      <profile>
        <!-- linux环境 -->
        <id>linux</id>
        <properties>
          <pom.office.home>/opt/libreoffice6.0</pom.office.home>
        </properties>
      </profile>
    </profiles>
    
  3. 调用

    @RequestMapping(value = “/convertfiles”, method = RequestMethod.GET)
    public String convertFiles() {
    String re = “convert files fail”;

    File wfile = new File("F:/testfiles/test.docx");
    File pfile = new File("F:/testfiles/pfile.pdf");
    try {
        documentConverter.convert(wfile).to(pfile).execute();
        re = "convert files ok";
        } catch (OfficeException e) {
            e.printStackTrace();
        }
    
    return re;
    }
    

Linux安装

  1. 下载libreoffice安装包、语言包
cd /opt
#下载
wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.1.2/rpm/x86_64/LibreOffice_6.1.2_Linux_x86-64_rpm.tar.gz)
wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.1.2/rpm/x86_64/LibreOffice_6.1.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
# 解压
tar xzvf LibreOffice_6.4.2.2_Linux_x86-64_rpm.tar.gz
tar xzvf LibreOffice_6.4.2.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
  1. 安装
yum install /opt/LibreOffice_6.4.2.2_Linux_x86-64_rpm/RPMS/*.rpm
yum install /opt/LibreOffice_6.4.2.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm
  1. 文件转换测试

修改源文件地址和输出地址,转换成功即表示ok。

    /usr/bin/libreoffice6.4 --headless --invisible --convert-to pdf /tmp/sample.docx --outdir /tmp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的Uno API来实现LibreOfficeOffice文档换为PDF的功能。具体实现步骤如下: 1. 首先需要安装LibreOffice,并启动LibreOffice服务。启动命令为: ``` soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" ``` 2. 在Java中使用Uno API连接LibreOffice服务,代码示例如下: ``` XComponentContext xContext = Bootstrap.bootstrap(); XMultiComponentFactory xMCF = xContext.getServiceManager(); Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); ``` 3. 加载需要换的Office文档,代码示例如下: ``` String inputUrl = "file:///path/to/input.docx"; PropertyValue[] loadProps = new PropertyValue[1]; loadProps[0] = new PropertyValue(); loadProps[0].Name = "Hidden"; loadProps[0].Value = Boolean.TRUE; XComponent xDoc = xCompLoader.loadComponentFromURL(inputUrl, "_blank", 0, loadProps); ``` 4. 将文档换为PDF格式,代码示例如下: ``` String outputUrl = "file:///path/to/output.pdf"; PropertyValue[] convertProps = new PropertyValue[2]; convertProps[0] = new PropertyValue(); convertProps[0].Name = "FilterName"; convertProps[0].Value = "writer_pdf_Export"; convertProps[1] = new PropertyValue(); convertProps[1].Name = "Overwrite"; convertProps[1].Value = Boolean.TRUE; XStorable xStore = UnoRuntime.queryInterface(XStorable.class, xDoc); xStore.storeToURL(outputUrl, convertProps); ``` 5. 最后需要关闭文档和LibreOffice服务,代码示例如下: ``` xDoc.dispose(); System.exit(0); ``` 完整的代码示例如下: ``` import com.sun.star.comp.helper.Bootstrap; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.beans.PropertyValue; public class OfficeToPdfConverter { public static void main(String[] args) { try { // Connect to LibreOffice service XComponentContext xContext = Bootstrap.bootstrap(); XMultiComponentFactory xMCF = xContext.getServiceManager(); Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); // Load input document String inputUrl = "file:///path/to/input.docx"; PropertyValue[] loadProps = new PropertyValue[1]; loadProps[0] = new PropertyValue(); loadProps[0].Name = "Hidden"; loadProps[0].Value = Boolean.TRUE; XComponent xDoc = xCompLoader.loadComponentFromURL(inputUrl, "_blank", 0, loadProps); // Convert document to PDF String outputUrl = "file:///path/to/output.pdf"; PropertyValue[] convertProps = new PropertyValue[2]; convertProps[0] = new PropertyValue(); convertProps[0].Name = "FilterName"; convertProps[0].Value = "writer_pdf_Export"; convertProps[1] = new PropertyValue(); convertProps[1].Name = "Overwrite"; convertProps[1].Value = Boolean.TRUE; XStorable xStore = UnoRuntime.queryInterface(XStorable.class, xDoc); xStore.storeToURL(outputUrl, convertProps); // Close document and exit xDoc.dispose(); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } } ``` 需要注意的是,在使用Uno API连接LibreOffice服务时,需要在classpath中加入相应的jar包。具体jar包名称和路径可以参考LibreOffice安装目录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值