prawn-labels 0.11.1
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.
- data/COPYING +340 -0
- data/LICENSE +56 -0
- data/README.md +37 -0
- data/Rakefile +9 -0
- data/examples/example_helper.rb +5 -0
- data/examples/hello_labels.pdf +337 -0
- data/examples/hello_labels.rb +11 -0
- data/examples/shrink_to_fit.pdf +1050 -0
- data/examples/shrink_to_fit.rb +35 -0
- data/lib/prawn/labels.rb +99 -0
- data/lib/prawn/types.yaml +29 -0
- data/prawn-labels.gemspec +25 -0
- metadata +100 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "example_helper")
|
2
|
+
|
3
|
+
one = "1"
|
4
|
+
two = "2\n2"
|
5
|
+
three = (["3"]*3).join("\n")
|
6
|
+
four = (["4"]*4).join("\n")
|
7
|
+
five = (["5"]*5).join("\n")
|
8
|
+
six = (["6"]*6).join("\n")
|
9
|
+
seven = (["7"]*7).join("\n")
|
10
|
+
eight = (["8"]*8).join("\n")
|
11
|
+
nine = (["9"]*9).join("\n")
|
12
|
+
ten = (["10"]*10).join("\n")
|
13
|
+
long = %{this
|
14
|
+
is
|
15
|
+
thelonglined
|
16
|
+
entrywhereijusttypealottomakeitoverflowthebuffer
|
17
|
+
andthislinetriggersbutitdontkeep }
|
18
|
+
long2 = %{this
|
19
|
+
is just
|
20
|
+
the other long line
|
21
|
+
entry where i just type to make it over flow the buffer
|
22
|
+
lol }
|
23
|
+
fifty = (["50"]*50).join("\n")
|
24
|
+
|
25
|
+
test_names = [one, two, three, four, five, six, seven, eight, nine, ten,
|
26
|
+
"Mike", "Kelly", "Bob", "Greg", "Jordan", "Chris", "Jon", "Mike",
|
27
|
+
"Kelly", long, long2, "Jordan", "Chris", "Jon", fifty, "Kelly",
|
28
|
+
"Bob", "Greg", "Jordan", "Chris",
|
29
|
+
#these next five should be on the next page.
|
30
|
+
"Jon", "Mike", "Kelly", "Bob", "Greg"]
|
31
|
+
|
32
|
+
Prawn::Labels.generate("shrink_to_fit.pdf", test_names, :type => "Avery5160", :shrink_to_fit => true) do |pdf, name|
|
33
|
+
pdf.text name
|
34
|
+
end
|
35
|
+
|
data/lib/prawn/labels.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# labels.rb : A simple helper to generate labels for Prawn PDFs
|
2
|
+
#
|
3
|
+
# Copyright February 2010, Jordan Byron. All Rights Reserved.
|
4
|
+
#
|
5
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
6
|
+
|
7
|
+
require 'prawn'
|
8
|
+
require 'yaml'
|
9
|
+
|
10
|
+
module Prawn
|
11
|
+
class Labels
|
12
|
+
attr_reader :document, :type
|
13
|
+
|
14
|
+
def self.generate(file_name, data, options = {}, &block)
|
15
|
+
labels = Labels.new(data, options, &block)
|
16
|
+
labels.document.render_file(file_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.render(data, options = {}, &block)
|
20
|
+
labels = Labels.new(data, options, &block)
|
21
|
+
labels.document.render
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(data, options = {}, &block)
|
25
|
+
types_file = File.join(File.dirname(__FILE__), 'types.yaml')
|
26
|
+
types = YAML.load_file(types_file)
|
27
|
+
|
28
|
+
unless @type = types[options[:type]]
|
29
|
+
raise "Label Type Unknown '#{options[:type]}'"
|
30
|
+
end
|
31
|
+
|
32
|
+
type["paper_size"] ||= "A4"
|
33
|
+
type["top_margin"] ||= 36
|
34
|
+
type["top_margin"] ||= 36
|
35
|
+
|
36
|
+
@document = Document.new :page_size => type["paper_size"],
|
37
|
+
:top_margin => type["top_margin"],
|
38
|
+
:bottom_margin => type["bottom_margin"],
|
39
|
+
:left_margin => type["left_margin"],
|
40
|
+
:right_margin => type["right_margin"]
|
41
|
+
|
42
|
+
generate_grid @type
|
43
|
+
|
44
|
+
data.each_with_index do |record, i|
|
45
|
+
create_label(i, record, options) do |pdf, record|
|
46
|
+
yield pdf, record
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def generate_grid(type)
|
55
|
+
@document.define_grid({ :columns => type["columns"],
|
56
|
+
:rows => type["rows"],
|
57
|
+
:column_gutter => type["column_gutter"],
|
58
|
+
:row_gutter => type["row_gutter"],
|
59
|
+
})
|
60
|
+
end
|
61
|
+
|
62
|
+
def row_col_from_index(i)
|
63
|
+
page, newi = i.divmod(@document.grid.rows * @document.grid.columns)
|
64
|
+
if newi == 0 and page > 0
|
65
|
+
@document.start_new_page
|
66
|
+
generate_grid @type
|
67
|
+
return [0,0]
|
68
|
+
end
|
69
|
+
return newi.divmod(@document.grid.columns)
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_label(i, record, options = {}, &block)
|
73
|
+
p = row_col_from_index(i)
|
74
|
+
|
75
|
+
shrink_text(record) if options[:shrink_to_fit] == true
|
76
|
+
|
77
|
+
b = @document.grid(p.first, p.last)
|
78
|
+
@document.bounding_box b.top_left, :width => b.width, :height => b.height do
|
79
|
+
yield @document, record
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def shrink_text(record)
|
84
|
+
linecount = (split_lines = record.split("\n")).length
|
85
|
+
|
86
|
+
# 30 is estimated max character length per line.
|
87
|
+
split_lines.each {|line| linecount += line.length / 30 }
|
88
|
+
|
89
|
+
# -10 accounts for the overflow margins
|
90
|
+
rowheight = @document.grid.row_height - 10
|
91
|
+
|
92
|
+
if linecount <= rowheight / 12.floor
|
93
|
+
@document.font_size = 12
|
94
|
+
else
|
95
|
+
@document.font_size = rowheight / (linecount + 1)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#
|
2
|
+
# prawns' basic unit is PostScript-Point
|
3
|
+
# 72 points per inch
|
4
|
+
#
|
5
|
+
# Avery 7160 family: Mailing labels, 63.5 x 38.1 mm, 21 per sheet
|
6
|
+
Avery7160:
|
7
|
+
page_size: A4
|
8
|
+
top_margin: 43.9
|
9
|
+
bottom_margin: 0
|
10
|
+
left_margin: 19.843
|
11
|
+
right_margin: 19.843
|
12
|
+
columns: 3
|
13
|
+
rows: 7
|
14
|
+
column_gutter: 8.504
|
15
|
+
row_gutter: 0
|
16
|
+
# Avery 5160 family
|
17
|
+
Avery5160:
|
18
|
+
page_size: A4
|
19
|
+
top_margin: 36
|
20
|
+
bottom_margin: 36
|
21
|
+
left_margin: 15.822
|
22
|
+
right_margin: 15.822
|
23
|
+
columns: 3
|
24
|
+
rows: 10
|
25
|
+
column_gutter: 10
|
26
|
+
row_gutter: 0
|
27
|
+
TEST:
|
28
|
+
left_margin: 123
|
29
|
+
right_margin: 789
|
@@ -0,0 +1,25 @@
|
|
1
|
+
PRAWN_LABELS_VERSION = "0.11.1"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "prawn-labels"
|
5
|
+
spec.version = PRAWN_LABELS_VERSION
|
6
|
+
spec.platform = Gem::Platform::RUBY
|
7
|
+
spec.summary = "Make labels using Prawn"
|
8
|
+
spec.files = Dir.glob("{examples,lib}/**/**/*") +
|
9
|
+
["Rakefile", "prawn-labels.gemspec"]
|
10
|
+
spec.require_path = "lib"
|
11
|
+
spec.required_ruby_version = '>= 1.8.7'
|
12
|
+
spec.required_rubygems_version = ">= 1.3.6"
|
13
|
+
|
14
|
+
spec.extra_rdoc_files = %w{README.md LICENSE COPYING}
|
15
|
+
spec.rdoc_options << '--title' << 'Prawn/Labels Documentation' <<
|
16
|
+
'--main' << 'README.md' << '-q'
|
17
|
+
spec.authors = ["Jordan Byron"]
|
18
|
+
spec.email = ["jordan.byron@gmail.com"]
|
19
|
+
spec.rubyforge_project = "prawn-labels"
|
20
|
+
spec.add_dependency('prawn', '~>0.11.1')
|
21
|
+
spec.homepage = "http://github.com/jordanbyron/prawn-labels"
|
22
|
+
spec.description = <<END_DESC
|
23
|
+
Prawn/Labels takes the guess work out of generating labels using Prawn
|
24
|
+
END_DESC
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prawn-labels
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 11
|
8
|
+
- 1
|
9
|
+
version: 0.11.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jordan Byron
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-07-08 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: prawn
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 11
|
31
|
+
- 1
|
32
|
+
version: 0.11.1
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: " Prawn/Labels takes the guess work out of generating labels using Prawn\n"
|
36
|
+
email:
|
37
|
+
- jordan.byron@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- README.md
|
44
|
+
- LICENSE
|
45
|
+
- COPYING
|
46
|
+
files:
|
47
|
+
- examples/example_helper.rb
|
48
|
+
- examples/hello_labels.pdf
|
49
|
+
- examples/hello_labels.rb
|
50
|
+
- examples/shrink_to_fit.pdf
|
51
|
+
- examples/shrink_to_fit.rb
|
52
|
+
- lib/prawn/labels.rb
|
53
|
+
- lib/prawn/types.yaml
|
54
|
+
- Rakefile
|
55
|
+
- prawn-labels.gemspec
|
56
|
+
- README.md
|
57
|
+
- LICENSE
|
58
|
+
- COPYING
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/jordanbyron/prawn-labels
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --title
|
66
|
+
- Prawn/Labels Documentation
|
67
|
+
- --main
|
68
|
+
- README.md
|
69
|
+
- -q
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 1
|
79
|
+
- 8
|
80
|
+
- 7
|
81
|
+
version: 1.8.7
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 1
|
89
|
+
- 3
|
90
|
+
- 6
|
91
|
+
version: 1.3.6
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project: prawn-labels
|
95
|
+
rubygems_version: 1.3.7
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Make labels using Prawn
|
99
|
+
test_files: []
|
100
|
+
|