prawn-label_sheet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f85fe1ff0bc71aa9563acbaa3b21ecac56dd90eb43f6f18f87b448c3587215d7
4
+ data.tar.gz: 8ee3acde8e20e96186492bc3cabf187c494ce3cba2b78012525fc9a4c981242d
5
+ SHA512:
6
+ metadata.gz: 06c27d6a1a091b2da11596c3f84f2091576f7c97fb38604354855ab0d886af983915e26d56b3e23d9867af802999dcf50abb0513e8946d3536d5fa887364a646
7
+ data.tar.gz: 3cb58232101e7dd8e1ecfb75a615228df167910dc9a5006f7e88aa21e1605fce3772fc697e78885fbb18de1f346badd1015cee27faf4903dea6e73e9fa056f93
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /Gemfile.lock
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # rubocop remote cache
15
+ .rubocop-*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,25 @@
1
+ inherit_gem:
2
+ relaxed-rubocop: .rubocop.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.4
6
+
7
+ Layout/AlignArguments:
8
+ EnforcedStyle: with_fixed_indentation
9
+ Layout/AlignParameters:
10
+ EnforcedStyle: with_fixed_indentation
11
+ Layout/MultilineMethodCallIndentation:
12
+ EnforcedStyle: indented
13
+ Layout/MultilineOperationIndentation:
14
+ EnforcedStyle: indented
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - example/**/*.rb
19
+
20
+ Style/TrivialAccessors:
21
+ Exclude:
22
+ - lib/prawn/label_sheet/configuration.rb
23
+ Style/Documentation:
24
+ Exclude:
25
+ - lib/prawn/label_sheet/configuration.rb
@@ -0,0 +1,15 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4
7
+ - 2.5.3
8
+ - 2.6
9
+ - 2.7
10
+ - ruby-head
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+ fast_finish: true
15
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in prawn-label_sheet.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Tom Crouch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,121 @@
1
+ # Prawn::LabelSheet
2
+
3
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/28933c41f8ed4d39a1c96a5895de2343)](https://app.codacy.com/manual/t.crouch/prawn-label_sheet?utm_source=github.com&utm_medium=referral&utm_content=tcrouch/prawn-label_sheet&utm_campaign=Badge_Grade_Dashboard)
4
+ [![Inline docs](http://inch-ci.org/github/tcrouch/prawn-label_sheet.svg?branch=master)](http://inch-ci.org/github/tcrouch/prawn-label_sheet)
5
+
6
+
7
+ Generate sets of labels or stickers using Prawn PDF.
8
+
9
+ For simple bulk generation, can break page between groups of data.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'prawn-label_sheet'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```shell
22
+ $ bundle
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```shell
28
+ $ gem install prawn-label_sheet
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ```ruby
34
+ items = %w[red orange yellow green blue indigo violet]
35
+
36
+ Prawn::LabelSheet.generate("out.pdf", items) do |pdf, item|
37
+ pdf.text_box item
38
+ end
39
+ ```
40
+
41
+ Each item is passed to the block, together with a bounding box for the label.
42
+
43
+ For larger sets, page breaks can be inserted on change of value:
44
+
45
+ ```ruby
46
+ # Change in value returned from item[3]
47
+ generate("out.pdf", items, break_on: 3)
48
+
49
+ # Change in value returned from proc
50
+ generate("out.pdf", items, break_on: ->(item) { item.downcase })
51
+ ```
52
+
53
+ ### Example
54
+
55
+ In a school you might want a single document with labels for all students,
56
+ where a new sheets is started for each class.
57
+
58
+ Given your class data, pre-sorted by [class, last, first]:
59
+
60
+ ```csv
61
+ Arthur,Aardvark,1234,Class A
62
+ Sam,Smith,1235,Class A
63
+ Bob,Bobbington,1236,Class B
64
+ Leah,Lemon,1237,Class B
65
+ ```
66
+
67
+ ```ruby
68
+ csv = CSV.read("students.csv")
69
+
70
+ Prawn::LabelSheet.generate("out.pdf", csv, break_on: 3) do |pdf, data|
71
+ last_name, first_name, class_name, student_id = data
72
+
73
+ b = pdf.bounds
74
+ full_width = b.width - 12
75
+ half_width = (full_width / 2).floor - 1
76
+
77
+ pdf.font 'Helvetica'
78
+
79
+ pdf.move_down 12
80
+ pdf.indent(6) do
81
+ pdf.text_box "#{last_name} #{first_name}",
82
+ at: [b.left, pdf.cursor],
83
+ width: full_width, height: 12,
84
+ overflow: :truncate
85
+ end
86
+ pdf.move_down 12
87
+ pdf.stroke_horizontal_rule
88
+ pdf.move_down 6
89
+ pdf.font_size 7 do
90
+ pdf.text_box "ID",
91
+ at: [b.left, pdf.cursor],
92
+ width: half_width, height: 8
93
+ pdf.text_box "Class",
94
+ at: [b.right - half_width, pdf.cursor],
95
+ width: half_width, height: 8
96
+ end
97
+ pdf.move_down 9
98
+ pdf.text_box student_id,
99
+ at: [b.left, pdf.cursor],
100
+ width: half_width, height: 10,
101
+ overflow: :shrink_to_fit
102
+ pdf.text_box class_name,
103
+ at: [b.right - half_width, pdf.cursor],
104
+ width: half_width, height: 10,
105
+ overflow: :shrink_to_fit
106
+ end
107
+ ```
108
+
109
+ ## Development
110
+
111
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
112
+
113
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
114
+
115
+ ## Contributing
116
+
117
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tcrouch/prawn-label_sheet.
118
+
119
+ ## License
120
+
121
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'prawn/label_sheet'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env bash
2
+ # frozen_string_literal: true
3
+
4
+ set -euo pipefail
5
+ IFS=$'\n\t'
6
+ set -vx
7
+
8
+ bundle install
9
+
10
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'prawn'
4
+ require 'polyfill'
5
+ require 'prawn/label_sheet/version'
6
+ require 'prawn/label_sheet/configuration'
7
+
8
+ # @see http://prawnpdf.org
9
+ module Prawn
10
+ # Document supporting bulk label/sticker generation
11
+ class LabelSheet
12
+ include Prawn::View
13
+
14
+ # Error class
15
+ class Error < StandardError; end
16
+
17
+ # Override Prawn::View#document
18
+
19
+ attr_reader :document, :layout
20
+
21
+ using Polyfill(Hash: %w[#transform_keys])
22
+
23
+ # Render and persist a set of label sheets
24
+ #
25
+ # @param labels [Enumerable, CSV]
26
+ # @param options (see #initialize)
27
+ def self.generate(filename, labels, **options, &block)
28
+ pdf = new(labels, options, &block)
29
+ pdf.document.render_file(filename)
30
+ end
31
+
32
+ # @param labels [Enumerable] collection of labels
33
+ # @option options [String] :layout
34
+ # @option options [Proc, Integer] :break_on
35
+ # @option options [Prawn::Document] :document
36
+ def initialize(labels, **options)
37
+ @layout = setup_layout(options[:layout])
38
+
39
+ @document = resolve_document(options[:document])
40
+ @document.define_grid @layout
41
+ # @document.on_page_create { self.instance_variable_set :@count, 0 }
42
+
43
+ @count = 0
44
+ @break_on = options[:break_on]
45
+
46
+ labels.each do |label|
47
+ make_label(label, options) { |pdf, item| yield pdf, item }
48
+ end
49
+ end
50
+
51
+ # Generate individual label
52
+ #
53
+ # @param item [#[], Object]
54
+ # @yieldparam doc [Prawn::Document] document
55
+ # @yieldparam item [Object] label item
56
+ # @return [Integer] tally of labels on current page
57
+ def make_label(item, _options)
58
+ break_page if break_page?(item)
59
+
60
+ @document.grid(*gridref).bounding_box do
61
+ yield @document, item
62
+ end
63
+ @count += 1
64
+ end
65
+
66
+ protected
67
+
68
+ # Determine whether to begin a new page
69
+ #
70
+ # @param item [Proc, #[]]
71
+ # @return [Boolean]
72
+ def break_page?(item)
73
+ return unless @break_on
74
+
75
+ val = @break_on.is_a?(Proc) ? @break_on.call(item) : item[@break_on]
76
+ return false if @last_val == val
77
+
78
+ @last_val = val
79
+ @count.positive?
80
+ end
81
+
82
+ # Begin a new page
83
+ def break_page
84
+ @document.start_new_page
85
+ @count = 0
86
+ end
87
+
88
+ # Current grid coordinates
89
+ #
90
+ # @return [Array(Integer, Integer)]
91
+ def gridref
92
+ q, r = @count.divmod(@document.grid.rows * @document.grid.columns)
93
+
94
+ if q.positive? && r.zero?
95
+ @document.start_new_page
96
+ return [0, 0]
97
+ end
98
+
99
+ r.divmod(@document.grid.columns)
100
+ end
101
+
102
+ # @param layout_def [String, #to_h] layout definition or identifier
103
+ # @return [Hash]
104
+ # rubocop:disable Style/RescueModifier
105
+ def setup_layout(layout_def)
106
+ defs = resolve_layout(layout_def).slice(
107
+ 'page_size', 'columns', 'rows',
108
+ 'top_margin', 'bottom_margin',
109
+ 'left_margin', 'right_margin',
110
+ 'column_gutter', 'row_gutter'
111
+ )
112
+ {
113
+ page_size: 'A4', top_margin: 40, left_margin: 20
114
+ }.merge!(defs.transform_keys { |key| key.to_sym rescue key })
115
+ end
116
+ # rubocop:enable Style/RescueModifier
117
+
118
+ # Lookup layout definition
119
+ #
120
+ # @param layout_def [String, #to_h] layout definition or identifier
121
+ # @return [Hash]
122
+ def resolve_layout(layout_def)
123
+ dfn = layout_def || config.default_layout
124
+ return dfn.to_h if dfn.respond_to?(:to_h)
125
+
126
+ config.layouts[dfn] || raise(Error, 'Unknown layout')
127
+ end
128
+
129
+ # @param doc [Prawn::Document, nil]
130
+ # @return [Prawn::Document]
131
+ def resolve_document(doc)
132
+ return doc.start_new_page(@layout) if doc
133
+
134
+ Document.new @layout
135
+ end
136
+
137
+ # @return [Prawn::LabelSheet::Configuration]
138
+ def config
139
+ self.class.config
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module Prawn
6
+ class LabelSheet
7
+ # config options
8
+ class Configuration
9
+ attr_accessor :default_layout
10
+ attr_writer :layouts
11
+
12
+ def initialize
13
+ @default_layout = 'Avery7160'
14
+ end
15
+
16
+ # @return [Hash]
17
+ def layouts
18
+ @layouts ||= YAML.load_file(File.expand_path('layouts.yml', __dir__))
19
+ end
20
+ end
21
+
22
+ # Current configuration
23
+ #
24
+ # @return [Prawn::LabelSheet::Configuration]
25
+ def self.config
26
+ @config ||= Configuration.new
27
+ end
28
+
29
+ # Define configuration
30
+ #
31
+ # @param configuration [Prawn::LabelSheet::Configuration]
32
+ def self.config=(configuration)
33
+ @config = configuration
34
+ end
35
+
36
+ # Modify configuration
37
+ #
38
+ # @yieldparam config [Prawn::LabelSheet::Configuration]
39
+ def self.configure
40
+ yield config
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ # Combined config for Prawn document and grid
2
+ #
3
+ # defaults:
4
+ # page_size: A4
5
+ # top_margin: 40
6
+ # left_margin: 20
7
+ #
8
+ Avery7160:
9
+ bottom_margin: 43.9
10
+ column_gutter: 7.08662
11
+ columns: 3
12
+ left_margin: 19.843
13
+ page_size: A4
14
+ right_margin: 19.843
15
+ row_gutter: 0
16
+ rows: 7
17
+ top_margin: 43.9
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prawn
4
+ class LabelSheet
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'prawn/label_sheet/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'prawn-label_sheet'
9
+ spec.version = Prawn::LabelSheet::VERSION
10
+ spec.authors = ['Tom Crouch']
11
+ spec.email = ['tom.crouch@gmail.com']
12
+
13
+ spec.summary = 'Generate PDF sets of labels & stickers.'
14
+ spec.description = <<~DESCRIPTION
15
+ Generate sets of labels or stickers programmatically using Prawn PDF.
16
+ DESCRIPTION
17
+ spec.homepage = 'https://github.com/tcrouch/prawn-label_sheet'
18
+ spec.license = 'MIT'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
22
+ # into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0")
25
+ .reject { |f| f.match(%r{^(test|spec|features|example)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+ spec.required_ruby_version = ">= 2.4.0"
31
+
32
+ spec.add_dependency 'polyfill', '~> 1.1.0'
33
+ spec.add_dependency 'prawn'
34
+
35
+ spec.add_development_dependency 'bundler', '~> 2.0'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'relaxed-rubocop', '~> 2.4'
38
+ spec.add_development_dependency 'rspec', '~> 3.0'
39
+ spec.add_development_dependency 'rubocop', '~> 0.74'
40
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn-label_sheet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Crouch
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: polyfill
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: prawn
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: relaxed-rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.74'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.74'
111
+ description: 'Generate sets of labels or stickers programmatically using Prawn PDF.
112
+
113
+ '
114
+ email:
115
+ - tom.crouch@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".rubocop.yml"
123
+ - ".travis.yml"
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - lib/prawn/label_sheet.rb
131
+ - lib/prawn/label_sheet/configuration.rb
132
+ - lib/prawn/label_sheet/layouts.yml
133
+ - lib/prawn/label_sheet/version.rb
134
+ - prawn-label_sheet.gemspec
135
+ homepage: https://github.com/tcrouch/prawn-label_sheet
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: 2.4.0
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.7.6
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Generate PDF sets of labels & stickers.
159
+ test_files: []