Grails render PDF file

人走茶凉 提交于 2019-12-24 02:27:29

问题


I am trying to render a PDF file in my web page but using the bellow syntax I get a weird characters combination.

render file: new File ("path/to/file.pdf"), fileName: 'myPdfFile.pdf'

Does anyone knows what do I have to add more than the line above?

Thanks


回答1:


I don't think that Grails does this by default.
Take a look at PDF rendering plugins. This looks to be the freshest, but make sure it's compatible with your Grails version.
If You are using Grails 3, this could be the way to go.




回答2:


in Grails 3, you can simply do

render file: new File ("path/to/file.pdf")



回答3:


Did you try your suggested plugin? I am not able to use this dependency in my project. 1.0.0 Version does not exist, there are 2.0.0, 2.0.2 and 2.0.3 versions - but project compilation fails when I try to use them.

I have tried to download git sources and run them - website is up, but I also see error below:

Grails Runtime Exception

Error Details

Error 404: 
Servlet: grailsDispatcherServlet
URI: /

When I try to visit particular controller action it ends like this:

java.lang.reflect.InvocationTargetException

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

    at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.NullPointerException

    at java.beans.Introspector.getPublicDeclaredMethods(Introspector.java:1337)

    at java.beans.Introspector.getTargetMethodInfo(Introspector.java:1197)

    at java.beans.Introspector.getBeanInfo(Introspector.java:426)

    at java.beans.Introspector.getBeanInfo(Introspector.java:173)

    at grails.plugins.rendering.document.RenderEnvironment.init(RenderEnvironment.groovy:31)



回答4:


Coincidentally, I had the same problem yesterday but with some differences. I had to get to load a PDF from a Grails form in a new tab, using POST method to access a PHP script which generates PDF according some variables.

Anyway, I recommend you to check your header. If you get the binary file, for example, then you must define the header. So, we have for example:

Controller/Action:

    def generatesReport = {
        def url = StaticContextService.app.config.grailsPath.urlLocalServer,
        path = 'some/url/which/generates/this.pdf',         
        parameters = /* defining parameters bla bla bla */

        def bytes = SendHttpService.getYourBinaryHere(url, path, parameters)

        /* HERE IS MY ANSWER, properly */
        response.setContentType('application/pdf')
        response.setContentLength(bytes.length)
        response.setHeader('Content-Disposition', 'inline; filename="yourPdfFile.pdf"')
        ServletOutputStream outputStream = response.getOutputStream()
        outputStream.write(bytes,0,bytes.length)
        outputStream.flush()
        outputStream.close()        
    } // end method

There are many ways to define the header, and so you need to verify which settings are better for your demand (:

Don't forget to add the necessary libraries for your dependencies. Cheers!



来源:https://stackoverflow.com/questions/36569758/grails-render-pdf-file

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