I'm trying to programmatically fill out a pdf using ruby. The pdf has fields that permit text to be entered:
I've had success in the past using pdftk
via the pdf_form
gem, however it doesn't find the fields in this particular pdf:
$ pry
[1] pry(main)> require 'pdf_forms'
=> true
[2] pry(main)> pdftk = PdfForms.new('/usr/local/bin/pdftk')
=> #<PdfForms::PdftkWrapper:0x007fece7af6d98 @options={}, @pdftk="/usr/local/bin/pdftk">
[3] pry(main)> pdftk.get_field_names('designation.pdf')
=> []
How can I fill out the pdf using ruby?
I am afraid you may be forced to use JRuby and iText. I've done similar things in the past and iText seems to be the best available library for working with sophisticated PDF files.
http://www.mikeperham.com/2011/02/15/filling-out-pdf-forms-with-jruby/
PdfTk is nothing more than a mighty old version of iText compiled using GCJ. Filling out forms using PdfTk is possible as long as the form contains fields defined using AcroForm technology. This isn't the case for the form you are referring to. I've opened the form in iText RUPS and this is what I see when I look inside the Form tab:
Do you see the /Fields
array? It's empty: []
. This means that there's nothing for PdfTk to fill out. Then where is the form?
The form is described using the XML Forms Architecture (XFA). There aren't many software products around that can fill out an XFA form. Heck, there aren't many PDF viewer that allow you to view the form. For instance, this is what I see when I open the form in my browser:
I know of two product lines that can meet your needs:
Being the CEO of iText Group, I'd recommend to use iText (you're using an old version already anyway if you're using PdfTk). iText is available as a Java library, so using JRuby is your best chance to meet your requirement.
Another option would be to completely redesign the form into a form based on AcroForm technology rather than XFA technology.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With