Grails Render PDF Plugin returns NullPointerException

我怕爱的太早我们不能终老 提交于 2019-12-12 03:22:59

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/28237595/grails-render-pdf-plugin-returns-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!