0

I want to programmatically fill a pdf using pdf-form gem

I was able to read the fields using the following code:

require 'pdf_forms'
pdftk = PdfForms.new('/usr/local/bin/pdftk')
fields = pdftk.get_fields('*/Desktop/sample_pdf.pdf')
for f in fields
  print f.to_s
end

But when I tried to fill the Pdf with the provide synthax, I created only an empty, not openable pdf.

pdftk.fill_form '/path/to/form.pdf', 'target.pdf', ['Value Name'] => 'Value to be inserted'

Does anybody know a working solution?

Thanks in advance!!!


Additional Information:

  • the fields of the pdf are the following: Name, Address, Dropdown1, Dropdown2, Dropdown3, Check Box4, Check Box1, Check Box3, Check Box2, Text5, Button7, Text6, Group6, %
  • I tried it with filling just one field:

pdftk.fill_form 'sample_pdf.pdf', 'sample_pdf_filled.pdf', {["Name"]=>"Value to be inserted"}

The result: I have an pdf "sample_pdf_filled.pdf", which I cannot open, because it's empty. I thought my problem is the form-filling, but it seems, that the pdf is not created correctly.

Note: I am working on a mac (unix)

Has anybody a solution?

Andres_oo
  • 17
  • 6

2 Answers2

1

Method #fill_form is defined like this:

 def fill_form(template, destination, data = {}, fill_options = {})

You example would produce something like this:

irb(main):001:0> { ['Value Name'] => 'Value to be inserted' }                                                                                   
=> {["Value Name"]=>"Value to be inserted"}      

The data is supposed to be a Hash. If you want to specify a key with a space you can use it like this: :'Value Name'.

irb(main):002:0> { :'Value Name' => 'Value to be inserted' }                                                                                    
=> {:"Value Name"=>"Value to be inserted"}  
DiodonHystrix
  • 424
  • 3
  • 7
0

I found a solution - for all MAC OS X 10.11 El Capitan User:

PDFtk Server on OS X 10.11

Community
  • 1
  • 1
Andres_oo
  • 17
  • 6