Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # -*- ruby -*-
- require 'combine_pdf'
- require 'pdf-reader'
- require 'optparse'
- require 'ostruct'
- class PDFSplitArgumentParser
- def self.parse(args)
- options = OpenStruct.new
- options.first_page = 1
- opts = OptionParser.new { |opts|
- opts.banner = "Usage: pdfsplit [options] <input> <output>"
- opts.separator ""
- opts.separator "Specific options:"
- opts.on("-f", "--first-page PAGE", Integer, "Specify the first page to begin spliting at") { |f| options.first_page = f }
- opts.on("-l", "--last-page PAGE", Integer, "Specify the last page to split") { |l| options.last_page = l }
- options[:help] = opts.help
- }
- opts.parse!(args)
- options
- end
- end
- options = PDFSplitArgumentParser.parse(ARGV)
- if ARGV.length != 2 then
- abort options[:help]
- end
- input_arg = ARGV[0]
- output_arg = ARGV[1]
- if !input_arg.end_with?(".pdf") || !File.file?(input_arg) then
- abort "Please specify a valid input file: #{input_arg}"
- end
- last_page = options.last_page || PDF::Reader.new(input_arg).page_count
- input_pdf = CombinePDF.load(input_arg)
- output_pdf = CombinePDF.new
- pages = 0
- input_pdf.pages.each { |page|
- if (pages += 1) >= options.first_page && pages <= last_page then
- output_pdf << page
- end
- }
- output_pdf.save "#{output_arg}#{output_arg.end_with?("pdf") ? "" : ".pdf"}"
Add Comment
Please, Sign In to add comment