1. 程式人生 > >jodconverter word文件轉PDF

jodconverter word文件轉PDF

1.建立maven專案
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion
>
<groupId>com.lht</groupId> <artifactId>jod</artifactId> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version
>
1.5.14.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.0</version> </dependency
>
<dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.2.0</version> </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安裝路徑