gpc / rendering

Provides rendering of GSPs as PDFs, JPEGs, GIFs and PNGs
http://gpc.github.com/rendering
Apache License 2.0
31 stars 45 forks source link

Unable to generate pdf with grails version 2.5.0 #24

Open pandeybk opened 9 years ago

pandeybk commented 9 years ago

I am trying to render html file to pdf using rendering plugin, my controller method is,

    def download() {
    response.contentType = 'application/pdf'
    response.setHeader("Content-disposition", "attachment; filename=\"download.pdf\"")
    def items = []
    (101..105).each { number->
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        items << [number:number, bytes:bos.toByteArray()]
    }

    renderPdf (template:'download', model:[items:items])
}

And my _download.gsp file is,

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<body>
<g:each in="${items}" var="item">
    <div>This is the barcode for the number: ${item.number}</div>
    <hr/>
</g:each>
</body>
</html>

I have added following dependencies in BuildConfig.groovy file

compile ":rendering:1.0.0"

I received following stack-trace,

| Error 2015-08-06 16:35:28,925 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /wheeloflife/test/download
Stacktrace follows:
Message: null
    Line | Method
->> 1281 | getPublicDeclaredMethods in java.beans.Introspector
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1141 | getTargetMethodInfo      in     ''
|    416 | getBeanInfo . . . . . .  in     ''
|    163 | getBeanInfo              in     ''
|     31 | init . . . . . . . . . . in grails.plugin.rendering.document.RenderEnvironment
|     68 | with                     in     ''
|     60 | with . . . . . . . . . . in     ''
|     65 | generateXhtml            in grails.plugin.rendering.document.XhtmlDocumentService
|     35 | createDocument . . . . . in     ''
|     36 | render                   in grails.plugin.rendering.RenderingService
|     35 | render . . . . . . . . . in     ''
|     65 | render                   in     ''
|     59 | doCall . . . . . . . . . in RenderingGrailsPlugin$_closure3
|     18 | download                 in com.codeharmony.wheeloflife.TestController$$EPKZmnkm
|    198 | doFilter . . . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter                 in grails.plugin.cache.web.filter.AbstractFilter
|     53 | doFilter . . . . . . . . in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     62 | doFilter                 in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|   1145 | runWorker . . . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    615 | run                      in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . . . . . . . .  in java.lang.Thread

Note Update: The same code base works with grails version 2.2.4, problem only seen in grails version 2.5.0

PaulWaldo commented 9 years ago

@pandeybk you might try my branch at https://github.com/PaulWaldo/rendering/tree/feature/upgradeDependencies I have upgraded to flying-saucer-pdf-itext5:9.0.7. That may resolve your problem.

pandeybk commented 9 years ago

@PaulWaldo Thank you for your response. Is it possible to mentioned dependencies in BuildConfig or I have to compile it manually and reference it later on?

On Fri, Aug 7, 2015 at 6:36 PM, Paul Waldo notifications@github.com wrote:

@pandeybk https://github.com/pandeybk you might try my branch at https://github.com/PaulWaldo/rendering/tree/feature/upgradeDependencies I have upgraded to flying-saucer-pdf-itext5:9.0.7. That may resolve your problem.

— Reply to this email directly or view it on GitHub https://github.com/gpc/rendering/issues/24#issuecomment-128692147.

PaulWaldo commented 9 years ago

Hi @pandeybk, here is what you do:

  1. Checkout the branch
  2. Run grails maven-install to publish locally
  3. Switch to your own project
  4. Ensure mavenLocal() is in the repositories closure in BuildConfig.groovy
  5. Add compile ":rendering:1.0.2-SNAPSHOT" to the plugins section of BuildConfig.groovy

That should pull in the updated plugin for your project.

pandeybk commented 9 years ago

Hi @PaulWaldo,

I followed the steps, here comes new issue, when I run grails maven-install to publish locally first of all I received following message,

| Script 'MavenInstall' not found, did you mean:
   1) UninstallPlugin
   2) InstallPlugin
   3) InstallDependency
   4) InstallJQuery
   5) InstallTemplates

I made little bit modification in code, removed export = false portion from BuildConfig.groovy as per the suggestion in stackoverflow (http://stackoverflow.com/questions/18056918/trouble-installing-using-grails-release-plugin-2-2-1).

Now I am finally receiving following compilation error.

| Error Compilation error: startup failed:
/home/bkpandey/workspace/github/opensource/rendering/grails-app/controllers/RenderingController.groovy: -1: Repetitive method name/signature for method 'java.lang.Object withFormat(groovy.lang.Closure)' in class 'RenderingController'.
 @ line -1, column -1.
/home/bkpandey/workspace/github/opensource/rendering/grails-app/controllers/RenderingController.groovy: -1: Repetitive method name/signature for method 'java.lang.Object withFormat(groovy.lang.Closure)' in class 'RenderingController'.
 @ line -1, column -1.
2 errors
PaulWaldo commented 9 years ago

@pandeybk, I'm wondering if it is a grails or groovy version problem. Here is my build transcript:

[~/src/grails/rendering (feature/upgradeDependencies) ] paul-> cat application.properties app.grails.version=2.3.8 app.name=grails-rendering [~/src/grails/rendering (feature/upgradeDependencies) ] paul-> gvm use grails 2.3.8 Using grails version 2.3.8 in this shell. [~/src/grails/rendering (feature/upgradeDependencies) ] paul-> groovy -version Groovy Version: 2.3.6 JVM: 1.7.0_67 Vendor: Oracle Corporation OS: Mac OS X [~/src/grails/rendering (feature/upgradeDependencies) ] | Plugin packaged grails-rendering-1.0.2-SNAPSHOT.zip | POM generated: /Users/paul/src/grails/rendering/target/pom.xml... Maven install complete.

pandeybk commented 9 years ago

@PaulWaldo

Thank you for pointing to right direction actually it was JDK version. I was using 1.8 and correcting it to 1.7 solve the issue.

But this branch again throwing the same initial error message. I just checked whether my application uses correct branch. Here comes excerpt of dependency-report

+--- org.grails.plugins:rendering:1.0.2-SNAPSHOT
|    \--- org.xhtmlrenderer:flying-saucer-pdf-itext5:9.0.7
|         \--- com.itextpdf:itextpdf:5.3.0
|         \--- org.xhtmlrenderer:flying-saucer-core:9.0.7
|    \--- com.lowagie:itext:2.1.0
|         \--- bouncycastle:bcmail-jdk14:136
|         \--- bouncycastle:bcprov-jdk14:136
PaulWaldo commented 9 years ago

@pandeybk it sounds like you are still using Java 8. What are your values for java -version as well as environment variable JAVA_HOME? Grails looks to JAVA_HOME, not the PATH, so I'll bet that is where you still have the pointer to Java 8.

pandeybk commented 9 years ago

Currently I am using Intellij and here is configuration, So I think this is not related to JAVA_HOME and PATH variable.

/usr/lib/jvm/java-7-openjdk-amd64/bin/java -Dgrails.home=/opt/grails/grails-2.5.0 -Dbase.dir=/home/bkpandey/workspace/wheeloflife -Dtools.jar=/usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar -Dgroovy.starter.conf=/opt/grails/grails-2.5.0/conf/groovy-starter.conf -Xmx768M -Xms768M -XX:MaxPermSize=256m -XX:PermSize=256m -javaagent:/opt/grails/grails-2.5.0/lib/org.springframework/springloaded/jars/springloaded-1.2.3.RELEASE.jar -noverify -Dspringloaded=profile=grails -Dfile.encoding=UTF-8 -classpath /opt/grails/grails-2.5.0/lib/org.codehaus.groovy/groovy-all/jars/groovy-all-2.4.3.jar:/opt/grails/grails-2.5.0/dist/grails-bootstrap-2.5.0.jar org.codehaus.groovy.grails.cli.support.GrailsStarter --main org.codehaus.groovy.grails.cli.GrailsScriptRunner --conf /opt/grails/grails-2.5.0/conf/groovy-starter.conf "run-app -plain-output"
PaulWaldo commented 9 years ago

@pandeybk I'm not an Intellij user, so I'm scratching my head there :-) I do know that grails will use the value of JAVA_HOME when it does its magic. See http://grails.github.io/grails-doc/2.5.0/guide/gettingStarted.html. I was able to recreate your problem on the command line just by switching my JAVA_HOME to Java 8. I assume you are using Linux: can you show me the output of

echo $JAVA_HOME

from the command line? It will look something like this:

[~/src/grails/rendering (feature/upgradeDependencies) ] paul-> echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home

pandeybk commented 9 years ago

@PaulWaldo

Ok, now I running application through command line only. Here is my echo $JAVA_HOME output.

/usr/lib/jvm/jdk1.7.0_79

Still no progress and same error output.

Also if you look at my first post, I mentioned that the same code base works with grails version 2.2.4. Problem is only identified in 2.5.0 only. So I pretty sure this is not related to JAVA_HOME related.

| Error 2015-08-11 11:08:30,578 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /wheeloflife/wheel/download
Stacktrace follows:
Message: null
    Line | Method
->> 1281 | getPublicDeclaredMethods in java.beans.Introspector
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1141 | getTargetMethodInfo      in     ''
|    416 | getBeanInfo . . . . . .  in     ''
|    163 | getBeanInfo              in     ''
|     31 | init . . . . . . . . . . in grails.plugin.rendering.document.RenderEnvironment
|     68 | with                     in     ''
|     60 | with . . . . . . . . . . in     ''
|     65 | generateXhtml            in grails.plugin.rendering.document.XhtmlDocumentService
|     35 | createDocument . . . . . in     ''
|     36 | render                   in grails.plugin.rendering.RenderingService
|     35 | render . . . . . . . . . in     ''
|     65 | render                   in     ''
|     59 | doCall . . . . . . . . . in RenderingGrailsPlugin$_closure3
|     90 | download                 in com.codeharmony.wheeloflife.WheelController
|    198 | doFilter . . . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter                 in grails.plugin.cache.web.filter.AbstractFilter
|     53 | doFilter . . . . . . . . in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     62 | doFilter                 in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|   1145 | runWorker . . . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    615 | run                      in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . . . . . . . .  in java.lang.Thread
PaulWaldo commented 9 years ago

I'm sorry @pandeybk, I misunderstood. I'm not familiar with the guts of the plugin to be of much help. One thing you can try is setting debug mode for the plugin debug 'grails.app.services.grails.plugin.rendering' I found this to be helpful in seeing the XHTML from the GSP. You can then copy that XHTML into one of the many web validators to see if it is well-formed.

pandeybk commented 9 years ago

@PaulWaldo thank you for the support and effort. I will let you know if I found any solution. Currently I implemented pdfbox as my PDF generation service.

Thank you again.

zaccak commented 9 years ago

I have the same problem on Grails 2.5.1 and Rendering 1.0.0. Exact same code on Grails 2.3.11 worked fine.

chery-qualset-hcd-ca-gov commented 9 years ago

This is what i used in my Grails 2.4.3 app. BuildConfig:

    // inherit Grails' default dependencies
    inherits("global") {
        // specify dependency exclusions here; for example, uncomment this to disable ehcache:
        // excludes 'ehcache'
        excludes 'commons-io', 'core-renderer', 'itext', 'org.grails:grails-docs'
    }

// more config...

dependencies {
        compile 'org.xhtmlrenderer:flying-saucer-core:9.0.7'
        compile 'org.xhtmlrenderer:flying-saucer-pdf:9.0.7'
        compile 'com.lowagie:itext:2.1.7'

// more config...
zaccak commented 9 years ago

I tried your config settings and still get the same issue.

zaccak commented 9 years ago

Found the solution to getting it to work on Grails 2.5.1, just add this dependency in your buildConfig: dependencies { runtime 'org.springframework:spring-test:4.1.6.RELEASE' }

rvargas commented 9 years ago

Thanks @zaccak that works :+1: