0

我正在尝试使用渲染插件将 html 文件渲染为 pdf,我的控制器方法是,

    def download() {
    response.contentType = 'application/pdf'
    response.setHeader("Content-disposition", "attachment; filename=\"download.pdf\"")
    def items = []
    (101..105).each { number->
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        items << [number:number, bytes:bos.toByteArray()]
    }

    renderPdf (template:'download', model:[items:items])
}

我的 _download.gsp 文件是,

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<body>
<g:each in="${items}" var="item">
    <div>This is the barcode for the number: ${item.number}</div>
    <hr/>
</g:each>
</body>
</html>

我在 BuildConfig.groovy 文件中添加了以下依赖项

compile ":rendering:1.0.0"

我收到了以下堆栈跟踪,

| Error 2015-08-06 16:35:28,925 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /wheeloflife/test/download
Stacktrace follows:
Message: null
    Line | Method
->> 1281 | getPublicDeclaredMethods in java.beans.Introspector
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1141 | getTargetMethodInfo      in     ''
|    416 | getBeanInfo . . . . . .  in     ''
|    163 | getBeanInfo              in     ''
|     31 | init . . . . . . . . . . in grails.plugin.rendering.document.RenderEnvironment
|     68 | with                     in     ''
|     60 | with . . . . . . . . . . in     ''
|     65 | generateXhtml            in grails.plugin.rendering.document.XhtmlDocumentService
|     35 | createDocument . . . . . in     ''
|     36 | render                   in grails.plugin.rendering.RenderingService
|     35 | render . . . . . . . . . in     ''
|     65 | render                   in     ''
|     59 | doCall . . . . . . . . . in RenderingGrailsPlugin$_closure3
|     18 | download                 in com.codeharmony.wheeloflife.TestController$$EPKZmnkm
|    198 | doFilter . . . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter                 in grails.plugin.cache.web.filter.AbstractFilter
|     53 | doFilter . . . . . . . . in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     62 | doFilter                 in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|   1145 | runWorker . . . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    615 | run                      in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . . . . . . . .  in java.lang.Thread

注意更新:相同的代码库适用于 grails 版本 2.2.4,问题仅在 grails 版本 2.5.0 中出现

4

1 回答 1

1

不知道您是否仍在寻找答案,但添加

runtime "org.springframework:spring-test:4.1.5.RELEASE"

作为您项目的依赖项解决了这个问题。

https://github.com/gpc/rendering/issues/17

于 2015-10-14T09:12:59.707 回答