jodconverter word文档转PDF

1.创建maven项目

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.lhtgroupId>
    <artifactId>jodartifactId>
    <version>1.0version>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.14.RELEASEversion>
    parent>
    <dependencies>
        <dependency>
            <groupId>org.jodconvertergroupId>
            <artifactId>jodconverter-spring-boot-starterartifactId>
            <version>4.2.0version>
        dependency>
        <dependency>
            <groupId>org.jodconvertergroupId>
            <artifactId>jodconverter-localartifactId>
            <version>4.2.0version>
        dependency>
    dependencies>
project>
2.编写spring boot 事件监听器
本文是为了做一个转码服务,和项目分开部署,如,在项目内部用可以注入DocumentConverter,在业务代码中调用
@Component
public class AppLisenter implements ApplicationListener<ApplicationReadyEvent> {


    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        ConfigurableApplicationContext context = event.getApplicationContext();
        DocumentConverter converter = context.getBean(DocumentConverter.class);
        try {
            converter.convert(new File("C:\\Users\\EDZ\\Desktop\\work\\(S)微商城项目功能清单.xlsx")).to(new File("C:\\Users\\EDZ\\Desktop\\work\\(S)微商城项目功能清单.pdf")).execute();
        } catch (OfficeException e) {
            e.printStackTrace();
        }
    }
}
3.编写启动类
@SpringBootApplication
public class TranscodingApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(TranscodingApplication.class);
        app.addListeners(new AppLisenter());
        app.run(args);
    }

}
4.编写配置文件
jodconverter:
  local:
    enabled: true #开启本地版
    office-home: D:\LibreOffice #libreoffice安装路径

你可能感兴趣的:(Spring,Boot)