0

Please suggest a ruby library/approach which can be used to fill pdf form fields with data from database.

Here, http://oldwiki.rubyonrails.org/rails/pages/HowtoGeneratePDFs in PDF fill form section, it has been explained how to create a FDF file and use it to send data to PDF. But in the approach suggested the pdf form must be created in a version of Acrobat prior to 7.0, they use a different method of creating a form, using xml and the method won’t work.

I have also come across Jruby iText but if there's a ruby based solution that would help http://www.mikeperham.com/2011/02/15/filling-out-pdf-forms-with-jruby/

I need to integrate the solution with rails 3.1 and ruby 1.9.3

Surbhi Gupta
  • 56
  • 1
  • 4

2 Answers2

1

I created a gem called pdf_ravager to accomplish this. It basically uses the approach that mperham found (and thus relies on JRuby), but it also has the enhanced ability to process XFA forms (XML-based forms created from Adobe LiveCycle). I also created a really simple DSL that makes the process more readable. Here's an example:

require 'pdf_ravager'

data = {:name => 'Bob', :gender => 'm', :relation => 'Uncle' }

info = pdf do
  text 'name', data[:name]
  text 'name_stylized', "<b>#{data[:name]}</b>", :rich => true
  radio_group 'sex' do
    fill 'male'   if data[:gender] == 'm'
    fill 'female' if data[:gender] == 'f'
  end
  check 'related' if data[:relation]
  checkbox_group 'relation' do
    case data[:relation]
    when 'Mom', 'Dad'
      check 'parent'
    when 'Brother', 'Sister'
      check 'sibling'
    else
      check 'other'
    end
  end
end

info.ravage '/tmp/info.pdf', :out_file => '/tmp/info_filled.pdf'
# if you'd like the populated form to be read-only:
info.ravage '/tmp/info.pdf', :out_file => '/tmp/info_filled.pdf', :read_only => true

It's still very young, but it should work for simple forms right now. I hope to tighten it up and add more features soon. Feedback/pull requests welcome!

Abe Voelker
  • 30,124
  • 14
  • 81
  • 98
1

Prawn: Fast, Nimble PDF Generation For Ruby

And

PDF::Writer http://www.bin-co.com/blog/2007/11/creating-pdf-in-ruby-on-rails-pdfwriter/

crazycrv
  • 2,395
  • 2
  • 19
  • 20
  • The Rfpdf Plugin for Rails. Generate PDF output from Rails templates using FPDF. The plugin includes Chinese, Korean and Japanese language ports. Can be Found at http://rubyforge.org/projects/rfpdf/ – crazycrv Jan 20 '12 at 06:52
  • New version at https://github.com/edwinmoss/rfpdf – crazycrv Jan 20 '12 at 07:00