Module: StickyFlag::Tags::PDF

Defined in:
lib/stickyflag/tags/pdf.rb

Class Method Summary collapse

Class Method Details

.clear(file_name, pdftk_path = 'pdftk') ⇒ Object



95
96
97
# File 'lib/stickyflag/tags/pdf.rb', line 95

def clear(file_name, pdftk_path = 'pdftk')
  write_tags_to(file_name, [], pdftk_path)
end

.config_valuesObject



14
15
16
# File 'lib/stickyflag/tags/pdf.rb', line 14

def config_values
  [ :pdftk_path ]
end

.extensionsObject



11
12
13
# File 'lib/stickyflag/tags/pdf.rb', line 11

def extensions
  [ '.pdf' ]
end

.get(file_name, pdftk_path = 'pdftk') ⇒ Object



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
# File 'lib/stickyflag/tags/pdf.rb', line 18

def get(file_name, pdftk_path = 'pdftk')
  stdout_str = ''
  stderr_str = ''

  begin
    Open3.popen3(pdftk_path, file_name.to_s, 'dump_data_utf8') do |i, o, e, t|
      out_reader = Thread.new { o.read }
      err_reader = Thread.new { e.read }
      i.close
      stdout_str = out_reader.value
      stderr_str = err_reader.value

      stdout_str.force_encoding("UTF-8") if RUBY_VERSION >= "1.9.0"
      stderr_str.force_encoding("UTF-8") if RUBY_VERSION >= "1.9.0"
    end
  rescue Exception
    raise Thor::Error.new("ERROR: Failed to get tags for #{file_name}; pdftk call failed")
  end
  if stderr_str.start_with?("Error: ") || stderr_str.include?("Errno::ENOENT")
    raise Thor::Error.new("ERROR: Failed to get tags for #{file_name}; pdftk call failed")
  end

  # More than one of these shouldn't be possible, but try to recover if
  # it somehow happens.
  matches = stdout_str.scan(/InfoKey: X-StickyFlag-Flags\nInfoValue: (.*?)\n/)
  return [] if matches.empty?

  tags = []

  matches.each do |m|
    match_string = m[0]
    match_tags = match_string.split(',').map { |t| t.empty? ? nil : t.strip }.compact
    next if match_tags.empty?
  
    tags.concat(match_tags)
  end

  tags
end

.set(file_name, tag, pdftk_path = 'pdftk') ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/stickyflag/tags/pdf.rb', line 77

def set(file_name, tag, pdftk_path = 'pdftk')
  tags = get(file_name, pdftk_path)
  return if tags.include? tag

  tags << tag
  
  write_tags_to(file_name, tags, pdftk_path)
end

.unset(file_name, tag, pdftk_path = 'pdftk') ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/stickyflag/tags/pdf.rb', line 86

def unset(file_name, tag, pdftk_path = 'pdftk')
  tags = get(file_name, pdftk_path)
  return unless tags.include? tag

  tags.delete(tag)
  
  write_tags_to(file_name, tags, pdftk_path)
end

.write_tags_to(file_name, tags, pdftk_path = 'pdftk') ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/stickyflag/tags/pdf.rb', line 58

def write_tags_to(file_name, tags, pdftk_path = 'pdftk')
  info = Tempfile.new_with_encoding ['sfpdftag', '.txt']
  begin
    info.write("InfoKey: X-StickyFlag-Flags\n")
    info.write("InfoValue: #{tags.join(', ')}\n")
    info.close
  
    outpath = File.tmpnam('.pdf')
    ret = system(pdftk_path, file_name, 'update_info', info.path, 'output', outpath)
    unless ret == true
      raise Thor::Error.new("ERROR: Failed to write tag for #{file_name}; pdftk call failed")
    end
    
    FileUtils.mv outpath, file_name
  ensure
    info.unlink
  end
end