-
Notifications
You must be signed in to change notification settings - Fork 37
/
run.rb
71 lines (52 loc) · 1.69 KB
/
run.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require_relative '../lib/fillable-pdf'
BASE64_PHOTO = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' # rubocop:disable Layout/LineLength
# opening a fillable PDF
pdf = FillablePDF.new('input.pdf')
# total number of fields
if pdf.any_fields?
puts "The form has a total of #{pdf.num_fields} fields."
else
puts 'The form is not fillable.'
end
puts
# setting form fields
pdf.set_fields({first_name: 'Richard', last_name: 'Rahl'})
pdf.set_fields(
{football: 'Yes', baseball: 'Yes', basketball: 'Yes', nascar: 'Yes', hockey: 'Yes', rugby: 'Yes'},
generate_appearance: false
)
pdf.set_field(:date, Time.now.strftime('%B %e, %Y'))
pdf.set_field(:newsletter, 'Off') # uncheck the checkbox
pdf.set_field(:language, 'dart') # select a radio button option
pdf.set_image_base64(:photo, BASE64_PHOTO)
pdf.set_image(:signature, 'signature.png')
# list of fields
puts "Fields hash: #{pdf.fields}"
puts
# list of field names
puts "Keys: #{pdf.names}"
puts
# list of field values
puts "Values: #{pdf.values}"
puts
# Checking field type
if pdf.field_type(:rugby) == FillablePDF::Field::BUTTON
puts "Field 'football' is of type BUTTON"
else
puts "Field 'football' is not of type BUTTON"
end
puts
# Renaming field
pdf.rename_field :last_name, :surname
puts "Renamed field 'last_name' to 'surname'"
puts
# Removing field
pdf.remove_field :marketing
puts "Removed field 'marketing'"
# saving the filled out PDF in another file
pdf.save_as('output.pdf')
# saving another copy of the filled out PDF in another file and making it non-editable
pdf = FillablePDF.new('output.pdf')
pdf.save_as 'output.flat.pdf', flatten: true
# closing the document
pdf.close