Search code examples
javagrailspdfgrails-pluginflying-saucer

Grails Render PDF Plugin returns NullPointerException


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"
    }

}

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?


Solution

  • 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.