Spring BootでJODConverterを使用する方法について。
Excelファイルからpdfファイルを生成するJODConverter(バージョン4)はSpring Bootからも使う事が出来る。
JODConverterで変換を行うOfficeDocumentConverterのインスタンスを自分で生成すればいいのだが、Spring Bootから扱う為のクラスも提供されている。
〜
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-rest') // REST
compile('org.springframework.boot:spring-boot-starter-security') // 認証
compile('org.jodconverter:jodconverter-spring-boot-starter:4.0.0-RELEASE') // JODConverter
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4')
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
Spring Boot 1.5.6・JODConverter 4.0.0の例。
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import org.jodconverter.OfficeDocumentConverter; import org.jodconverter.office.OfficeException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class DownloadPdfController { @Autowired private OfficeDocumentConverter documentConverter;
@RequestMapping(path = "/rest/pdf", produces = MediaType.APPLICATION_PDF_VALUE) public Resource pdf() throws IOException, OfficeException { Path excelFile = Paths.get("C:/Users/hishidama/Desktop/test.xlsx"); Path pdfFile = Files.createTempFile("spring-boot-pdf-", ".pdf"); documentConverter.convert(excelFile.toFile(), pdfFile.toFile()); return new FileSystemResource(pdfFile.toFile()); } }
OfficeDocumentConverterはExcelファイルをpdfファイルに変換するので、FileSystemResourceを使ってpdfファイルを返してやればいい。
なお、デフォルトではJODConverterが使えない状態になっているので、使える状態にする必要がある。
jodconverter.enabled=true
上記の例では、@Autowiredアノテーションを使ってOfficeDocumentConverterインスタンスを取得している。
OfficeDocumentConverterインスタンスを生成するメソッド(@Beanアノテーション付きメソッド)はJodConverterAutoConfigurationクラスにある。
@Configuration @ConditionalOnClass({OfficeDocumentConverter.class}) @ConditionalOnProperty( prefix = "jodconverter", name = "enabled", havingValue = "true", matchIfMissing = false ) @EnableConfigurationProperties(JodConverterProperties.class) public class JodConverterAutoConfiguration { 〜
@Bean(initMethod = "start", destroyMethod = "stop") @ConditionalOnMissingBean public OfficeManager officeManager() { return createOfficeManager(); }
@Bean @ConditionalOnMissingBean @ConditionalOnBean(OfficeManager.class) public OfficeDocumentConverter jodConverter(final OfficeManager officeManager) { return new OfficeDocumentConverter(officeManager); } }
@ConditionalOnPropertyアノテーションは、プロパティーをチェックし、ダメだった場合はコンフィグレーションを実施しないものらしい。
ここでは、jodconverter.enabled
プロパティーがtrueである場合だけコンフィグレーションを実施する。(デフォルトはfalse扱い)
デフォルトではjodconverter.enabled
プロパティーは設定されていないので、コンフィグレーションは実施されない。
Spring Bootの実行時に以下のようなエラーが発生して、Spring Bootが起動できない。
(DownloadPdfControllerクラスにある@Autowiredアノテーションが付いているOfficeDocumentConverterのインスタンスが見つからない、というエラー)
*************************** APPLICATION FAILED TO START *************************** Description: Field documentConverter in com.example.demo.rest.DownloadPdfController required a bean of type 'org.jodconverter.OfficeDocumentConverter' that could not be found. - Bean method 'jodConverter' not loaded because @ConditionalOnProperty (jodconverter.enabled=true) did not find property 'enabled' Action: Consider revisiting the conditions above or defining a bean of type 'org.jodconverter.OfficeDocumentConverter' in your configuration.
application.propertiesで「jodconverter.enabled=true
」を設定してやるとコンフィグレーションが実施されるようになる。
(OfficeDocumentConverterインスタンスが生成され、コンテナーで管理されるようになる)
Spring Boot用のJODConverterの設定はJodConverterPropertiesクラス(JavaBeans)で保持されている。
application.propertiesで設定(JodConverterPropertiesで定義されているプロパティー)を書いてやると、OfficeDocumentConverter生成時に反映される。
jodconverter.enabled=true jodconverter.officeHome=C:/Program Files/LibreOffice 5 jodconverter.workingDir=D:/temp/jodconverter/spring-boot-workingdir
(ちなみに、JODConverter 4.0.0にはバグがあり、officeHomeを指定した場合はworkingDirも指定しないといけない)