Created
May 17, 2022 17:34
-
-
Save ashaninBenjamin/c2274687d656d54b49745b7f8020493b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'combine_pdf' # https://github.com/boazsegev/combine_pdf | |
input_filename = ARGV[0] | |
if input_filename.empty? | |
puts 'ERROR:' | |
puts 'Enter filename' | |
return | |
end | |
pdf = CombinePDF.load(input_filename) | |
booklet_pdf = CombinePDF.new | |
total_pages_count = pdf.pages.size | |
if (total_pages_count % 4).nonzero? | |
puts 'ERROR:' | |
puts "Pages count #{total_pages_count} should be divisible by 4" | |
puts 'Add pages to the pdf file' | |
return | |
end | |
page_nums = (1..total_pages_count).to_a[0..((total_pages_count / 2) - 1)] | |
page_nums_rev = (1..total_pages_count).to_a[(total_pages_count / 2)..-1].reverse | |
res_page_nums = [] | |
page_nums.each.with_index do |num, index| | |
pair = [num, page_nums_rev[index]] | |
res_page_nums << (index.odd? ? pair : pair.reverse) | |
end | |
res_page_nums.flatten! | |
res_page_nums.each do |page_num| | |
booklet_pdf << pdf.pages[page_num - 1] | |
end | |
output_filename = File.basename(input_filename, '.pdf') + '_booklet.pdf' | |
booklet_pdf.save(output_filename) | |
puts "See #{output_filename} file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment