Class: Pdfrb::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pdfrb/cli.rb

Overview

pdfrb executable. Thor-based CLI mirroring pdftk/pdfinfo ergonomics. Each subcommand wraps a Task::* module or reads directly off the Document facade.

Instance Method Summary collapse

Instance Method Details

#decrypt(input, output) ⇒ Object



103
104
105
106
107
# File 'lib/pdfrb/cli.rb', line 103

def decrypt(input, output)
  doc = open_doc(input)
  doc.write(output)
  puts "Wrote #{output}"
end

#diff(left, right) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pdfrb/cli.rb', line 71

def diff(left, right)
  report = Pdfrb::Compare.compare(
    File.binread(left),
    File.binread(right)
  )
  puts report.summary
  unless report.equivalent?
    puts
    report.per_page_text_diffs.first(5).each do |d|
      puts "  Page #{d[:page] + 1}: #{(d[:similarity] * 100).round(1)}% similar"
    end
    unless report.font_diff[:added].empty? && report.font_diff[:removed].empty?
      puts "  Fonts added: #{report.font_diff[:added].join(', ')}" unless report.font_diff[:added].empty?
      puts "  Fonts removed: #{report.font_diff[:removed].join(', ')}" unless report.font_diff[:removed].empty?
    end
    puts "  Page count delta: #{report.page_count_delta}" unless report.page_count_delta.zero?
    exit 1
  end
end

#encrypt(input, output) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/pdfrb/cli.rb', line 93

def encrypt(input, output)
  warn "encrypt: not yet fully implemented (Phase 11 integration pending)"
  # Copy through for now.
  doc = open_doc(input)
  doc.write(output)
  puts "Wrote #{output} (encryption not yet applied)"
end

#extract_text(input, output = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/pdfrb/cli.rb', line 42

def extract_text(input, output = nil)
  doc = open_doc(input)
  texts = Pdfrb::Task::ExtractText.call(doc)
  if output
    File.write(output, texts.join("\n\n--- page break ---\n\n"))
    puts "Wrote #{output}"
  else
    texts.each_with_index { |t, i| puts "--- page #{i + 1} ---"; puts t }
  end
end

#form(input) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/pdfrb/cli.rb', line 119

def form(input)
  doc = open_doc(input)
  acro = doc.catalog[:AcroForm]
  if acro.nil?
    puts "No AcroForm in this document."
    return
  end
  fields = acro[:Fields]
  return unless fields

  fields.each do |ref|
    field = ref.is_a?(Pdfrb::Model::Reference) ? doc.object(ref) : ref
    next unless field

    name = field[:T] || "(unnamed)"
    type = field[:FT] || "?"
    value = field[:V] || "-"
    puts "/#{name}  type=#{type}  value=#{value}"
  end
end

#images(input) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/pdfrb/cli.rb', line 54

def images(input)
  doc = open_doc(input)
  Pdfrb::Task::ExtractImages.call(doc) do |info|
    puts "page #{info.page_index + 1} /#{info.name} " \
         "#{info.width}x#{info.height} filter=#{info.filter.inspect}"
  end
end

#images_add(input, image, output) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/pdfrb/cli.rb', line 110

def images_add(input, image, output)
  doc = open_doc(input)
  name = doc.images.add(image)
  doc.pages.add # just adds a blank page for now
  doc.write(output)
  puts "Added image as /#{name} and wrote #{output}"
end

#info(input) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pdfrb/cli.rb', line 19

def info(input)
  doc = open_doc(input)
  puts "File: #{input}"
  puts "PDF version: #{doc.version}"
  puts "Pages: #{doc.pages.count}"
  puts "Title: #{doc..title || '-'}"
  puts "Author: #{doc..author || '-'}"
  puts "Subject: #{doc..subject || '-'}"
  puts "Producer: #{doc..producer || '-'}"
  puts "Encrypted: #{encrypted?(doc)}"
end

#merge(output, *inputs) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
# File 'lib/pdfrb/cli.rb', line 32

def merge(output, *inputs)
  raise ArgumentError, "merge needs at least one INPUT" if inputs.empty?

  target = Pdfrb::Document.new
  inputs.each { |i| Pdfrb::Task::Merge.call(target, open_doc(i)) }
  target.write(output)
  puts "Wrote #{output} (#{target.pages.count} pages)"
end

#optimize(input, output) ⇒ Object



63
64
65
66
67
68
# File 'lib/pdfrb/cli.rb', line 63

def optimize(input, output)
  doc = open_doc(input)
  Pdfrb::Task::Optimize.call(doc)
  doc.write(output)
  puts "Wrote #{output}"
end

#versionObject



14
15
16
# File 'lib/pdfrb/cli.rb', line 14

def version
  puts "pdfrb #{Pdfrb::VERSION}"
end