とりあえずApache PDFBoxを使ってみる

まずApache PDFBoxをインストール(Gradle)

compile 'org.apache.pdfbox:pdfbox:2.0.5'



とりあえずコードを書いてみる

@Path("/job")
public class Resource {
    @GET
    @UnitOfWork
    @Path("/pdf")
    @Produces(MediaType.TEXT_HTML)
    public PDDocument pdf() throws Exception {
    PDDocument document = new PDDocument();
    PDPage blankPage = new PDPage();
        document.addPage( blankPage );
        // Save the newly created document
        document.save("BlankPage.pdf");
            
        // finally make sure that the document is properly
        // closed.
        document.close();
    return document;
     }
}



実行したらエラー。

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html.



調べてみたら、JSONサービスが足りてなかったっぽいので足す。

compile 'org.glassfish.jersey.media:jersey-media-moxy:2.22.2'



もう一回実行したらまたエラー。

ERROR [2017-05-01 11:49:39,487] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: d92592dce5a88ddf
! org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class org.apache.pdfbox.pdmodel.PDDocument, genericType=class org.apache.pdfbox.pdmodel.PDDocument.
! at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)



@Produces(MediaType.TEXT_HTML)から@Produces(MediaType.APPLICATION_JSON)に変更してみた。



JSONで何かが帰ってきたから成功なのかこれは?
{“allSecurityToBeRemoved”:false,“documentInformation”:{},“resourceCache”:{},“version”:1.4}

BlankPage.pdfというファイルが新たに保存されていたから成功かな…