Last active
January 1, 2016 02:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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