Archive for the 'Java' Category

PDF Generation in Ruby on Rails

Friday, December 22nd, 2006

Project Background

We’re getting down to the final stages of the Benefitfinder a project that I mentioned a while ago.

Step 2 of the Benefitfinder consists of a series of interview style questions, data from those questions is used to fill exisiting pdf benefit applications. We are using smart logic to ask the fewest number of questions possible to fill in all of the pdf applications needed.

The PDF Forms

The forms we are using were provided by Washington State There are 4 different pdf forms, some have as many as 6 pages of data.

The layout is very complex, lots of checkboxes and nested tables. Have a look at the Basic Health application to see for yourself.

Ruby and PDF’s

Ruby has a couple of PDF libraries PDF, one of more common libraries is PDF::Writer

However, these libraries are for writing new pdf files, in our case we needed to write data in existing pdf files. We needed to fill in the pdf forms from the State with user data.

After alot of research I came to the conclusion that it couldn’t be done in Pure Ruby.

Java, Ruby and PDF’s

Throughout my research iText kept coming it. iText is a Java library that can do pretty much anything with pdf files. I also read quite a bit about pdftk which is another great Java pdf toolkit.

Inorder to interface with the iText Java library I used the Ruby Java Bridge it was easy to install on my iBook and in a matter of minutes I was using Ruby to manipulate Java.

The Details

The final solution goes like this:

Considerations

The more technologies you throw into your app the harder it is to debug. By using this solution I went from having only Ruby to having Ruby, RubyJavaBridge, Java and iText.

Currently it is hard to find shared hosting that accomidates both Ruby and Java. In the end we went with a dedicated solution which gave us full control.

There is another way to fill in pdf forms using pdftk, this other method (still needs java) is described on the HowtoGeneratePDFs RubyonRails wiki. This howto also covers several approaches to writing new pdfs.

RJB Code

Here is a simple Ruby class (that uses RJB). This class takes a pdf form and fills in each of the acroformfields with the name of the field.

Another class I have not listed is the Importer which takes a PDF, reads in all acroformfields and saves them in the database.

NOTE: I suggest using a good naming convention for your acroformfields. For instance all of the checkboxes end in _chk which makes it easy to set them all to an x.

Java Code

Here is one of the simple Java classes I wrote to fill in the PDF file. It uses the iText library.

Closing

I look forward to Ruby PDF libraries growing and a time when I will not have use use Java for my PDF needs. However I am happy to know that it is easy to leverage the power of Java from Ruby.

NOTE: I have created a Ruby Webservice that fills in PDF forms. If you are interested in this pdf filling webservice contact me