jodconverter 文档转换

我们知道办公文档中常用的各种.doc.xls等都是Microsoft Office的专有格式,有关这些格式的开源资料非常少,一直以来,开源的Office办公软件也只有LibreOffice(和早期的OpenOffice)这两个选择,其它都是商业公司的付费产品。然而办公文档在线预览又是一个很常见的功能,在不使用付费产品的前提下,我们如何在自己的代码中实现办公文档的预览呢?jodconverter这个库可以调用LibreOffice或OpenOffice,将专有格式转换成浏览器支持的.pdf.html等格式,这样就间接实现了Office文档的预览功能。

Github地址:https://github.com/jodconverter/jodconverter

注:jodconverter这个项目文档很烂,好在用起来不算复杂。

安装LibreOffice

使用jodconverter前我们需要先安装LibreOffice程序。Windows下我们可以使用msi安装版或是Portable版,安装位置无所谓,因为可以在代码中指定。Linux下可以直接从软件源安装。不过这里注意Linux下我们需要使用中文版系统并安装语言包,此外还要安装中文字体,否则转换可能乱码或是字体错误。如果使用Docker部署程序也是同理。

LibreOffice官方网站:https://www.libreoffice.org

SpringBoot工程引入jodconverter

jodconverter提供了SpringBoot的起步依赖,它用于配置相关的Bean,此外我们还需要引入jodconverter的核心依赖。

<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-spring-boot-starter</artifactId>
    <version>4.4.6</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local-lo</artifactId>
    <version>4.4.6</version>
</dependency>

注:lo代表LibreOffice。jodconverter支持OpenOffice和LibreOffice,分别对应oolo

引入依赖后,我们还需要在application.properties中进行一些配置:

jodconverter.local.enabled=true
jodconverter.local.office-home=E:/windows-office
jodconverter.local.port-numbers=8001
jodconverter.local.max-tasks-per-process=1

其中,office-home属性指定了我们LibreOffice程序的安装位置;port-numbers指定启动LibreOffice进程的端口,配置的每个端口对应启动一个进程;max-tasks-per-process对应一个进程可以处理的任务数,达到对应任务数后进程将重启,该设定存在的目的是避免进程存在内存溢出问题时耗尽资源。文档转换是CPU密集型程序,这个进程数应该根据主机或容器的CPU核心数合理指定。

文档转换例子

下面我们编写一个例子程序,实现Office文档类型的转换。

package com.gacfox.demo.controller;

import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.office.OfficeException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;

@RestController
public class ConvertController {
    @Resource
    private DocumentConverter documentConverter;

    @GetMapping("/")
    public void convert() {
        File fromFile = new File("E:/说明文档.docx");
        File toFile = new File("E:/说明文档.pdf");
        try {
            documentConverter
                    .convert(fromFile)
                    .as(DefaultDocumentFormatRegistry.DOCX)
                    .to(toFile)
                    .as(DefaultDocumentFormatRegistry.PDF)
                    .execute();
        } catch (OfficeException e) {
            throw new RuntimeException(e);
        }
    }
}

代码中,我们注入了DocumentConverter对象,然后调用该对象,将.docx文件转换成了.pdf文件。这里对于.as()方法的调用是可以省略的,jodconverter也可以自己根据File对象的文件后缀名,自动识别文件类型和要转换的目标文件类型。

有关jodconverter支持的文档转换类型,可以参考如下表格:

Format Family From (any of) To (any of)
Text .odt* OpenDocument Text .ott OpenDocument Text Template .sxw OpenOffice.org 1.0 Text .rtf* Rich Text Format .doc Microsoft Word .docx Microsoft Word XML .wpd* WordPerfect .txt Plain Text .html HTML1 .pdf* Portable Document Format .odt OpenDocument Text .ott OpenDocument Text Template .sxw* OpenOffice.org 1.0 Text .rtf Rich Text Format .doc Microsoft Word .docx* Microsoft Word XML .txt Plain Text .html HTML2 *.wiki MediaWiki wikitext
Spreadsheet .ods* OpenDocument Spreadsheet .ots OpenDocument Spreadsheet Template .sxc OpenOffice.org 1.0 Spreadsheet .xls* Microsoft Excel .xlsx Microsoft Excel XML .csv Comma-Separated Values *.tsv Tab-Separated Values .pdf* Portable Document Format .ods OpenDocument Spreadsheet .ots OpenDocument Spreadsheet Template .sxc* OpenOffice.org 1.0 Spreadsheet .xls Microsoft Excel .xlsx Microsoft Excel XML .csv* Comma-Separated Values .tsv Tab-Separated Values .html HTML2
Presentation .odp* OpenDocument Presentation .otp OpenDocument Presentation Template .sxi OpenOffice.org 1.0 Presentation .ppt* Microsoft PowerPoint .pptx* Microsoft PowerPoint XML .pdf* Portable Document Format .swf Macromedia Flash .odp OpenDocument Presentation .otp* OpenDocument Presentation Template .sxi OpenOffice.org 1.0 Presentation .ppt Microsoft PowerPoint .pptx* Microsoft PowerPoint XML .html* HTML2
Drawing .odg* OpenDocument Drawing .otg* OpenDocument Drawing Template .svg* Scalable Vector Graphics .swf* Macromedia Flash
作者:Gacfox
版权声明:本网站为非盈利性质,文章如非特殊说明均为原创,版权遵循知识共享协议CC BY-NC-ND 4.0进行授权,转载必须署名,禁止用于商业目的或演绎修改后转载。
Copyright © 2017-2024 Gacfox All Rights Reserved.
Build with NextJS | Sitemap