自去年11月以来,我有一个与飞碟PDF一起使用的html模板。我最近重新构建了该项目,突然之间,相同的HTML和相同的代码引发了NPE。我将其范围缩小到页面计数器。当我删除它们时,它可以工作。这是演示此问题的html示例。

<?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>


我将其作为有效负载发送到使用飞碟渲染并发送回的小型Web服务。这是我在服务中的代码:

@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);
    }

}


我得到以下堆栈跟踪:

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]


再次,让我重申一下,此代码在尝试重建它之前就起作用了。我没有更改飞碟版本,但在Maven中使用的是RELEASE。因此,为了确保没有问题,我指定了具体的版本号,该版本号可以追溯到它起作用时的日期,并且早于它仍然会发生。

当我发布此消息时,我正在跟踪源,但尚未找到任何内容。

最佳答案

飞碟项目的9.1.5版解决了该问题。
责怪9.1.4。

07-26 09:27