Created
June 1, 2015 23:52
-
-
Save gabeodess/4fb085fd64638b503c95 to your computer and use it in GitHub Desktop.
Building PDF with Prawn
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
| class CollapseLabelsJob < ActiveJob::Base | |
| queue_as :default | |
| def perform(tracking_sheet) | |
| pdf_paths, destination = tracking_sheet.pdf_paths, File.join(Rails.root, "tmp/#{SecureRandom.hex}.pdf") | |
| paths = pdf_paths.map{ |i| open(i).path }.join(' ') | |
| options = "-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite" | |
| system "gs #{options} -sOutputFile=#{destination} #{paths}" | |
| Prawn::Document.generate(destination) do | |
| groups = pdf_paths.in_groups_of(4,false) | |
| groups.each_with_index do |group, i| | |
| group.each_with_index do |path, index| | |
| image_path = "tmp/#{SecureRandom.hex}.jpg" | |
| options = { | |
| :trim => '+repage', | |
| :density => 150, | |
| :sharpen => '0x1.0', | |
| :quality => '100', | |
| :rotate => 90 | |
| }.map{ |k,v| "-#{k} #{v}" }.join(' ') | |
| system "convert #{open(path).path} #{options} #{image_path}" | |
| position = [ | |
| [:left, :top], [:right, :top], | |
| # [:left, :center], [:right, :center], | |
| [:left, :bottom], [:right, :bottom] | |
| ][index] | |
| image image_path, :position => position[0], :vposition => position[1], :height => 200 | |
| FileUtils.rm(image_path) | |
| end | |
| start_new_page if group != groups.last | |
| end | |
| end | |
| tracking_sheet.update_attribute(:labels, File.open(destination)) | |
| FileUtils.rm(destination) | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are 5 images in this example, but for some reason some of them are getting bumped into additional pages instead of all rendering on the same page and I cannot figure why?
https://www.dropbox.com/s/2ce1yscqbdb5ndl/Screenshot%202015-06-01%2019.50.54.png?dl=0
Is there some kind of MB limit per page or something?