Fill PDF form using Ruby
Asked Answered
N

3

13

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?

Nighthawk answered 10/11, 2015 at 2:28 Comment(3)
I can't seem to be able to get the actual PDF file from the link you have in the OP. Do you happen to know what kind of PDF form you have in the document? There are several different kinds and this particular one might be unsupported.Multidisciplinary
didn't try it but you might want to try github.com/abevoelker/pdf_ravager [note: Jruby only]Exoenzyme
The PDF contains a pure XFA form (XFA forms are not part of the PDF specification and, therefore, not universally supported).Hogan
L
9

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.

Logorrhea answered 18/11, 2015 at 11:13 Comment(0)
W
6

pdf-form gem will work only for the pdf's generated by Adobe Acrobat. Please follow this issue

Make sure in your PDF, you created text field through adobe acrobat

Wien answered 17/11, 2015 at 5:42 Comment(2)
pdftk installation fails on El Capitan 10.11Merganser
Fix detailed here: gist.github.com/jvenator/…Faefaeces
R
4

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/

Rocha answered 17/11, 2015 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.