Search code examples
javaflying-saucer

Why am I suddenly getting an NPE on counters in Flying Saucer?


I have an html template that I have been using with Flying Saucer PDF since last November. I rebuilt the project recently and suddenly the same HTML and the same code is throwing an NPE. I narrowed it down to the page counters. When I remove them it works. Here is a sample of my html that demonstrates the issue.

<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <style type="text/css">

        @page {
          @top-right {
            content: element(header);
          }
        }
        #pagenumber:before {
          content: counter(page);
        }

        #pagecount:before {
            content: counter(pages);
        }

    </style>
</head>

<body>
<header id="header" class="clearfix">

    <div class="page-header">
        <span class="label">Order Status</span><br/>
        Page <div id="pagenumber"></div> of <div id="pagecount"></div>
    </div>
</header>

<div>Hello World</div>
</body>
</html>

I am sending that as the payload to a small webservice that uses flying saucer to render it and send it back. This is my code in the service:

@RequestMapping(value="/render", method= RequestMethod.POST)
public void renderPdf(@RequestBody String xhtmlContent, HttpServletResponse response) {

    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(xhtmlContent);
    renderer.layout(); //ERROR HAPPENS HERE

    try  {
        response.addHeader("Content-disposition", "attachment;filename=invoice.pdf");
        response.setContentType("application/x-octet-stream");
        renderer.createPDF(response.getOutputStream());
    } catch (Exception e) {
        logger.error("Error creating pdf: ", e);
    }

}

And I get the following stack trace:

java.lang.NullPointerException: null
        at org.xhtmlrenderer.layout.breaker.Breaker.getLanguage(Breaker.java:136) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.breaker.Breaker.getBreakPointsProvider(Breaker.java:124) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.breaker.Breaker.doBreakText(Breaker.java:147) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.breaker.Breaker.breakText(Breaker.java:112) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.InlineBoxing.layoutText(InlineBoxing.java:848) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.InlineBoxing.layoutContent(InlineBoxing.java:201) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layoutInlineChildren(BlockBox.java:983) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layoutChildren(BlockBox.java:964) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:847) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:776) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:321) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:299) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutContent(BlockBoxing.java:90) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layoutChildren(BlockBox.java:967) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:847) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:776) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:321) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:299) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutContent(BlockBoxing.java:90) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layoutChildren(BlockBox.java:967) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:847) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:776) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:321) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:299) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.layout.BlockBoxing.layoutContent(BlockBoxing.java:90) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layoutChildren(BlockBox.java:967) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:847) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:776) ~[flying-saucer-core-9.1.4.jar:na]
        at org.xhtmlrenderer.pdf.ITextRenderer.layout(ITextRenderer.java:229) ~[flying-saucer-pdf-9.1.4.jar:na]
        at com.byrne.pdfprinter.pdf.PdfController.renderPdf(PdfController.java:26) ~[classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_112]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_112]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.4.jar:8.5.4]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_112]

Again, let me reiterate, this code worked before I tried to rebuild it. I did not change my flying saucer version, but I was using RELEASE as my version in Maven. So just to ensure there weren't issues I specified concrete version numbers going back to the date when it worked and a little before and it still happens.

As I post this I'm tracing through the source but haven't found anything yet.


Solution

  • The version 9.1.5 of flying saucer project fixed the problem. The 9.1.4 is to blame.