class Prawn::LabelSheet

Document supporting bulk label/sticker generation

Constants

VERSION

Attributes

document[R]

Override Prawn::View#document

layout[R]

Override Prawn::View#document

Public Class Methods

config() click to toggle source

Current configuration

@return [Prawn::LabelSheet::Configuration]

# File lib/prawn/label_sheet/configuration.rb, line 25
def self.config
  @config ||= Configuration.new
end
config=(configuration) click to toggle source

Define configuration

@param configuration [Prawn::LabelSheet::Configuration]

# File lib/prawn/label_sheet/configuration.rb, line 32
def self.config=(configuration)
  @config = configuration
end
configure() { |config| ... } click to toggle source

Modify configuration

@yieldparam config [Prawn::LabelSheet::Configuration]

# File lib/prawn/label_sheet/configuration.rb, line 39
def self.configure
  yield config
end
generate(filename, labels, **options, &block) click to toggle source

Render and persist a set of label sheets

@param labels [Enumerable, CSV] @param options (see initialize)

# File lib/prawn/label_sheet.rb, line 27
def self.generate(filename, labels, **options, &block)
  pdf = new(labels, options, &block)
  pdf.document.render_file(filename)
end
new(labels, **options) { |pdf, item| ... } click to toggle source

@param labels [Enumerable] collection of labels @option options [String] :layout @option options [Proc, Integer] :break_on @option options [Prawn::Document] :document

# File lib/prawn/label_sheet.rb, line 36
def initialize(labels, **options)
  @layout = setup_layout(options[:layout])

  @document = resolve_document(options[:document])
  @document.define_grid @layout
  # @document.on_page_create { self.instance_variable_set :@count, 0 }

  @count = 0
  @break_on = options[:break_on]

  labels.each do |label|
    make_label(label, options) { |pdf, item| yield pdf, item }
  end
end

Public Instance Methods

make_label(item, _options) { |document, item| ... } click to toggle source

Generate individual label

@param item [#[], Object] @yieldparam doc [Prawn::Document] document @yieldparam item [Object] label item @return [Integer] tally of labels on current page

# File lib/prawn/label_sheet.rb, line 57
def make_label(item, _options)
  break_page if break_page?(item)

  @document.grid(*gridref).bounding_box do
    yield @document, item
  end
  @count += 1
end

Protected Instance Methods

break_page() click to toggle source

Begin a new page

# File lib/prawn/label_sheet.rb, line 83
def break_page
  @document.start_new_page
  @count = 0
end
break_page?(item) click to toggle source

Determine whether to begin a new page

@param item [Proc, []] @return [Boolean]

# File lib/prawn/label_sheet.rb, line 72
def break_page?(item)
  return unless @break_on

  val = @break_on.is_a?(Proc) ? @break_on.call(item) : item[@break_on]
  return false if @last_val == val

  @last_val = val
  @count.positive?
end
config() click to toggle source

@return [Prawn::LabelSheet::Configuration]

# File lib/prawn/label_sheet.rb, line 138
def config
  self.class.config
end
gridref() click to toggle source

Current grid coordinates

@return [Array(Integer, Integer)]

# File lib/prawn/label_sheet.rb, line 91
def gridref
  q, r = @count.divmod(@document.grid.rows * @document.grid.columns)

  if q.positive? && r.zero?
    @document.start_new_page
    return [0, 0]
  end

  r.divmod(@document.grid.columns)
end
resolve_document(doc) click to toggle source

@param doc [Prawn::Document, nil] @return [Prawn::Document]

# File lib/prawn/label_sheet.rb, line 131
def resolve_document(doc)
  return doc.start_new_page(@layout) if doc

  Document.new @layout
end
resolve_layout(layout_def) click to toggle source

Lookup layout definition

@param layout_def [String, to_h] layout definition or identifier @return [Hash]

# File lib/prawn/label_sheet.rb, line 122
def resolve_layout(layout_def)
  dfn = layout_def || config.default_layout
  return dfn.to_h if dfn.respond_to?(:to_h)

  config.layouts[dfn] || raise(Error, 'Unknown layout')
end
setup_layout(layout_def) click to toggle source

@param layout_def [String, to_h] layout definition or identifier @return [Hash] rubocop:disable Style/RescueModifier

# File lib/prawn/label_sheet.rb, line 105
def setup_layout(layout_def)
  defs = resolve_layout(layout_def).slice(
    'page_size', 'columns', 'rows',
    'top_margin', 'bottom_margin',
    'left_margin', 'right_margin',
    'column_gutter', 'row_gutter'
  )
  {
    page_size: 'A4', top_margin: 40, left_margin: 20
  }.merge!(defs.transform_keys { |key| key.to_sym rescue key })
end