Class: HexaPDF::CLI::Form

Inherits:
Command
  • Object
show all
Defined in:
lib/hexapdf/cli/form.rb

Overview

Processes a PDF that contains an interactive form (AcroForm).

Instance Method Summary collapse

Methods included from Command::Extensions

#help_banner

Constructor Details

#initializeForm

:nodoc:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hexapdf/cli/form.rb', line 46

def initialize #:nodoc:
  super('form', takes_commands: false)
  short_desc("Show form fields and fill out a form")
  long_desc(<<~EOF)
    Use this command to process interactive PDF forms.

    If the the output file name is not given, all form fields are listed in page order. Use
    the global --verbose option to show additional information like field type and location.

    If the output file name is given, the fields can be interactively filled out. By
    additionally using the --template option, the data for the fields is read from the given
    template file instead of the standard input.
  EOF

  options.on("--password PASSWORD", "-p", String,
             "The password for decryption. Use - for reading from standard input.") do |pwd|
    @password = (pwd == '-' ? read_password : pwd)
  end
  options.on("--template TEMPLATE_FILE", "-t TEMPLATE_FILE",
             "Use the template file for the field values") do |template|
    @template = template
  end
  options.on("--[no-]viewer-override", "Let the PDF viewer override the visual " \
             "appearance. Default: use setting from input PDF") do |need_appearances|
    @need_appearances = need_appearances
  end
  options.on("--[no-]incremental-save", "Append the changes instead of rewriting the " \
             "whole file. Default: true") do |incremental|
    @incremental = incremental
  end

  @password = nil
  @template = nil
  @need_appearances = nil
  @incremental = true
end

Instance Method Details

#execute(in_file, out_file = nil) ⇒ Object

:nodoc:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hexapdf/cli/form.rb', line 83

def execute(in_file, out_file = nil) #:nodoc:
  maybe_raise_on_existing_file(out_file) if out_file
  with_document(in_file, password: @password, out_file: out_file,
                incremental: @incremental) do |doc|
    if !doc.acro_form
      raise "This PDF doesn't contain an interactive form"
    elsif out_file
      doc.acro_form[:NeedAppearances] = @need_appearances unless @need_appearances.nil?
      if @template
        fill_form_with_template(doc)
      else
        fill_form(doc)
      end
    else
      list_form_fields(doc)
    end
  end
end