Module: Sibyl::Task

Includes:
Magick
Defined in:
lib/sibyl/task.rb

Instance Method Summary collapse

Instance Method Details

#clearObject



8
9
10
11
# File 'lib/sibyl/task.rb', line 8

def clear
	@sibyl_elements = {}
	@sibyl_pdfs = []
end

#create_pdf(json, dest_dir) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sibyl/task.rb', line 30

def create_pdf(json, dest_dir)
			STDERR.puts "json: #{json}"
			data = JSON.parse(File.read(json))
			STDERR.puts "data: #{data.inspect}"
			dir = Pathname.new(json.gsub(/\.json/i, ""))
			STDERR.puts "dir: #{dir}"
			STDERR.puts "dir.basename: #{dir.basename}"
			pdf_path = Pathname.new(dest_dir.to_s).join( dir.basename.to_s.gsub(/\/$|\\$/, '') + ".pdf" )
			STDERR.puts "pdf_path: #{pdf_path}"
			pdf = nil
			tmp_images = []
			data.keys.each do |page_name|
img_path = dir.join(page_name).to_s
img_outpath = dest_dir.join(page_name).to_s
STDERR.puts "img_path: #{img_path}"
STDERR.puts "img_outpath: #{img_outpath}"
STDERR.puts "data[#{page_name}]: #{data[page_name]}"
if data[page_name].has_key? 'elements'
	img = ImageList.new(img_path)
	text = Magick::Draw.new
	elements = data[page_name]['elements']
	STDERR.puts "elements: #{elements}"
	elements.keys.each do |col|
		STDERR.puts "elments[#{col}]: #{elements[col]}"
		if elements[col].has_key? 'fontsize'
			STDERR.puts "setting fontsize: #{elements[col]['fontsize']}"
    		text.pointsize = elements[col]['fontsize'].to_f + 2
		end
		if elements[col].has_key?('x') and elements[col].has_key?('y')
			STDERR.puts "received element: #{elements[col]['x']},#{elements[col]['y']}"
			if self.respond_to? col
				STDERR.puts "Setting: #{col} at #{elements[col]['x']},#{elements[col]['y']}"
				text.annotate(img, 0,0,elements[col]['x'].to_i,elements[col]['y'].to_i + 15, self.send(col)) {
        	self.fill = 'black'
      	}
			end
		end
	end
	img.write(img_outpath)
	tmp_images.push img_outpath
else
	STDERR.puts "No elements to using default: #{img_path}"
	img_outpath = img_path
end

img = Magick::Image.ping(img_outpath).first
  	width = img.columns
  	height = img.rows

   # Image should be 300dpi
   # LETTER = 612.00 x 792.0
   # Which is 2550 px, 3300 px
   # This works out to 612 / 2550 = 0.24
   ratio = 0.24
   pdf_width = width * ratio
   pdf_height = height * ratio

	if pdf
	STDERR.puts "start_new_page"
	pdf.start_new_page(:page_size => [pdf_width, pdf_height], :margin => 0)
else
	STDERR.puts "generate_pdf"
	pdf = Prawn::Document.new(:page_size => [pdf_width, pdf_height], :margin => 0)
end
STDERR.puts "#{page_name}: Putting: #{img_outpath} of #{width},#{height} at [0,#{pdf_height}]"
pdf.image img_outpath, :at => [0,pdf_height],  :width => pdf_width
			end
			pdf.render_file pdf_path
  STDERR.puts "Saved to: #{pdf_path}"
			# Clean up tmp images
			tmp_images.each do |img_path|
File.unlink img_path
			end
			@sibyl_pdfs.push pdf_path
  return pdf_path
end

#fillObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/sibyl/task.rb', line 20

def fill
	prepare
	STDERR.puts "Obj: #{self}"
	STDERR.puts "Obj.class: #{self.class}"
	@task = ActiveSupport::Inflector.underscore(self.class.to_s).pluralize
	STDERR.puts "@task: #{@task}"
	Dir[Rails.root.join("app", "sibyl", @task, "*.json").to_s].each do |json|
		self.create_pdf(json, Rails.root.join("tmp"))
	end
end

#prepare(obj = nil) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/sibyl/task.rb', line 12

def prepare(obj = nil)
	clear unless @sibyl_elements
	obj = self if obj == nil
	obj.attributes.each do |col,val|
		STDERR.puts "attr: #{col}: #{val}"
		@sibyl_elements[col] = val
	end
end

#zip_pathObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sibyl/task.rb', line 107

def zip_path
	fill

	dirpath = Rails.root.join('tmp', "#{@task}#{self.id}-#{SecureRandom.hex(8)}")
    FileUtils.mkdir dirpath
    zip_path = dirpath.join("#{@task}.zip").to_s
    STDERR.puts "Creating: #{zip_path}"
	Zip::File.open(zip_path, Zip::File::CREATE) do |zipfile|
		@sibyl_pdfs.each do |pdf|
			STDERR.puts "Adding: #{pdf}"
			zipfile.add(Pathname.new(pdf.to_s).basename, pdf)
		end
	end
	return zip_path
end