Class: HexaPDF::CLI::Split

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

Overview

Splits a PDF file, putting each page into a separate file.

Instance Method Summary collapse

Methods included from Command::Extensions

#help, #help_banner

Constructor Details

#initializeSplit

:nodoc:



45
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
# File 'lib/hexapdf/cli/split.rb', line 45

def initialize #:nodoc:
  super('split', takes_commands: false)
  short_desc("Split a PDF file")
  long_desc("    The default strategy is to split a PDF into individual pages, i.e. splitting is done by\n    page number. It is also possible to split by page size where pages with the same page size\n    get put into the same output PDF.\n\n    If no OUTPUT_SPEC is specified, the resulting PDF files are named <PDF>_0001.pdf,\n    <PDF>_0002.pdf, ... when splitting by page number and <PDF>_A4.pdf, <PDF>_Letter.pdf, ...\n    when splitting by page size.\n\n    To specify a custom name, provide the OUTPUT_SPEC argument. It can contain a printf-style\n    format definition like '%04d' to specify the place where the page number should be\n    inserted. In case of splitting by page size, the place of the format defintion is replaced\n    with the name of the page size, e.g. A4 or Letter.\n\n    The optimization and encryption options are applied to each created output file.\n  EOF\n\n  options.on(\"--strategy STRATEGY\", \"-s\", [:page_number, :page_size], \"Defines how the PDF \" \\\n             \"file should be split: page_number or page_size (default: page_number)\") do |s|\n    @strategy = s\n  end\n  options.on(\"--password PASSWORD\", \"-p\", String,\n             \"The password for decryption. Use - for reading from standard input.\") do |pwd|\n    @password = (pwd == '-' ? read_password : pwd)\n  end\n  define_optimization_options\n  define_encryption_options\n\n  @password = nil\n  @strategy = :page_number\nend\n")

Instance Method Details

#execute(pdf, output_spec = pdf.sub(/\.pdf$/i, '_%04d.pdf')) ⇒ Object

:nodoc:



80
81
82
83
84
85
86
87
88
# File 'lib/hexapdf/cli/split.rb', line 80

def execute(pdf, output_spec = pdf.sub(/\.pdf$/i, '_%04d.pdf')) #:nodoc:
  with_document(pdf, password: @password) do |doc|
    if @strategy == :page_number
      split_by_page_number(doc, output_spec)
    else
      split_by_page_size(doc, output_spec)
    end
  end
end