badger 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +11 -0
- data/README.txt +108 -0
- data/Rakefile +18 -0
- data/bin/badger +73 -0
- data/data/fields.rfdf +21 -0
- data/lib/badger.rb +8 -0
- data/lib/badger/driver.rb +89 -0
- data/lib/badger/fields.rb +52 -0
- data/lib/badger/template.rb +30 -0
- data/test/test_badger.rb +0 -0
- metadata +63 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
badger
|
2
|
+
http://codefluency.rubyforge.org/badger
|
3
|
+
by Bruce Williams (http://codefluency.com)
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Badger is a library used to generate badge sheets for events, given a PDF template
|
8
|
+
and data for the badges.
|
9
|
+
|
10
|
+
== FEATURES/PROBLEMS:
|
11
|
+
|
12
|
+
It's free, it's quick, and it's easy. That being said, it requires a few things:
|
13
|
+
|
14
|
+
* That you have the [free] PDF Toolkit (http://accesspdf.com/pdftk) installed
|
15
|
+
* That you have a way to create the PDF template (using the full version of Adobe Acrobat, for instance)
|
16
|
+
|
17
|
+
== SYNOPSYS:
|
18
|
+
|
19
|
+
The process of creating badges is easy.
|
20
|
+
|
21
|
+
* Create a one-page PDF template with the badge design
|
22
|
+
* Create a file containing information on the people attending the event
|
23
|
+
* Run badger
|
24
|
+
|
25
|
+
Here's the details:
|
26
|
+
|
27
|
+
=== The PDF Template
|
28
|
+
|
29
|
+
Create a one-page PDF with the badge design you'd like to use. Fit as many
|
30
|
+
badges in the page as you'd like.
|
31
|
+
|
32
|
+
For each badge, make fields as in the following example:
|
33
|
+
|
34
|
+
For the first badge, fields "first_name1", "last_name1", "company1", "role1"
|
35
|
+
.. badges 2 - 5 ...
|
36
|
+
For the sixth badge, fields "first_name6", "last_name6", "company6", "role6"
|
37
|
+
|
38
|
+
You can use Adobe Acrobat to create the fields.
|
39
|
+
|
40
|
+
* Fields should be named consecutively from 1 to the number of the last badge on the page. If the last
|
41
|
+
badge has a field like "last_name8" it's assumed there are fields "last_name1" through "last_name7" somewhere
|
42
|
+
on the page.
|
43
|
+
* What order the badges are in is completely up to you; left-to-right, top-to-bottom, etc
|
44
|
+
* With the exception of "first_name," use whatever field [base]names you like. If you use "role" fields, however,
|
45
|
+
keep in mind that, by default, it will be filled to "Attendee" if there if no data provided for the badge (you
|
46
|
+
can override the default, as well, see "badger -h")
|
47
|
+
|
48
|
+
=== The Data Source
|
49
|
+
|
50
|
+
Create a YAML document. Inside the document should be an array of hashes, as in the following example:
|
51
|
+
|
52
|
+
---
|
53
|
+
- first_name: Foo
|
54
|
+
last_name: Bar
|
55
|
+
company: Foo Bar, Inc.
|
56
|
+
role: Speaker
|
57
|
+
- first_name: Jane
|
58
|
+
last_name: Jungle
|
59
|
+
- first_name: Yukihiro
|
60
|
+
last_name: Matsumoto
|
61
|
+
role: matz
|
62
|
+
|
63
|
+
You don't need to break them up by pages, or have them in any special order.
|
64
|
+
|
65
|
+
=== Building the badges
|
66
|
+
|
67
|
+
Assuming you have pdftk (in your PATH) and badger installed:
|
68
|
+
|
69
|
+
badger -t your_template.pdf your_data.yml
|
70
|
+
|
71
|
+
See badger -h for more information about the possible options.
|
72
|
+
|
73
|
+
== REQUIREMENTS:
|
74
|
+
|
75
|
+
PDF Toolkit, freely available from http://accesspdf.com/pdftk
|
76
|
+
* The pdftk executable must be in your PATH
|
77
|
+
|
78
|
+
Ruby requirements: activesupport
|
79
|
+
|
80
|
+
== INSTALL:
|
81
|
+
|
82
|
+
Install PDF Toolkit (http://accesspdf.com/pdftk)
|
83
|
+
sudo gem install badger
|
84
|
+
|
85
|
+
== LICENSE:
|
86
|
+
|
87
|
+
(The MIT License)
|
88
|
+
|
89
|
+
Copyright (c) 2006 Bruce Williams
|
90
|
+
|
91
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
92
|
+
a copy of this software and associated documentation files (the
|
93
|
+
'Software'), to deal in the Software without restriction, including
|
94
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
95
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
96
|
+
permit persons to whom the Software is furnished to do so, subject to
|
97
|
+
the following conditions:
|
98
|
+
|
99
|
+
The above copyright notice and this permission notice shall be
|
100
|
+
included in all copies or substantial portions of the Software.
|
101
|
+
|
102
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
103
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
104
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
105
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
106
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
107
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
108
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/badger.rb'
|
6
|
+
|
7
|
+
Hoe.new('badger', Badger::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'badger'
|
9
|
+
p.summary = "Badger is a library used to generate badge sheets for events, given a PDF template and data for the badges."
|
10
|
+
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
11
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
12
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
13
|
+
p.extra_deps = [['activesupport', '>= 1.3.1']]
|
14
|
+
p.email = %q{bruce@codefluency.com}
|
15
|
+
p.author = "Bruce Williams"
|
16
|
+
end
|
17
|
+
|
18
|
+
# vim: syntax=Ruby
|
data/bin/badger
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# ===================
|
2
|
+
# = Check for pdftk =
|
3
|
+
# ===================
|
4
|
+
|
5
|
+
require 'optparse'
|
6
|
+
require 'ostruct'
|
7
|
+
require 'badger'
|
8
|
+
|
9
|
+
# =================
|
10
|
+
# = Parse options =
|
11
|
+
# =================
|
12
|
+
|
13
|
+
options = OpenStruct.new
|
14
|
+
|
15
|
+
opts = OptionParser.new do |opt|
|
16
|
+
|
17
|
+
opt.banner = <<-EOB
|
18
|
+
|
19
|
+
USAGE: badger -t TEMPLATE_FILE DATA_FILE [OPTIONS]"
|
20
|
+
|
21
|
+
For information on how to build the template and data files, see
|
22
|
+
the documentation for the 'badger' RubyGem or read the local copy of the README
|
23
|
+
located at:
|
24
|
+
#{File.expand_path(File.join(File.dirname(__FILE__), '../README.txt'))}
|
25
|
+
|
26
|
+
OPTIONS
|
27
|
+
|
28
|
+
EOB
|
29
|
+
|
30
|
+
opt.on('-r DEFAULT_ROLE', '--default-role DEFAULT_ROLE', "Modify the default role (or uses '#{Badger::Fields::DEFAULT_ROLE}')") do |r|
|
31
|
+
options.default_role = r
|
32
|
+
end
|
33
|
+
|
34
|
+
opt.on('-t TEMPLATE_FILE', '--template TEMPLATE_FILE', "REQUIRED: Template file (pdf with fields)") do |f|
|
35
|
+
if File.file?(f)
|
36
|
+
options.template_file = f
|
37
|
+
else
|
38
|
+
$stderr.puts "Invalid template file: #{f}"
|
39
|
+
exit
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
opt.on('-o OUTPUT_FILE', '--output-file OUTPUT_FILE', "OPTIONAL: Set output filename (pdf)") do |o|
|
44
|
+
options.output_file = o
|
45
|
+
end
|
46
|
+
|
47
|
+
opt.on_tail('-h', '--help', "Show this message") do |section|
|
48
|
+
if section == 'template'
|
49
|
+
|
50
|
+
else
|
51
|
+
puts opt
|
52
|
+
end
|
53
|
+
exit
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
opts.parse!
|
58
|
+
|
59
|
+
# ===================
|
60
|
+
# = Go! (ok, maybe) =
|
61
|
+
# ===================
|
62
|
+
|
63
|
+
if ARGV.empty? || !File.exists?(ARGV[0])
|
64
|
+
$stderr.puts "ERROR: Missing input file"
|
65
|
+
puts opts
|
66
|
+
elsif !options.template_file
|
67
|
+
$stderr.puts "ERROR: Missing template file"
|
68
|
+
puts opts
|
69
|
+
else
|
70
|
+
output = Badger::Driver.new(ARGV[0], options).run
|
71
|
+
puts "Output #{output}"
|
72
|
+
end
|
73
|
+
|
data/data/fields.rfdf
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
%FDF-1.2
|
2
|
+
1 0 obj
|
3
|
+
<<
|
4
|
+
/FDF
|
5
|
+
<<
|
6
|
+
/Fields [
|
7
|
+
<% people.each_with_index do |person,person_index|
|
8
|
+
next unless person
|
9
|
+
person.each do |field_base, value|
|
10
|
+
%>
|
11
|
+
<< /T (<%= field_base %><%= person_index + 1 %>) /V (<%= escape_value value %>) >>
|
12
|
+
<%
|
13
|
+
end
|
14
|
+
end %>
|
15
|
+
]
|
16
|
+
/F (<%= @template.absolute_path %>) >>
|
17
|
+
>>
|
18
|
+
endobj
|
19
|
+
trailer
|
20
|
+
<< /Root 1 0 R >>
|
21
|
+
%%EOF
|
data/lib/badger.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
module Badger
|
2
|
+
|
3
|
+
DEFAULT_OUTPUT_FILE = 'badges.pdf'
|
4
|
+
BUILD_BASE = 'build'
|
5
|
+
|
6
|
+
::Time::DATE_FORMATS[:build_number] = '%Y%m%d%H%M%S'
|
7
|
+
|
8
|
+
class Driver
|
9
|
+
|
10
|
+
class BuildError < ::StandardError; end
|
11
|
+
|
12
|
+
def initialize(input_file, options)
|
13
|
+
@options = options
|
14
|
+
@template = Template.new(options.template_file)
|
15
|
+
@fields = Fields.new(input_file, @template, options.default_role)
|
16
|
+
@number_of_pages = @fields.size / @template.badges_per_page
|
17
|
+
@number_of_pages += 1 if @fields.size % @template.badges_per_page > 0
|
18
|
+
@build_number = Time.now.to_s(:build_number)
|
19
|
+
end
|
20
|
+
|
21
|
+
def run
|
22
|
+
setup
|
23
|
+
archive_template_and_data
|
24
|
+
$stderr.puts "Building in #{build_dir}"
|
25
|
+
write_fdfs
|
26
|
+
build_pages
|
27
|
+
append_pages
|
28
|
+
end
|
29
|
+
|
30
|
+
#######
|
31
|
+
private
|
32
|
+
#######
|
33
|
+
|
34
|
+
def archive_template_and_data
|
35
|
+
FileUtils.cp @template.filename, build_path('src/template.yml')
|
36
|
+
FileUtils.cp @fields.filename, build_path('src/fields.yml')
|
37
|
+
end
|
38
|
+
|
39
|
+
def write_fdfs
|
40
|
+
@fields.in_pages_of(@template.badges_per_page) do |fdf, page_number|
|
41
|
+
File.open(build_path("src/page#{page_number}.fdf"), 'w'){|f| f.puts(fdf) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_pages
|
46
|
+
1.upto(@number_of_pages){|n| build_page(n) }
|
47
|
+
end
|
48
|
+
|
49
|
+
def build_page(number)
|
50
|
+
pdf = build_path("src/page#{number}.pdf")
|
51
|
+
args = ["pdftk", @template.filename,
|
52
|
+
'fill_form', build_path("src/page#{number}.fdf"),
|
53
|
+
"output", pdf,
|
54
|
+
"flatten"]
|
55
|
+
unless system(*args) && File.file?(pdf)
|
56
|
+
raise BuildError, "Could not build Page ##{number} (bad response from pdftk)"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def append_pages
|
61
|
+
pdf = @options.output_file || build_path(DEFAULT_OUTPUT_FILE)
|
62
|
+
if @number_of_pages > 1
|
63
|
+
$stderr.puts "Appending pages..."
|
64
|
+
args = ["pdftk", Dir[build_path('src/page*.pdf')] , "cat", "output", pdf].flatten
|
65
|
+
unless system(*args) && File.file?(pdf)
|
66
|
+
raise BuildError, "Could not append pages to #{pdf} (bad response from pdftk)"
|
67
|
+
end
|
68
|
+
else
|
69
|
+
$stderr.puts "Only one page, skipping append..."
|
70
|
+
FileUtils.cp build_path("src/page1.pdf"), pdf
|
71
|
+
end
|
72
|
+
pdf
|
73
|
+
end
|
74
|
+
|
75
|
+
def setup
|
76
|
+
FileUtils.mkdir_p build_path('src')
|
77
|
+
end
|
78
|
+
|
79
|
+
def build_path(basename)
|
80
|
+
File.join(build_dir, basename)
|
81
|
+
end
|
82
|
+
|
83
|
+
def build_dir
|
84
|
+
File.join(BUILD_BASE, @build_number)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Badger
|
2
|
+
|
3
|
+
class Fields
|
4
|
+
|
5
|
+
DEFAULT_ROLE = 'Attendee'
|
6
|
+
|
7
|
+
class FormatError < ::StandardError; end
|
8
|
+
|
9
|
+
delegate :size, :to => :@data
|
10
|
+
|
11
|
+
attr_reader :filename
|
12
|
+
def initialize(filename, template, default_role=nil)
|
13
|
+
@filename = filename
|
14
|
+
@data = YAML.load(File.read(filename))
|
15
|
+
@template = template
|
16
|
+
@default_role = default_role || DEFAULT_ROLE
|
17
|
+
check_fields
|
18
|
+
end
|
19
|
+
|
20
|
+
def in_pages_of(number)
|
21
|
+
fdf_template = ERB.new(File.read(File.join(File.dirname(__FILE__), '../../data/fields.rfdf')), nil, '>')
|
22
|
+
page_number = 0
|
23
|
+
@data.in_groups_of(number) do |p|
|
24
|
+
people = p
|
25
|
+
page_number += 1
|
26
|
+
yield(fdf_template.result(binding), page_number)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
#######
|
31
|
+
private
|
32
|
+
#######
|
33
|
+
|
34
|
+
def check_fields
|
35
|
+
unless @data && @data.kind_of?(Array) && @data.all?{|item| item.kind_of?(Hash) }
|
36
|
+
raise FormatError, "Should be array of hashes"
|
37
|
+
end
|
38
|
+
@data.each do |person|
|
39
|
+
person['role'] ||= @default_role
|
40
|
+
person['role'].capitalize!
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def escape_value(value)
|
45
|
+
value.to_s.gsub(/[\(\)]/, '')
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Badger
|
2
|
+
|
3
|
+
class Template
|
4
|
+
|
5
|
+
class FormatError < ::ArgumentError; end
|
6
|
+
|
7
|
+
attr_reader :filename
|
8
|
+
def initialize(filename)
|
9
|
+
@filename = filename
|
10
|
+
end
|
11
|
+
|
12
|
+
def absolute_path
|
13
|
+
File.expand_path(@filename)
|
14
|
+
end
|
15
|
+
|
16
|
+
def badges_per_page
|
17
|
+
@badges_per_page ||= begin
|
18
|
+
data_fields = `pdftk '#{@filename}' dump_data_fields`
|
19
|
+
per_page = data_fields.scan(/first_name(\d+)/).flatten.map(&:to_i).max
|
20
|
+
if per_page.zero?
|
21
|
+
raise FormatError, "No badges found in template #{@filename}; see badger -h"
|
22
|
+
else
|
23
|
+
per_page
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/test/test_badger.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: badger
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2006-12-07 00:00:00 -07:00
|
8
|
+
summary: Badger is a library used to generate badge sheets for events, given a PDF template and data for the badges.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: bruce@codefluency.com
|
12
|
+
homepage: " http://codefluency.rubyforge.org/badger"
|
13
|
+
rubyforge_project: badger
|
14
|
+
description: "== FEATURES/PROBLEMS: It's free, it's quick, and it's easy. That being said, it requires a few things: * That you have the [free] PDF Toolkit (http://accesspdf.com/pdftk) installed * That you have a way to create the PDF template (using the full version of Adobe Acrobat, for instance) == SYNOPSYS: The process of creating badges is easy."
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Bruce Williams
|
30
|
+
files:
|
31
|
+
- History.txt
|
32
|
+
- Manifest.txt
|
33
|
+
- README.txt
|
34
|
+
- Rakefile
|
35
|
+
- bin/badger
|
36
|
+
- lib/badger.rb
|
37
|
+
- lib/badger/driver.rb
|
38
|
+
- lib/badger/fields.rb
|
39
|
+
- lib/badger/template.rb
|
40
|
+
- data/fields.rfdf
|
41
|
+
- test/test_badger.rb
|
42
|
+
test_files:
|
43
|
+
- test/test_badger.rb
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
extra_rdoc_files: []
|
47
|
+
|
48
|
+
executables:
|
49
|
+
- badger
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
dependencies:
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
version_requirement:
|
58
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.3.1
|
63
|
+
version:
|