Thursday, September 17, 2009

Tweaking grails pdf plugin

I had a little bit of problem when I wanted to use grails PDF plugin. (By the way Aaron Eischeid, thanks for providing this). However some things is not as wanted.

pdfLink: this is a nice feature for generating PDF by providing an url to the page to generate as PDF. But I do not want to provide parameters by adding them to the url in get-form.

pdfForm: Somewhat better, But this method gives some other problems. First of all, pictures is lost since the PDF only generates from string, also the layout is not automaticly included in the render call.

So I did the followin tweaks. Sorry Aron, but I had to hack into the PdfService.groovy and PdfController.groovy to get it work and also set up a template in a specific way.

This is what I did:
In PdfService.groovy I added a method to be able to generate a PDF with images for pdfForm, to do this the renderer need to know the baseUri to get find the image.

I added this code:
code

In the method pdfForm for the PdfController.groovy, I replaced code with call to the new method.
code


I then made a template with the following structure to also use the layout, without the applyTemplate, I only got the template code (also note that the template is looked for in views/pdf/):
code

I have not done this with the options for controller and action, just for template, since the g.include seams to return empty content. But I will continue to explore.

! An update. I realized that the pdf-plugin was build for grails 1.1. maybe this was the reason that the g.render in the plugin gave med nothing as a result. I was actually using grails 1.0.4. Using 1.1.1 however makes the action and controller work better. No pictures though, but they show when I am running pdf-plugin as an application after downloading the source.

I will try to find the proper place to address these issues to improve the plugin.

/Peter

5 comments:

  1. If you want to contribute code I host this project on Github. I would be glad to include your stuff once I understand it just a little better. I have a number of fixes and features I would like to get included myself. Unfortunately the project that inspired me to work on this plugin has gotten put on hold indefinitely, and my time has been focused on other things. Cool to see other people using it and writing about it

    ReplyDelete
  2. You are welcome. I am trying some reporting stuff out, so I want to find the most convinient way to do stuff. I will loock up Github to see how I can contribute. Maybe I will also take a better look at the plugin to better contribute.

    ReplyDelete
  3. I use the PD4ML library for converting HTML (including CSS styling) to PDF. It supports the HTML base element in the head, which allows you to specify a URL relative to which resources such as images are searched for. Perhaps the PDF plug-in could take a similar approach.

    <head>
    <base href="${baseURL}" />
    ...

    --Matt

    ReplyDelete
  4. Thanks for the tip Matt.

    However though I work at an university. I am not so keen on having to register to get PDF4ML even if it is free for universities. First of all it seams that I have to put up a project homepage, well we do not have it public.

    /Peter

    ReplyDelete
  5. Hi Peter,

    Sorry if I wasn't clear. I wasn't meaning to suggest that you license PD4ML, but that perhaps the Grails PDF plug-in should similarly pay attention to the presence of a <base href="..." /> tag inside the head. That way, no method signatures have to change, no additional argument needs to be passed in, and you're following standards.

    --Matt

    ReplyDelete