first steps

HI,

for the newbies to groovy and grails
There are many things for the experts, butfor the newbies, sometimes it´d a hard time, when justa little brick is missing.

I try to show may way to groovy and grails

Sunday, 27 May, 2012

Groovy and creating of PDF (iText)

For some next test, I want to create a PDF
(later with data of the database)

It´s the first time, I combine a Java-Lib with groovy.
It seems, that iText is state of the the art for producing PDFs.

http://itextpdf.com/

I found only few combination at google: Groovy and iText.
This link lead to empty pages (but the contents looked good)
http://swik.net/technology/dzone.com:+tech+links/Generate+a+PDF+book+with+groovy+and+iText/by7jg

This one was a great help.
http://milkedeek.wordpress.com/2012/01/03/itext-on-the-jvm/

Before the information is lost, I copy it to here.

It was very easy, strait forward. Copy th download of  iText to groovy/lib
and the imports will the solved. Some magic with Java for the newbee,
but it realy works.

goovy pdf.groovy  and the PDF-File is created


Groovy

I followed some Groovy talks during Devoxx 2011 and I was impressed by the language, but I haven’t been able to test it out, up until now. As with Jython, it was pretty easy to install and to import iText into a project. It’s actually the same process as you would import a jar into a Java project. Assuming that you’re able to do that without instructions, so here’s the Hello World code for Groovy:

01/*
02 * Created on 28-dec.-2011
03 * Creating a simple Hello World pdf in Groovy using iText 5.1.3
04 * @author: iText
05 */
06import com.itextpdf.text.Document
07import com.itextpdf.text.Paragraph
08import com.itextpdf.text.pdf.PdfWriter
09 
10// step 1
11def document = new Document()
12println("Document Created")
13 
14// step 2
15PdfWriter.getInstance(document, new FileOutputStream("document.pdf"))
16println("PdfWriter Created")
17 
18// step 3
19document.open()
20println("Document Opened")
21 
22// step 4
23document.add(new Paragraph("Hello Groovy!"))
24println("Content Added")
25 
26// step 5
27document.close()
28println("Document Closed")

As you can see, the code is straightforward and simple and it looks pretty much like Jython. I prefer Groovy over Jython because it’s much faster and actually feels like a JVM language and not like a port of another language. Still a lot of love for Python though.


1 comment:

  1. Hi,

    Glad to have been a help in your groovy programming :)

    Regards,
    Eek.

    ReplyDelete