Skip to content

Instantly share code, notes, and snippets.

@macarthy
Last active January 1, 2016 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macarthy/8080925 to your computer and use it in GitHub Desktop.
Save macarthy/8080925 to your computer and use it in GitHub Desktop.
Small utility to find the relevant field in a PDF form Usage find_pdf_field myfile.pdf searchstring
#!/bin/ruby
# Usage: ruby find_pdf_field myfile.pdf searchstring
require 'pdf_forms'
require 'pp'
pdftk = PdfForms.new("/usr/local/bin/pdftk")
pdf = ARGV[0]
fields = pdftk.get_fields pdf
search_string = ARGV[1]
matched_fields = []
def hit(object_to_search,string_to_search_for)
string_to_search_for = string_to_search_for.downcase
if (object_to_search.class == "Hash")
search_results = object_to_search.select do |key, val|
val.downcase.include?(string_to_search_for) || key.downcase.include?(string_to_search_for)
end
return true if search_results.any
elsif (object_to_search.class == "Array")
return true if object_to_search.any? { |s| s.downcase.include?(string_to_search_for) }
else
return true if object_to_search.to_s.downcase.include?(string_to_search_for)
end
return false
end
def hit_in_object(obj,string_to_search_for)
obj.instance_variables.each do |m|
return true if hit(obj.instance_variable_get(m),string_to_search_for)
end
return false
end
#find hits
fields.each do |f|
matched_fields << f if hit_in_object(f,search_string)
end
# outputs results
puts "There are #{matched_fields.length} matches"
matched_fields.each do |field|
puts field.inspect
end
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment