Filling out PDF forms with JRuby


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:

require 'java'
require 'iText-5.0.6.jar'

module Pdf
  include_package "com.itextpdf.text.pdf"
  include_package ""

  def self.write
    reader ='application.pdf');
    stamper =,'completed.pdf'))
    form = stamper.acro_fields
    puts "Form has these fields: #{form.fields.key_set.to_a}"
    form.set_field("some_zipcode_field", "94115")


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.