views:

172

answers:

3

I am working on a web application (using Grails) which will generate a gift certificate. I'm thinking of a workflow like this:

  1. The user will pick a template which will be stored as an image.

  2. Then the text (name, date, amount etc) will be overlaid on the image to make the final certificate. There is a set of co-ordinates associated with each template which describes where to put each bit of text.

  3. There is a kind of 'live preview' in the browser which shows the user what the final certificate will look like.

  4. When the user is happy with the results, they download the certificate as a PDF and print it.

Can anyone recommend a library for Java (or Groovy) that will make it easy to do this? I'm not particularly worried about speed, as I suspect that the webapp will only be used by a few people at a time.

UPDATE: in the end I used the iText PDF library to overlay text on a PDF template using PDFStamper.

A: 

You might consider using Batik to do this as SVG. Your image would be an <img> tag and your text would be one or more <text> tags. There's a converter (called FOP, I believe) which will get you PDF output.

uckelman
Thanks for the suggestion - I can't believe I didn't think of using SVG, that's a great idea. Having the text bits as scalable object might make it much easier to figure out how to fit them into the spaces in the template without resorting to messing with the font size.
mojones
+3  A: 

You can do this with the standard Java 2D graphics libraries - create a BufferedImage from the image, get its Graphics and use drawString() to put the text on top. Of course, the text would then be part of the bitmap in the resulting PDF, and not use the full printing resolution.

Michael Borgwardt
A: 

In addition to the answers above, I have come across the groovy library GraphicsBuilder and the Grails plugin j2D which are also potiential solutions.

mojones