Filling out PDF forms with JRuby

Feb 15, 2011

I recently had to figure out how to programmatically fill out a PDF based on the form input from a Rails application. It looks like there's nothing native to Ruby but there is a comprehensive PDF library called iText which will handle form duties. Using JRuby, we can access their Java API to fill out the form pretty easily:

Obviously sample code, not production quality, etc. Really the only hard/tedious part is mapping field names to Ruby object attribute values. If you have a complex form, you may have tens or even hundreds of set_field calls.