Skip to content

Instantly share code, notes, and snippets.

@hosiawak
Created December 16, 2010 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hosiawak/743644 to your computer and use it in GitHub Desktop.
Save hosiawak/743644 to your computer and use it in GitHub Desktop.
# java -cp ./Multivalent.jar tool.pdf.Impose -paper 42x59.4cm -dim 1x1 -margin 0inch in.pdf
require 'fileutils'
class Multivalent
PAGE_SIZES = {
'A0' => { :page_width => "84.1", :page_height => "118.9"},
'A1' => { :page_width => "59.4", :page_height => "84.1"},
'A2' => { :page_width => "42.0", :page_height => "59.4"},
'A3' => { :page_width => "29.7", :page_height => "42.0"},
'A4' => { :page_width => "21.0", :page_height => "29.7"},
'A5' => { :page_width => "14.8", :page_height => "21.0"},
'A6' => { :page_width => "10.5", :page_height => "14.8"},
'A7' => { :page_width => "7.4", :page_height => "10.5"},
'A8' => { :page_width => "5.2", :page_height => "7.4"},
'A9' => { :page_width => "3.7", :page_height => "5.2"},
'A10' => { :page_width => "2.6", :page_height => "3.7"}
}
def self.resize_data(data,page_size,orientation)
f = Tempfile.new("pdf")
f << data
f.close
self.resize(f.path,page_size,orientation)
end
# returns path to the resized outfile
def self.resize(in_file,page_size, orientation)
`#{java_binary} -cp #{Rails.root}/lib/Multivalent.jar tool.pdf.Impose -paper #{paper_size(page_size,orientation)} -dim 1x1 #{in_file}`
basename = File.basename(in_file)
path = File.dirname(in_file)
out_file ="#{path}/#{basename}-#{page_size}.pdf"
FileUtils.mv("#{path}/#{basename}-up.pdf", out_file)
FileUtils.rm(in_file)
out_file
end
def self.java_binary
r = `which java`
if r.blank?
# try AppConfig path
r = AppConfig.java_path
unless File.exist?(r)
raise "Please set :java_path in config/app_config.yml"
end
end
r.chomp
end
def self.paper_size(page_size,orientation)
h = PAGE_SIZES[page_size]
if orientation == "Portrait"
"#{h[:page_width]}x#{h[:page_height]}cm"
else
"#{h[:page_height]}x#{h[:page_width]}cm"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment