class HexaPDF::CLI::Split
Splits a PDF file, putting each page into a separate file.
Private Instance Methods
page_size_name(media_box)
click to toggle source
Tries to retrieve a page size name based on the media box. If this is not possible, the returned page size name consists of width x height.
# File lib/hexapdf/cli/split.rb, line 129 def page_size_name(media_box) @page_name_cache ||= {} return @page_name_cache[media_box] if @page_name_cache.key?(media_box) paper_size = HexaPDF::Type::Page::PAPER_SIZE.find do |_name, box| box.each_with_index.all? {|entry, index| (entry - media_box[index]).abs < 5 } end @page_name_cache[media_box] = paper_size ? paper_size[0] : "%.0fx%.0f" % media_box.values_at(2, 3) end
split_by_page_number(doc, output_spec)
click to toggle source
Splits the document into individual pages.
# File lib/hexapdf/cli/split.rb, line 92 def split_by_page_number(doc, output_spec) doc.pages.each_with_index do |page, index| output_file = sprintf(output_spec, index + 1) maybe_raise_on_existing_file(output_file) out = HexaPDF::Document.new out.pages.add(out.import(page)) apply_encryption_options(out) apply_optimization_options(out) write_document(out, output_file) end end
split_by_page_size(doc, output_spec)
click to toggle source
Splits the document into files based on the page sizes.
# File lib/hexapdf/cli/split.rb, line 105 def split_by_page_size(doc, output_spec) output_spec = output_spec.sub(/%.*?[a-zA-Z]/, '%s') out_files = Hash.new do |hash, key| output_file = sprintf(output_spec, key) maybe_raise_on_existing_file(output_file) out = HexaPDF::Document.new out.config['output_file'] = output_file hash[key] = out end doc.pages.each do |page| out = out_files[page_size_name(page.box(:media).value)] out.pages.add(out.import(page)) end out_files.each_value do |out| apply_encryption_options(out) apply_optimization_options(out) write_document(out, out.config['output_file']) end end