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



82
83
84
85
86
# File 'lib/pdfrb/cli.rb', line 82

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

#encrypt(input, output) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/pdfrb/cli.rb', line 72

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pdfrb/cli.rb', line 98

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



89
90
91
92
93
94
95
# File 'lib/pdfrb/cli.rb', line 89

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