Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill PDF form using Ruby

Tags:

ruby

pdf

pdftk

I'm trying to programmatically fill out a pdf using ruby. The pdf has fields that permit text to be entered:

screenshot of pdf fields

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?

like image 258
dug Avatar asked Nov 10 '15 02:11

dug


2 Answers

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/

like image 72
Marcin Urbanski Avatar answered Oct 08 '22 14:10

Marcin Urbanski


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:

enter image description here

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:

enter image description here

I know of two product lines that can meet your needs:

  1. Adobe software: for instance Adobe LiveCycle ES.
  2. iText software: you can fill the form with the AGPL version of iText by injecting XML (see How can I set XFA data in a static XFA form in iTextSharp and get it to save?); you can flatten the form with iText's XFA Worker (see How can I flatten a XFA PDF Form using iTextSharp?).

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.

like image 25
Bruno Lowagie Avatar answered Oct 08 '22 15:10

Bruno Lowagie