Spring BootでJODConverterを使用する方法について。
|
Excelファイルからpdfファイルを生成するJODConverter(バージョン4)はSpring Bootからも使う事が出来る。
JODConverterで変換を行うDocumentConverterのインスタンスを自分で生成すればいいのだが、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.1.0') // 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.1.0の例。
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import org.jodconverter.DocumentConverter; import org.jodconverter.document.DocumentFormat; import org.jodconverter.office.OfficeException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ByteArrayResource; 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 DocumentConverter 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"); DocumentFormat outputFormat = documentConverter.getFormatRegistry().getFormatByExtension("pdf"); ByteArrayOutputStream os = new ByteArrayOutputStream(); documentConverter.convert(excelFile.toFile()).to(os).as(outputFormat).execute(); return new ByteArrayResource(os.toByteArray()); } }
JODConverter 4.1.0ではOutputStreamに出力できるので、ByteArrayResourceを使ってpdfファイルを返すことが出来る。
なお、デフォルトではJODConverterが使えない状態になっているので、使える状態にする必要がある。
jodconverter.enabled=true
上記の例では、@Autowiredアノテーションを使ってDocumentConverterインスタンスを取得している。
DocumentConverterインスタンスを生成するメソッド(@Beanアノテーション付きメソッド)はJodConverterAutoConfigurationクラスにある。
@Configuration @ConditionalOnClass({DocumentConverter.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 DocumentConverter jodConverter(final OfficeManager officeManager) { return LocalConverter.make(officeManager); } }
@ConditionalOnPropertyアノテーションは、プロパティーをチェックし、ダメだった場合はコンフィグレーションを実施しないものらしい。
ここでは、jodconverter.enabled
プロパティーがtrueである場合だけコンフィグレーションを実施する。(デフォルトはfalse扱い)
デフォルトではjodconverter.enabled
プロパティーは設定されていないので、コンフィグレーションは実施されない。
Spring Bootの実行時に以下のようなエラーが発生して、Spring Bootが起動できない。
(DownloadPdfControllerクラスにある@Autowiredアノテーションが付いているDocumentConverterのインスタンスが見つからない、というエラー)
*************************** APPLICATION FAILED TO START *************************** Description: Field documentConverter in com.example.demo.rest.DownloadPdfController required a bean of type 'org.jodconverter.DocumentConverter' 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.DocumentConverter' in your configuration.
application.propertiesで「jodconverter.enabled=true
」を設定してやるとコンフィグレーションが実施されるようになる。
(DocumentConverterインスタンスが生成され、コンテナーで管理されるようになる)
Spring Boot用のJODConverterの設定はJodConverterPropertiesクラス(JavaBeans)で保持されている。
application.propertiesで設定(JodConverterPropertiesで定義されているプロパティー)を書いてやると、DocumentConverter生成時に反映される。
jodconverter.enabled=true jodconverter.officeHome=C:/Program Files/LibreOffice 5