JodConverter 4.1.0 + OpenOffice 4.1.4 文档转换(Spring boot 1.5.8)

注:本实例最终在 CentOS 7 上运行,Spring boot 最好是1.5.7 以上!

1、Maven 配置,最基本 JAR 包,当然还有可能需要其他包,根据提示或项目情况导入:

<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-spring-boot-starter</artifactId>
    <version>4.1.0</version>
</dependency>
<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>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
</dependency>

2、application.properties 配置如下:

##################################################
# JodConverter Configuration
##################################################
jodconverter.enabled=true
jodconverter.officeHome=/opt/openoffice4
jodconverter.portNumbers=8100, 8101, 8102, 8103, 8104, 8105, 8106, 8107, 8108, 8109
jodconverter.maxTasksPerProcess=10

注意下分隔的格式,在 jodconverter-spring-boot-starter 包里可看到一些默认参数,如下:

@Configuration
@ConditionalOnClass({DocumentConverter.class})
@ConditionalOnProperty(
    prefix = "jodconverter",
    name = {"enabled"},
    havingValue = "true",
    matchIfMissing = false
)
@EnableConfigurationProperties({JodConverterProperties.class})
public class JodConverterAutoConfiguration {
    private final JodConverterProperties properties;

    public JodConverterAutoConfiguration(JodConverterProperties properties) {
        this.properties = properties;
    }

    private OfficeManager createOfficeManager() {
        Builder builder = LocalOfficeManager.builder();
        if (!StringUtils.isBlank(this.properties.getPortNumbers())) {
            Set<Integer> iports = new HashSet();
            String[] var3 = StringUtils.split(this.properties.getPortNumbers(), ", ");
            int var4 = var3.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String portNumber = var3[var5];
                iports.add(NumberUtils.toInt(portNumber, 2002));
            }

            builder.portNumbers(ArrayUtils.toPrimitive((Integer[])iports.toArray(new Integer[iports.size()])));
        }

        builder.officeHome(this.properties.getOfficeHome());
        builder.workingDir(this.properties.getWorkingDir());
        builder.templateProfileDir(this.properties.getTemplateProfileDir());
        builder.killExistingProcess(this.properties.isKillExistingProcess());
        builder.processTimeout(this.properties.getProcessTimeout());
        builder.processRetryInterval(this.properties.getProcessRetryInterval());
        builder.taskExecutionTimeout(this.properties.getTaskExecutionTimeout());
        builder.maxTasksPerProcess(this.properties.getMaxTasksPerProcess());
        builder.taskQueueTimeout(this.properties.getTaskQueueTimeout());
        return builder.build();
    }

    @Bean(
        initMethod = "start",
        destroyMethod = "stop"
    )
    @ConditionalOnMissingBean
    public OfficeManager officeManager() {
        return this.createOfficeManager();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean({OfficeManager.class})
    public DocumentConverter jodConverter(OfficeManager officeManager) {
        return LocalConverter.make(officeManager);
    }
}
@ConfigurationProperties("jodconverter")
public class JodConverterProperties {
    private boolean enabled;
    private String officeHome;
    private String portNumbers = "2002";
    private String workingDir;
    private String templateProfileDir;
    private boolean killExistingProcess = true;
    private long processTimeout = 120000L;
    private long processRetryInterval = 250L;
    private long taskExecutionTimeout = 120000L;
    private int maxTasksPerProcess = 200;
    private long taskQueueTimeout = 30000L;

    public JodConverterProperties() {
    }

    public boolean isEnabled() {
        return this.enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getOfficeHome() {
        return this.officeHome;
    }

    public void setOfficeHome(String officeHome) {
        this.officeHome = officeHome;
    }

    public String getPortNumbers() {
        return this.portNumbers;
    }

    public void setPortNumbers(String portNumbers) {
        this.portNumbers = portNumbers;
    }

    public String getWorkingDir() {
        return this.workingDir;
    }

    public void setWorkingDir(String workingDir) {
        this.workingDir = workingDir;
    }

    public String getTemplateProfileDir() {
        return this.templateProfileDir;
    }

    public void setTemplateProfileDir(String templateProfileDir) {
        this.templateProfileDir = templateProfileDir;
    }

    public boolean isKillExistingProcess() {
        return this.killExistingProcess;
    }

    public void setKillExistingProcess(boolean killExistingProcess) {
        this.killExistingProcess = killExistingProcess;
    }

    public long getProcessTimeout() {
        return this.processTimeout;
    }

    public void setProcessTimeout(long processTimeout) {
        this.processTimeout = processTimeout;
    }

    public long getProcessRetryInterval() {
        return this.processRetryInterval;
    }

    public void setProcessRetryInterval(long procesRetryInterval) {
        this.processRetryInterval = procesRetryInterval;
    }

    public long getTaskExecutionTimeout() {
        return this.taskExecutionTimeout;
    }

    public void setTaskExecutionTimeout(long taskExecutionTimeout) {
        this.taskExecutionTimeout = taskExecutionTimeout;
    }

    public int getMaxTasksPerProcess() {
        return this.maxTasksPerProcess;
    }

    public void setMaxTasksPerProcess(int maxTasksPerProcess) {
        this.maxTasksPerProcess = maxTasksPerProcess;
    }

    public long getTaskQueueTimeout() {
        return this.taskQueueTimeout;
    }

    public void setTaskQueueTimeout(long taskQueueTimeout) {
        this.taskQueueTimeout = taskQueueTimeout;
    }
}

3、最后在业务层引入 DocumentConverter,如下:

@Autowired
private DocumentConverter documentConverter;
documentConverter.convert(sourceFile).to(targetFile).execute();

说明:

jodconverter-core (转换核心包)

jodconverter-local(本地化部署,还有一个是 online 版,有需要自己去了解下)

jodconverter-spring-boot-starter(与 Spring boot 依赖配置)

参考 API 地址:http://javadoc.io/doc/org.jodconverter/jodconverter-core/4.1.0

This is JODConverter version 2.2.2, released on 2009-04-11. JODConverter is a Java library for converting office documents into different formats, using OpenOffice.org 2.x or 3.x. See http://www.artofsolving.com/opensource/jodconverter for the latest documentation. Before you can perform any conversions you need to start OpenOffice.org in listening mode on port 8100 as described in the JODConverter Guide. As a quick start you can type from a command line soffice -headless -accept="socket,port=8100;urp;" JODConverter is both a Java library and a set of ready-to-use tools: * a web application that you can deploy into any servlet container (e.g. Apache Tomcat) * a command line tool (java -jar jodconverter-cli-2.2.2.jar <input-document> <output-document>) Requirements ============ The JAR library requires * Java 1.4 or higher * OpenOffice.org 2.x or 3.x; the latest stable version (currenty 3.0.1) is generally recommended The webapp additionally requires * A Servlet 2.3 container such as Apache Tomcat v4.x or higher Licenses ======== JODConverter is distributed under the terms of the LGPL. This basically means that you are free to use it in both open source and commercial projects. If you modify the library itself you are required to contribute your changes back, so JODConverter can be improved. (You are free to modify the sample webapp as a starting point for your own webapp without restrictions.) JODConverter includes various third-party libraries so you must agree to their respective licenses - included in docs/third-party-licenses. That may include software developed by * the Apache Software Foundation (http://www.apache.org) * the Spring Framework project (http://www.springframework.org)
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值