Module: Generator

Defined in:
lib/generator_pdf.rb

Class Method Summary collapse

Class Method Details

.insert_code_png(pdf, string, x, y) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/generator_pdf.rb', line 13

def Generator.insert_code_png(pdf, string, x, y)
	barcode = Barby::Code128B.new(string)
	outputter = Barby::PngOutputter.new(barcode)
	outputter.height = 30
	outputter.margin = 0
	blob = outputter.to_svg #Raw PNG data
	data = StringIO.new(blob)
	pdf.image data, :at => [x, y], :width => 70, :height => 30

end

.insert_code_svg(pdf, string, x, y, width, height) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generator_pdf.rb', line 24

def Generator.insert_code_svg(pdf, string, x, y, width, height)
	barcode = Barby::Code128B.new(string)
	outputter = Barby::SvgOutputter.new(barcode)
	outputter.height = height
	outputter.margin = 0
	blob = outputter.to_svg #Raw PNG data
	data = StringIO.new(blob)
	pdf.bounding_box([x,y], :width => width, :height => height) do
		pdf.svg data, :vposition => :bottom, :position => :center
	end
end

.insert_qr_svg(pdf, string, x, y, width, height) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generator_pdf.rb', line 36

def Generator.insert_qr_svg(pdf, string, x, y, width, height)
	barcode = Barby::QrCode.new(string, size: 4)
	outputter = Barby::SvgOutputter.new(barcode)
	outputter.height = height
	outputter.margin = 0
	blob = outputter.to_svg #Raw PNG data
	# File.open('barcode.svg', 'wb'){|f| f.write blob }
	data = StringIO.new(blob)
	pdf.bounding_box([x,y], :width => width, :height => height) do
		pdf.svg data, :vposition => :bottom, :position => :center, :bottom_margin => 0.4.cm
		
	end
end

.render(template, data, output_filename) ⇒ Object



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/generator_pdf.rb', line 50

def Generator.render(template, data, output_filename)
	validation = {
		font: String,
		size: Array,
		boxes: Array
	}
	box_validation = {
		position: Array,
		size: Array,
		align: String,
		font_size: Integer
	}
	# Do validation on template general shape
	template_shape_validation = template.valid_template?validation
	if template_shape_validation[:valid] then
		#If it is valid, the try to validate each defined box in the template
		template[:boxes].each_with_index{ |box,index|
				box_shape_validation = box.valid_template?box_validation
				if not box_shape_validation[:valid] then
					raise ArgumentError.new("#{box_shape_validation[:reason]} on box number #{index}")
				end
		}
	else
		raise raise ArgumentError.new(template_shape_validation[:reason])
	end
	# Check if the amount of data given fits the template
	if(template[:boxes].count != data.count) then
		raise ArgumentError.new("Data input should have the same lenght as available boxes. "\
		 			"Expecting #{template[:boxes].count} but #{data.count} given")
	end

	# From here template seems to be valid
	pdf = Prawn::Document.new(:page_size => template[:size], :margin => 0)
	pdf.font template[:font]
	template[:boxes].each_with_index do |box,index|
		value = data[index]
		if value.start_with?('text:', 'qr:', 'code128:', 'base64:') then
			if value.start_with?('text:') then
				pdf.text_box(
					value.sub('text:',''),
					:at => box[:position],
					:size => box[:font_size],
					:width => box[:size][0],
					:height=> box[:size][1],
					:overflow => :shrink_to_fit,
					:disable_wrap_by_char => true,
					:align => box[:align].to_sym
				)
			elsif value.start_with?('code128:') then
				insert_code_svg(
					pdf,
					(value.sub('code128:','')),
					box[:position][0],
					box[:position][1],
					box[:size][0],
					box[:size][1]
				)
			elsif value.start_with?('qr:') then
				insert_qr_svg(
					pdf,
					value.sub('qr:',''),
					box[:position][0],
					box[:position][1],
					box[:size][0],
					box[:size][1]
				)	
			end
		else
			pdf.text_box(
				value,
				:at => box[:position],
				:size => box[:font_size],
				:width => box[:size][0],
				:height=> box[:size][1],
				:overflow => :shrink_to_fit,
				:disable_wrap_by_char => true,
				:align => box[:align].to_sym
			)
		end
	end
	pdf.render_file output_filename
end

.render_bounds(template, output_filename) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/generator_pdf.rb', line 133

def Generator.render_bounds(template, output_filename)
	validation = {
		font: String,
		size: Array,
		boxes: Array
	}
	box_validation = {
		position: Array,
		size: Array,
		align: String,
		font_size: Integer
	}
	# Do validation on template general shape
	template_shape_validation = template.valid_template?validation
	if template_shape_validation[:valid] then
		#If it is valid, the try to validate each defined box in the template
		template[:boxes].each_with_index{ |box,index|
				box_shape_validation = box.valid_template?box_validation
				if not box_shape_validation[:valid] then
					raise ArgumentError.new("#{box_shape_validation[:reason]} on box number #{index}")
				end
		}
	else
		raise raise ArgumentError.new(template_shape_validation[:reason])
	end

	# From here template seems to be valid
	pdf = Prawn::Document.new(:page_size => template[:size], :margin => 0)
	pdf.font template[:font]
	pdf.stroke_bounds
	template[:boxes].each do |box|
		pdf.bounding_box(box[:position], :width => box[:size][0], :height => box[:size][1]) do
			pdf.stroke_bounds
		end
	end
	pdf.render_file output_filename
end