PDF插件返回NullPointerException

PDF插件返回NullPointerException

本文介绍了Grails Render PDF插件返回NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用render插件渲染pdf文件.我的控制器代码是:

I'm trying to render a pdf file with render plugin. My controller code is:

def toPDF(){
    DomainClass domainClass = DomainClass.get(params.id)


    try{
        renderPdf(template: "/domainClass/pdf", model: [domain: domainClass], filename: System.currentTimeMillis().toString() + "_" + domainClass.id.toString() + ".pdf")
    }catch(e){
        redirect action: "error"
    }

}

在开发模式下,它可以正常工作.但是在生产中,此操作将引发NullPointerException

In development mode, it works properly. But in production, this action throws NullPointerException

    2015-01-30 11:51:40,393 [http-apr-8080-exec-48] ERROR StackTrace  - Full Stack Trace:
java.lang.NullPointerException
    at org.xhtmlrenderer.swing.NaiveUserAgent.getBinaryResource(NaiveUserAgent.java:228)
    at org.xhtmlrenderer.pdf.ITextFontResolver.importFontFaces(ITextFontResolver.java:97)
    at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:178)
    at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:142)
    at grails.plugin.rendering.pdf.PdfRenderingService.doRender(PdfRenderingService.groovy:36)
    at grails.plugin.rendering.RenderingService.render(RenderingService.groovy:43)
    at grails.plugin.rendering.RenderingService.render(RenderingService.groovy:37)
    at grails.plugin.rendering.RenderingService.render(RenderingService.groovy:35)
    at grails.plugin.rendering.RenderingService.render(RenderingService.groovy:65)
    at RenderingGrailsPlugin$_closure3.doCall(RenderingGrailsPlugin.groovy:59)
    at plano.ensino.PlanoController.toPDF(PlanoController.groovy:33)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)

我已经尝试过这种模式:

I already try this mode:

def render = g.render(template: "/domainClass/pdf",
            model: [domain: domain])

    ITextRenderer renderer = new ITextRenderer()
    ByteArrayOutputStream baos = new ByteArrayOutputStream()
    byte[] b
    renderer.setDocumentFromString(render.toString());
    renderer.layout()
    renderer.createPDF(baos)
    b = baos.toByteArray()
    def filename = "file.pdf"

    response.setContentType("application/pdf")
    response.setHeader("Content-disposition", "attachment; filename=${filename}")
    response.setContentLength(b.length)
    response.getOutputStream().write(b)

我该怎么做才能解决此问题?

What can I do to fix this problem?

推荐答案

当我删除CSS文件中的自定义字体时,此问题已解决.我将这些字体更改为默认的sans-serif字体,现在一切正常.

This problem was solved when I remove custom fonts inside css file. I changed these fonts to default sans-serif font and now is all working right.

这篇关于Grails Render PDF插件返回NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:17