3

jodconverter 4.1.0版本的话,改进了api的结构,同时新增了local以及online的模块,本文就来分析一下。

maven

        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.1.0</version>
        </dependency>

依赖变化

新版的话,对原来的jodconverter-core进行了抽离,将对libreoffice相关jar包的依赖从core模块中抽取出来,抽到jodconverter-local模块当中。
另外也新增了jodconverter-online模块,以支持libreoffice online server的远程调用。

  • Uno
    local方式是本地启动libreoffice,然后使用uno进行编程,操作本地的office。

Uno-Arc

相关的jar依赖如下

<dependency>
    <groupId>org.libreoffice</groupId>
    <artifactId>juh</artifactId>
    <version>5.4.2</version>
</dependency>
<dependency>
    <groupId>org.libreoffice</groupId>
    <artifactId>jurt</artifactId>
    <version>5.4.2</version>
</dependency>
<dependency>
    <groupId>org.libreoffice</groupId>
    <artifactId>ridl</artifactId>
    <version>5.4.2</version>
</dependency>
<dependency>
    <groupId>org.libreoffice</groupId>
    <artifactId>unoil</artifactId>
    <version>5.4.2</version>
</dependency>
  • online

This is LibreOffice Online, which provides basic collaborative editing of documents in a browser by re-using the LibreOffice core. Rendering fidelity should be excellent, and interoperability match that of LibreOffice.
可以部署到自己的私有云端,然后通过有ui界面可以操作,也通过http对外提供api服务。

比如

    - API: HTTP POST to /lool/convert-to/<format>
        - the format is e.g. "png", "pdf" or "txt"
        - the file itself in the payload
    - example
        - curl -F "data=@test.txt" https://localhost:9980/lool/convert-to/docx > out.docx
    - or in html:
          <form action="https://localhost:9980/lool/convert-to/docx" enctype="multipart/form-data" method="post">
              File: <input type="file" name="data"><br/>
              <input type="submit" value="Convert to DOCX">
          </form>

jodconverter-online模块基于这个api使用apache http client进行了client端的封装并进行连接池的优化处理。

api变化

4.1.0版本改进了api,方便进行流式操作
旧版的如下

jodConverter.convert(inputFile, outputFile);

新版如下

File inputFile = new File("spreadsheet.xls");
File outputFile = new File("spreadsheet.ods");
JodConverter
         .convert(inputFile)
         .to(outputFile)
         .execute();

或者

InputStream inputStream = ...
OutputStream outputStream = ...
JodConverter
         .convert(inputStream)
         .as(DefaultDocumentFormatRegistry.XLS)
         .to(outputStream)
         .as(DefaultDocumentFormatRegistry.ODS)
         .execute();

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...