Class: HexaPDF::CLI::Modify

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

Overview

Modifies a PDF file:

  • Decrypts or encrypts the resulting output PDF file.

  • Generates or deletes object and cross-reference streams.

  • Optimizes the output PDF by merging the revisions of a PDF file and removes unused entries.

See: HexaPDF::Task::Optimize

Instance Method Summary collapse

Methods included from Command::Extensions

#help, #help_banner

Constructor Details

#initializeModify

:nodoc:



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
82
83
84
# File 'lib/hexapdf/cli/modify.rb', line 52

def initialize #:nodoc:
  super('modify', takes_commands: false)
  short_desc("Modify a PDF file")
  long_desc(<<~EOF)
    This command modifies a PDF file. It can be used, for example, to select pages that should
    appear in the output file and/or rotate them. The output file can also be
    encrypted/decrypted and optimized in various ways.
  EOF

  @password = nil
  @pages = '1-e'
  @embed_files = []
  @annotation_mode = nil

  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("-i", "--pages PAGES", "The pages of the input file that should be used " \
             "(default: 1-e)") do |pages|
    @pages = pages
  end
  options.on("-e", "--embed FILE", String, "Embed the file into the output file (can be " \
             "used multiple times)") do |file|
    @embed_files << file
  end
  options.on("--annotations MODE", [:remove, :flatten], "Handling of annotations (either " \
             "remove or flatten)") do |mode|
    @annotation_mode = mode
  end
  define_optimization_options
  define_encryption_options
end

Instance Method Details

#execute(in_file, out_file) ⇒ Object

:nodoc:



86
87
88
89
90
91
92
93
94
95
# File 'lib/hexapdf/cli/modify.rb', line 86

def execute(in_file, out_file) #:nodoc:
  maybe_raise_on_existing_file(out_file)
  with_document(in_file, password: @password, out_file: out_file) do |doc|
    arrange_pages(doc) unless @pages == '1-e'
    handle_annotations(doc)
    @embed_files.each {|file| doc.files.add(file, embed: true) }
    apply_encryption_options(doc)
    apply_optimization_options(doc)
  end
end