ricardogobbo
ricardogobbo

Reputation: 1740

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?

Upvotes: 0

Views: 2152

Answers (2)

ricardogobbo
ricardogobbo

Reputation: 1740

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.

Upvotes: 2

Joe
Joe

Reputation: 1219

Template files must start with a _ (underscore). Try adding an underscore to the beginning of the file. Also try a simple filename. Below is a working example of mine.

renderPdf(template: "print", model: [tipInstance: tipInstance,mdeLogo: mdeLogo], filename: "TipLine.pdf")

Do you have an image in the template you are rendering? Try taking out the image and see what happens. Images take extra effort using the rendering plugin.

Did you include the following DOCTYPE as your first line of your template?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Upvotes: 0

Related Questions