Generator_pdf 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/generator_pdf.rb +172 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ac3620a3e77790a96d18eeca1526b920e9802b46
|
4
|
+
data.tar.gz: 99957433bcfcf7e9fa5d45a8cb80b489058dcf6d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bcc5f965ecb3c0a922be17fb1aa2a12235e1657893222aa11f4be50b006d5495acd3f309a4471732c5c1197fc71493a6539b942ecdb0620c25d195876f88ad81
|
7
|
+
data.tar.gz: bb5391abb19e058985935f9ce413c34dcf678d5d3bced2c224294c199742e0a376ec2554c9dfb0f0faa4d38fd3dd378acc2b3f1485f73c855c20129500f15ee6
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require 'prawn'
|
2
|
+
require 'barby'
|
3
|
+
require 'barby/barcode/code_128'
|
4
|
+
require 'barby/outputter/png_outputter'
|
5
|
+
require 'barby/outputter/svg_outputter'
|
6
|
+
require 'stringio'
|
7
|
+
require 'prawn/measurement_extensions'
|
8
|
+
require 'prawn-svg'
|
9
|
+
require 'barby/barcode/qr_code'
|
10
|
+
|
11
|
+
module Generator
|
12
|
+
|
13
|
+
def Generator.insert_code_png(pdf, string, x, y)
|
14
|
+
barcode = Barby::Code128B.new(string)
|
15
|
+
outputter = Barby::PngOutputter.new(barcode)
|
16
|
+
outputter.height = 30
|
17
|
+
outputter.margin = 0
|
18
|
+
blob = outputter.to_svg #Raw PNG data
|
19
|
+
data = StringIO.new(blob)
|
20
|
+
pdf.image data, :at => [x, y], :width => 70, :height => 30
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def Generator.insert_code_svg(pdf, string, x, y, width, height)
|
25
|
+
barcode = Barby::Code128B.new(string)
|
26
|
+
outputter = Barby::SvgOutputter.new(barcode)
|
27
|
+
outputter.height = height
|
28
|
+
outputter.margin = 0
|
29
|
+
blob = outputter.to_svg #Raw PNG data
|
30
|
+
data = StringIO.new(blob)
|
31
|
+
pdf.bounding_box([x,y], :width => width, :height => height) do
|
32
|
+
pdf.svg data, :vposition => :bottom, :position => :center
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def Generator.insert_qr_svg(pdf, string, x, y, width, height)
|
37
|
+
barcode = Barby::QrCode.new(string, size: 4)
|
38
|
+
outputter = Barby::SvgOutputter.new(barcode)
|
39
|
+
outputter.height = height
|
40
|
+
outputter.margin = 0
|
41
|
+
blob = outputter.to_svg #Raw PNG data
|
42
|
+
# File.open('barcode.svg', 'wb'){|f| f.write blob }
|
43
|
+
data = StringIO.new(blob)
|
44
|
+
pdf.bounding_box([x,y], :width => width, :height => height) do
|
45
|
+
pdf.svg data, :vposition => :bottom, :position => :center, :bottom_margin => 0.4.cm
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def Generator.render(template, data, output_filename)
|
51
|
+
validation = {
|
52
|
+
font: String,
|
53
|
+
size: Array,
|
54
|
+
boxes: Array
|
55
|
+
}
|
56
|
+
box_validation = {
|
57
|
+
position: Array,
|
58
|
+
size: Array,
|
59
|
+
align: String,
|
60
|
+
font_size: Integer
|
61
|
+
}
|
62
|
+
# Do validation on template general shape
|
63
|
+
template_shape_validation = template.valid_template?validation
|
64
|
+
if template_shape_validation[:valid] then
|
65
|
+
#If it is valid, the try to validate each defined box in the template
|
66
|
+
template[:boxes].each_with_index{ |box,index|
|
67
|
+
box_shape_validation = box.valid_template?box_validation
|
68
|
+
if not box_shape_validation[:valid] then
|
69
|
+
raise ArgumentError.new("#{box_shape_validation[:reason]} on box number #{index}")
|
70
|
+
end
|
71
|
+
}
|
72
|
+
else
|
73
|
+
raise raise ArgumentError.new(template_shape_validation[:reason])
|
74
|
+
end
|
75
|
+
# Check if the amount of data given fits the template
|
76
|
+
if(template[:boxes].count != data.count) then
|
77
|
+
raise ArgumentError.new("Data input should have the same lenght as available boxes. "\
|
78
|
+
"Expecting #{template[:boxes].count} but #{data.count} given")
|
79
|
+
end
|
80
|
+
|
81
|
+
# From here template seems to be valid
|
82
|
+
pdf = Prawn::Document.new(:page_size => template[:size], :margin => 0)
|
83
|
+
pdf.font template[:font]
|
84
|
+
template[:boxes].each_with_index do |box,index|
|
85
|
+
value = data[index]
|
86
|
+
if value.start_with?('text:', 'qr:', 'code128:', 'base64:') then
|
87
|
+
if value.start_with?('text:') then
|
88
|
+
pdf.text_box(
|
89
|
+
value.sub('text:',''),
|
90
|
+
:at => box[:position],
|
91
|
+
:size => box[:font_size],
|
92
|
+
:width => box[:size][0],
|
93
|
+
:height=> box[:size][1],
|
94
|
+
:overflow => :shrink_to_fit,
|
95
|
+
:disable_wrap_by_char => true,
|
96
|
+
:align => box[:align].to_sym
|
97
|
+
)
|
98
|
+
elsif value.start_with?('code128:') then
|
99
|
+
insert_code_svg(
|
100
|
+
pdf,
|
101
|
+
(value.sub('code128:','')),
|
102
|
+
box[:position][0],
|
103
|
+
box[:position][1],
|
104
|
+
box[:size][0],
|
105
|
+
box[:size][1]
|
106
|
+
)
|
107
|
+
elsif value.start_with?('qr:') then
|
108
|
+
insert_qr_svg(
|
109
|
+
pdf,
|
110
|
+
value.sub('qr:',''),
|
111
|
+
box[:position][0],
|
112
|
+
box[:position][1],
|
113
|
+
box[:size][0],
|
114
|
+
box[:size][1]
|
115
|
+
)
|
116
|
+
end
|
117
|
+
else
|
118
|
+
pdf.text_box(
|
119
|
+
value,
|
120
|
+
:at => box[:position],
|
121
|
+
:size => box[:font_size],
|
122
|
+
:width => box[:size][0],
|
123
|
+
:height=> box[:size][1],
|
124
|
+
:overflow => :shrink_to_fit,
|
125
|
+
:disable_wrap_by_char => true,
|
126
|
+
:align => box[:align].to_sym
|
127
|
+
)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
pdf.render_file output_filename
|
131
|
+
end
|
132
|
+
|
133
|
+
def Generator.render_bounds(template, output_filename)
|
134
|
+
validation = {
|
135
|
+
font: String,
|
136
|
+
size: Array,
|
137
|
+
boxes: Array
|
138
|
+
}
|
139
|
+
box_validation = {
|
140
|
+
position: Array,
|
141
|
+
size: Array,
|
142
|
+
align: String,
|
143
|
+
font_size: Integer
|
144
|
+
}
|
145
|
+
# Do validation on template general shape
|
146
|
+
template_shape_validation = template.valid_template?validation
|
147
|
+
if template_shape_validation[:valid] then
|
148
|
+
#If it is valid, the try to validate each defined box in the template
|
149
|
+
template[:boxes].each_with_index{ |box,index|
|
150
|
+
box_shape_validation = box.valid_template?box_validation
|
151
|
+
if not box_shape_validation[:valid] then
|
152
|
+
raise ArgumentError.new("#{box_shape_validation[:reason]} on box number #{index}")
|
153
|
+
end
|
154
|
+
}
|
155
|
+
else
|
156
|
+
raise raise ArgumentError.new(template_shape_validation[:reason])
|
157
|
+
end
|
158
|
+
|
159
|
+
# From here template seems to be valid
|
160
|
+
pdf = Prawn::Document.new(:page_size => template[:size], :margin => 0)
|
161
|
+
pdf.font template[:font]
|
162
|
+
pdf.stroke_bounds
|
163
|
+
template[:boxes].each do |box|
|
164
|
+
pdf.bounding_box(box[:position], :width => box[:size][0], :height => box[:size][1]) do
|
165
|
+
pdf.stroke_bounds
|
166
|
+
end
|
167
|
+
end
|
168
|
+
pdf.render_file output_filename
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Generator_pdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cristian Canales
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Permite la generacion de pdf a traves de un json formateado
|
14
|
+
email: ccanales@handband.cl
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/generator_pdf.rb
|
20
|
+
homepage: http://rubygems.org/gems/generator_pdf
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.14.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: A lo xoro oe!
|
44
|
+
test_files: []
|