Last active
August 29, 2015 14:06
-
-
Save leftis/4b054f666dca35d4b7f9 to your computer and use it in GitHub Desktop.
This file contains 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
def pdf_image_tag(image, options = {}) | |
options[:src] = File.expand_path(RAILS_ROOT) + '/public' + image.split('?').first | |
Rails.logger.info "IMAGE_PATH: #{options[:src]}" | |
tag(:img, options) | |
end | |
def media_to_pdf | |
if project_have_media | |
ac = ActionController::Base.new | |
cover = WickedPdf.new.pdf_from_string( | |
ac.render_to_string('projects/pdf_cover.html.haml', | |
:locals => { :image => pdf_image_tag('/images/website/pdf_cover.png'), :project_title => title } | |
), { :orientation => 'Landscape', :page_size => 'A3' } | |
) | |
back = WickedPdf.new.pdf_from_string( | |
ac.render_to_string('projects/pdf_cover.html.haml', | |
:locals => { :image => pdf_image_tag('/images/website/pdf_back.png', :class => 'back'), :project_title => '' } | |
), { :orientation => 'Landscape', :page_size => 'A3' } | |
) | |
file = File.open(to_file_path(title, "0"), 'wb') do |file| | |
file << cover | |
end | |
file = File.open(to_file_path(title, "999"), 'wb') do |file| | |
file << back | |
end | |
media.each_with_index do |image, index| | |
index = index + 1 | |
orientation, page_size, height, width, tb_margin, lr_margin = determine_orientation_for(image) | |
pdf = WickedPdf.new.pdf_from_string( | |
ac.render_to_string('projects/image_to_pdf.html.haml', | |
:locals => { :height => height, :klass => orientation, :image => pdf_image_tag(image.data.url, :width => width.to_i, :height => height.to_i) } | |
),{ | |
:orientation => orientation, | |
:page_size => page_size, | |
:lowquality => false, | |
:page_offset => 0, | |
:dpi => 150, | |
:margin => { :top => mms(tb_margin), :left => mms(lr_margin), :bottom => mms(tb_margin), :right => mms(lr_margin) } | |
} | |
) | |
file = File.open(to_file_path(title, sprintf('%02d', index)), 'wb') do |file| | |
Rails.logger.info "FILE_CREATED: #{index}" | |
file << pdf | |
end | |
end | |
project_name = title.split('/').each(&:downcase).join.gsub(/\s+/, "") | |
dir = "#{Rails.root}/public/#{project_name}/" | |
[merge_pdfs(Dir["#{dir}*.pdf"], dir, project_name), project_name] | |
end | |
end | |
def merge_pdfs(pdfs, dir, project_name) | |
if pdfs.count > 1 | |
FileUtils.rm_rf("#{Rails.root}/public/#{project_name}/#{project_name}.pdf") if File.exists? "#{Rails.root}/public/#{project_name}/#{project_name}.pdf" | |
system "gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=#{dir}/#{project_name}.pdf #{pdfs.sort.join(' ')}" | |
Rails.root.join("public/#{project_name}", "#{project_name}.pdf") | |
end | |
end | |
def mms(pixel) | |
(pixel * 25.4)/150 | |
end | |
def to_file_path(project, image) | |
project_name, image_name = project.split('/').each(&:downcase).join.gsub(/\s+/, ""), "#{image}.pdf" | |
dir = "#{Rails.root}/public/#{project_name}/#{image_name}" | |
directory = File.dirname(dir) | |
FileUtils.mkdir_p(directory) unless File.directory?(directory) | |
Rails.root.join("public/#{project_name}", "#{image_name}") | |
end | |
def determine_orientation_for(image) | |
ratio, width, height = get_geometry(image) | |
orientation, page_size = '' | |
if ratio < 1 | |
page_size = 'A2' | |
orientation = 'Portrait' | |
else | |
page_size = 'A3' | |
orientation = 'Landscape' | |
end | |
[orientation, page_size, calculate_margins_for(ratio, width, height, page_size)].flatten | |
end | |
def calculate_margins_for(image_ratio, image_original_width, image_original_height, page_size) | |
# 72 dpi | |
# | |
# page_height, page_width = 843, 1191 if page_size == 'A3' | |
# page_height, page_width = 1648, 1191 if page_size == 'A2' | |
# 150 dpi | |
# | |
page_height, page_width = 1753, 2480 if page_size == 'A3' | |
page_height, page_width = 3507, 2480 if page_size == 'A2' | |
# 300 dpi | |
# | |
# page_height, page_width = 3507, 4960 if page_size == 'A3' | |
# page_height, page_width = 7015, 4960 if page_size == 'A2' | |
left_right_margin, top_bottom_margin = 0, 0 | |
margin = 59.055118110 | |
height, width = image_original_height, image_original_width | |
if height >= (page_height-(margin*2)) || width >= (page_width-(margin*2)) | |
while height >= (page_height-(margin*2)) || width >= (page_width-(margin*2)) | |
width = width - 1 | |
height = width/image_ratio | |
end | |
end | |
image_new_width = width | |
image_new_height = image_new_width/image_ratio | |
top_bottom_margin = (page_height - image_new_height)/2 | |
left_right_margin = (page_width - image_new_width)/2 | |
if page_size == 'A3' | |
top_bottom_margin = top_bottom_margin - margin/2 | |
end | |
[image_new_height, image_new_width, top_bottom_margin, left_right_margin] | |
end | |
def get_geometry(image) | |
geometry = Paperclip::Geometry.from_file(image.data.to_file(:original)) | |
width = geometry.width | |
height = geometry.height | |
ratio = width/height | |
[ratio, width, height] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment