1

Does anyone know how to apply a password on renderPdf (org.grails.plugins:rendering:2.0.3) on Grails 3.0.8? I have tried googled but not result.

Kit
  • 13
  • 4
  • what's wrong with http://grails.org/plugin/spring-security-core ? – injecteer Aug 16 '16 at 09:56
  • my objective is to send pdf for individual users with password protected, so that others will not be able to open the pdf(eg: payslip,invoice,account statement). Will be like credit card statement. – Kit Aug 16 '16 at 12:27
  • do you need to password-protect the PDF-file itself, not the controller action, don't you? – injecteer Aug 16 '16 at 12:29
  • yes sir. i need password-protected pdf. – Kit Aug 16 '16 at 12:35
  • post the link to the rendering plugin you use... I find some irrelevant crap, either outdated or plain wrong – injecteer Aug 16 '16 at 12:38
  • http://grails.org/plugin/rendering Sorry, press enter key too quick. – Kit Aug 16 '16 at 12:40
  • I mean the url of plugin documentation – injecteer Aug 16 '16 at 12:40
  • and this is the example that i followed, http://stackoverflow.com/questions/11047692/grails-renderpdf-plugin-how-does-it-work – Kit Aug 16 '16 at 12:45
  • http://grails.org/plugin/rendering -- I found this one as well. It is outdated `compile "org.grails.plugins:rendering:1.0.0"` and the documentation link returns 404 – injecteer Aug 16 '16 at 12:48
  • it has a Grails 3 compatible version **org.grails.plugins:rendering:2.0.3** but is lack of documentation. – Kit Aug 16 '16 at 12:57
  • basically you're gonna need to introduce some meta-data into your pdf file. for this you must access the `PdfWriter` API directly. I'll have some code samples on this in the evening, in the meantime your can get some inspiration here http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-12 – injecteer Aug 16 '16 at 12:57

1 Answers1

0

In order to password-protect your PDF-files you have to use the lower level stuff, like:

  import org.xhtmlrenderer.pdf.*

  void renderEncPdf( String url, out ){
    def enc = new PDFEncryption( 'userPassword'.bytes, 'ownerPassword'.bytes )

    ITextRenderer renderer
    try{
      renderer = new ITextRenderer()
      renderer.setPDFEncryption enc
      renderer.setDocument url
      renderer.layout()
      renderer.createPDF out
    }catch( Throwable e ){
      log.error e
    }finally{
      renderer.finishPDF()
    }
  }

the url should be the absolute url of your GSP-view you use for generation.

For this I didn't use any plugin (as the documentation is missing now), it's a simple itext dependency. It should be also possible to hook in with rendering plugin somehow

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • can you show me the example of the exact url? can it be a template (eg; _detail.gsp)? what if i need to pass the model in the gsp? – Kit Aug 17 '16 at 04:02
  • i found a solution on [render](http://stackoverflow.com/questions/28237595/grails-render-pdf-plugin-returns-nullpointerexception) by adding this line **def render = g.render(template: "/domainClass/pdf", model: [domain: domain])** but the image is missing. – Kit Aug 17 '16 at 07:01
  • all urls should be absolute: `http://example.com/domainlass/pdf` or `http://example.com/images/logo.png` – injecteer Aug 17 '16 at 07:24