Created
August 10, 2015 21:40
-
-
Save synthetiv/259c7eab2feb8d7547df to your computer and use it in GitHub Desktop.
Rakefile - take a source PDF of pages and spit out a printer-ready version for binding as a 8.5x5.5" book
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 'colored' | |
def x(message) | |
puts 'x '.red << message | |
abort | |
end | |
def √(message) | |
puts '√ '.green << message | |
end | |
def status(message) | |
puts '- '.cyan << message | |
end | |
task :default, [:input_pdf, :signature_count] => :clean do |task, args| | |
args = args.with_defaults \ | |
input_pdf: 'yucky.pdf', | |
signature_count: 9 | |
input_pdf = args[:input_pdf] | |
signature_count = args[:signature_count] | |
# count pages in input pdf | |
input_length = `pdftk "#{input_pdf}" dump_data | grep ^PageMediaNumber | wc -l`.to_i | |
# TODO: shouldn't REALLY have to be perfectly divisible, | |
# because you can always have signatures of different lengths. | |
x "Number of pages in input PDF must be divisible by both signature_count (i.e. #{signature_count}) and 4" unless input_length % (signature_count * 4) === 0 | |
# determine length of each signature | |
signature_length = input_length / signature_count | |
status "building #{signature_count} signatures of #{signature_length} pages (#{signature_length/4} sheets) each" | |
mkdir_p 'sig' | |
# create signatures | |
signature_count.times do |i| | |
status "creating signature ##{i}" | |
# create new human-ordered pdf for this signature | |
first_page = (i * signature_length) + 1 | |
last_page = first_page + signature_length - 1 | |
sh "pdftk Y=\"#{input_pdf}\" cat Y#{first_page}-#{last_page} output sig/#{i}.pdf" | |
# collate | |
middle = signature_length / 2 | |
sh "pdftk S=sig/#{i}.pdf shuffle Send-#{middle+1}even S1-#{middle}odd S1-#{middle}even Send-#{middle+1}odd output sig/#{i}_coll.pdf" | |
# create n-up version | |
sh "pdfnup --papersize '{8.5in,11in}' sig/#{i}_coll.pdf -o sig/#{i}_nup.pdf" | |
√ "#{i}_nup.pdf" | |
end | |
√ "all signatures created" | |
# join 'em back together | |
status "putting it all together..." | |
sh "pdftk sig/*_nup.pdf cat output yucky_nup.pdf" | |
√ 'yucky_nup.pdf ' << "(#{signature_count} signatures of #{signature_length/4} sheets each)".green | |
end | |
task :clean do | |
rm_rf 'yucky_nup.pdf' | |
rm_rf 'sig/' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment