prawml 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +71 -0
- data/Rakefile +2 -0
- data/lib/prawml/version.rb +3 -0
- data/lib/prawml.rb +144 -0
- data/prawml.gemspec +22 -0
- metadata +104 -0
data/.gitignore
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
tmp
|
2
|
+
spec/reports
|
3
|
+
.bundle
|
4
|
+
*.gem
|
5
|
+
InstalledFiles
|
6
|
+
*.rbc
|
7
|
+
.config
|
8
|
+
rdoc
|
9
|
+
_yardoc
|
10
|
+
test/version_tmp
|
11
|
+
lib/bundler/man
|
12
|
+
prawml.sublime-project
|
13
|
+
doc/
|
14
|
+
coverage
|
15
|
+
.yardoc
|
16
|
+
prawml.sublime-workspace
|
17
|
+
Gemfile.lock
|
18
|
+
test/tmp
|
19
|
+
pkg
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Wanderson Policarpo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Prawml
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'prawml'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install prawml
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Describe in an yaml file the fields and its xpos, ypos and style/type options:
|
22
|
+
|
23
|
+
```yaml
|
24
|
+
# example.yml
|
25
|
+
text1:
|
26
|
+
- [250, 680, {style: bold, align: center, size: 96, font: 'Helvetica'}]
|
27
|
+
- [220, 0]
|
28
|
+
text2: [415, 660, {style: italic, size: 16,align: right, color: '123456'}]
|
29
|
+
barcode1: [100, 15, {type: 'barcode', symbology: 'code_25_interleaved'}]
|
30
|
+
```
|
31
|
+
PDF generation example:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
pdf = Prawml::PDF.new('path_to_yaml/example.yml')
|
35
|
+
pdf.generate({
|
36
|
+
:text1 => 'Prawml',
|
37
|
+
:text2 => 'The pdf generator',
|
38
|
+
:barcode1 => 'My little barcode string'
|
39
|
+
}).render_file('example.pdf')
|
40
|
+
```
|
41
|
+
You can also pass an object that responds to methods described in YAML file.
|
42
|
+
|
43
|
+
### Available options:
|
44
|
+
|
45
|
+
* `type`: **text|image|barcode** *[text]*
|
46
|
+
* `color`: **RGB color** *[00000]*
|
47
|
+
* `fixed`: **A fixed text. Ignores values and renders the static text informed** *[false]*
|
48
|
+
|
49
|
+
##### Text specific options:
|
50
|
+
* `font`: **Any Prawn supported font can be used** *[Times-Roman]*
|
51
|
+
* `align`: **left|center|right** *[left]*
|
52
|
+
* `size`: **float** *[12]*
|
53
|
+
* `style`: **bold|normal|italic|bold_italic** *[normal]*
|
54
|
+
* `format`: **Formating hooks** (currency|date) *[false]*
|
55
|
+
|
56
|
+
##### Barcode specific options:
|
57
|
+
* `symbology`: **Symbologies available in barby gem [seen here](https://github.com/toretore/barby/wiki/Symbologies)** *[nil]*
|
58
|
+
|
59
|
+
## Contributors
|
60
|
+
|
61
|
+
1. Wanderson Policarpo (http://github.com/wpolicarpo)
|
62
|
+
2. Edson Júnior (http://github.com/ebfjunior)
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
1. Fork it
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create new Pull Request
|
71
|
+
|
data/Rakefile
ADDED
data/lib/prawml.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require "prawml/version"
|
2
|
+
require "prawn"
|
3
|
+
require "barby/outputter/prawn_outputter"
|
4
|
+
require "active_support/inflector"
|
5
|
+
require "active_support/core_ext"
|
6
|
+
require "yaml"
|
7
|
+
|
8
|
+
module Prawml
|
9
|
+
|
10
|
+
class PDF
|
11
|
+
|
12
|
+
attr_reader :pdf
|
13
|
+
|
14
|
+
SYMBOLOGIES = {
|
15
|
+
"bookland" => "Bookland",
|
16
|
+
"code_128" => "Code128",
|
17
|
+
"code_25" => "Code25",
|
18
|
+
"code_25_iata" => "Code25IATA",
|
19
|
+
"code_25_interleaved" => "Code25Interleaved",
|
20
|
+
"code_39" => "Code39",
|
21
|
+
"code_93" => "Code93",
|
22
|
+
"data_matrix" => "DataMatrix",
|
23
|
+
"ean_13" => "EAN13",
|
24
|
+
"ean_8" => "EAN8",
|
25
|
+
"gs1_128" => "GS1128",
|
26
|
+
"pdf_417" => "Pdf417",
|
27
|
+
"qr_code" => "QrCode",
|
28
|
+
"upc_supplemental" => "UPCSupplemental"
|
29
|
+
}
|
30
|
+
|
31
|
+
def initialize(yaml, options = {})
|
32
|
+
raise "You must pass a valid YAML file or a string with YAML to generate PDF." if yaml.empty?
|
33
|
+
|
34
|
+
begin
|
35
|
+
rules = File.open(yaml)
|
36
|
+
rescue
|
37
|
+
rules = yaml
|
38
|
+
end
|
39
|
+
@rules = YAML::load(rules)
|
40
|
+
|
41
|
+
@options = {
|
42
|
+
:page_size => "A4",
|
43
|
+
:page_layout => :portrait
|
44
|
+
}.merge options
|
45
|
+
|
46
|
+
template = @options[:template]
|
47
|
+
@options.delete(:template)
|
48
|
+
|
49
|
+
@pdf = Prawn::Document.new @options
|
50
|
+
|
51
|
+
unless template.nil?
|
52
|
+
draw_image template[:path], [template[:x], template[:y], {:width => template[:width], :height => template[:height]}]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate(collection)
|
57
|
+
defaults = {
|
58
|
+
:style => :normal,
|
59
|
+
:size => 12,
|
60
|
+
:align => :left,
|
61
|
+
:format => false,
|
62
|
+
:font => 'Times-Roman',
|
63
|
+
:type => :text,
|
64
|
+
:color => '000000',
|
65
|
+
:fixed => false
|
66
|
+
}
|
67
|
+
|
68
|
+
@rules.each do |field, draws|
|
69
|
+
unless draws[0].is_a? Array
|
70
|
+
draws = [draws]
|
71
|
+
end
|
72
|
+
|
73
|
+
draws.each do |params|
|
74
|
+
params[2] = defaults.merge(params[2] || {})
|
75
|
+
params[2].symbolize_keys!
|
76
|
+
params[2][:style] = params[2][:style].to_sym
|
77
|
+
|
78
|
+
set_options params[2]
|
79
|
+
|
80
|
+
value = collection.respond_to?(field.to_sym) ? collection.send(field.to_sym) : collection[field.to_sym]
|
81
|
+
|
82
|
+
send :"draw_#{params[2][:type]}", value, params unless value.nil?
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
@pdf
|
87
|
+
end
|
88
|
+
|
89
|
+
protected
|
90
|
+
|
91
|
+
def draw_text(text, params)
|
92
|
+
xpos, ypos, options = params
|
93
|
+
|
94
|
+
@pdf.draw_text text, :at => [align(text, xpos, options[:align]), ypos]
|
95
|
+
end
|
96
|
+
|
97
|
+
def draw_barcode(text, params)
|
98
|
+
xpos, ypos, options = params
|
99
|
+
|
100
|
+
begin
|
101
|
+
symbology = options[:symbology].to_s
|
102
|
+
|
103
|
+
require "barby/barcode/#{symbology}"
|
104
|
+
|
105
|
+
barby_module = "Barby::#{SYMBOLOGIES[symbology]}"
|
106
|
+
|
107
|
+
barcode = ActiveSupport::Inflector.constantize(barby_module).new(text)
|
108
|
+
|
109
|
+
outputter = Barby::PrawnOutputter.new(barcode)
|
110
|
+
outputter.annotate_pdf(@pdf, options.merge({:x => xpos, :y => ypos}))
|
111
|
+
rescue LoadError
|
112
|
+
raise "Symbology '#{symbology}' is not defined. Please see https://github.com/toretore/barby/wiki/Symbologies for more information on available symbologies."
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def draw_image(image, params)
|
117
|
+
xpos, ypos, options = params
|
118
|
+
|
119
|
+
@pdf.image image, :at => [xpos, ypos], :width => options[:width], :height => options[:height]
|
120
|
+
end
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
def set_options(options)
|
125
|
+
@pdf.fill_color options[:color]
|
126
|
+
@pdf.font options[:font], options
|
127
|
+
end
|
128
|
+
|
129
|
+
def align(text, position, alignment)
|
130
|
+
font = @pdf.font
|
131
|
+
width = font.compute_width_of(text.to_s.parameterize)
|
132
|
+
|
133
|
+
case alignment.to_sym
|
134
|
+
when :center then
|
135
|
+
position - width/2
|
136
|
+
when :right then
|
137
|
+
position - width
|
138
|
+
else
|
139
|
+
position
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
data/prawml.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/prawml/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Wanderson Policarpo", "Edson Júnior"]
|
6
|
+
gem.email = ["wpolicarpo@gmail.com", "ejunior.batista@gmail.com"]
|
7
|
+
gem.description = "Prawml is a Yaml DSL interpreted by ruby over Prawn gem to generate PDF files easily"
|
8
|
+
gem.summary = "Yaml intends to help you generate PDF files writing only an yaml file."
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "prawml"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Prawml::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "prawn"
|
19
|
+
gem.add_dependency "barby"
|
20
|
+
gem.add_dependency "activesupport", "~> 3.0"
|
21
|
+
end
|
22
|
+
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prawml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Wanderson Policarpo
|
9
|
+
- Edson Júnior
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-12-20 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: prawn
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: barby
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
type: :runtime
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: activesupport
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '3.0'
|
63
|
+
description: Prawml is a Yaml DSL interpreted by ruby over Prawn gem to generate PDF
|
64
|
+
files easily
|
65
|
+
email:
|
66
|
+
- wpolicarpo@gmail.com
|
67
|
+
- ejunior.batista@gmail.com
|
68
|
+
executables: []
|
69
|
+
extensions: []
|
70
|
+
extra_rdoc_files: []
|
71
|
+
files:
|
72
|
+
- .gitignore
|
73
|
+
- Gemfile
|
74
|
+
- LICENSE
|
75
|
+
- README.md
|
76
|
+
- Rakefile
|
77
|
+
- lib/prawml.rb
|
78
|
+
- lib/prawml/version.rb
|
79
|
+
- prawml.gemspec
|
80
|
+
homepage: ''
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.8.23
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Yaml intends to help you generate PDF files writing only an yaml file.
|
104
|
+
test_files: []
|