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(<<~EOF)
    The default strategy is to split a PDF into individual pages, i.e. splitting is done by
    page number. It is also possible to split by page size where pages with the same page size
    get put into the same output PDF.

    If no OUTPUT_SPEC is specified, the resulting PDF files are named <PDF>_0001.pdf,
    <PDF>_0002.pdf, ... when splitting by page number and <PDF>_A4.pdf, <PDF>_Letter.pdf, ...
    when splitting by page size.

    To specify a custom name, provide the OUTPUT_SPEC argument. It can contain a printf-style
    format definition like '%04d' to specify the place where the page number should be
    inserted. In case of splitting by page size, the place of the format defintion is replaced
    with the name of the page size, e.g. A4 or Letter.

    The optimization and encryption options are applied to each created output file.
  EOF

  options.on("--strategy STRATEGY", "-s", [:page_number, :page_size], "Defines how the PDF " \
             "file should be split: page_number or page_size (default: page_number)") do |s|
    @strategy = s
  end
  options.on("--password PASSWORD", "-p", String,
             "The password for decryption. Use - for reading from standard input.") do |pwd|
    @password = (pwd == '-' ? read_password : pwd)
  end
  define_optimization_options
  define_encryption_options

  @password = nil
  @strategy = :page_number
end

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