jordanbyron/prawn-labels

How to generate multiple labels per name?

mathuin opened this issue · 4 comments

The documentation shows how to print one label per name with a loop iterating over the name list. How can I generate multiple labels per name? I tried adding another method to the do/end loop, but that returned Prawn::Errors::EmptyGraphicStateStack. I can run multiple generate()'s but that wastes labels. Am I missing something obvious? The labels aren't identical so I can't just double up the names list. Thanks!

Jack.

Hi Jack,

I'm not sure I understand. Could you show me an example output of what you'd like to create?

Jordan

On Oct 24, 2011, at 4:41 PM, Jack Twilley wrote:

The documentation shows how to print one label per name with a loop iterating over the name list. How can I generate multiple labels per name? I tried adding another method to the do/end loop, but that returned Prawn::Errors::EmptyGraphicStateStack. I can run multiple generate()'s but that wastes labels. Am I missing something obvious? The labels aren't identical so I can't just double up the names list. Thanks!

Jack.

Reply to this email directly or view it on GitHub:
#10

I have designed a "front label" and a "back label" for my homebrew. I am prototyping these labels with Ruby before installing the code in my Ruby on Rails application. The Ruby script here fakes the database connection with static data, including seqarray which is an array of sequences ("A1", "A2", stuff like that). Here's the relevant code:

Prawn::Labels.generate("sideways.pdf", seqarray, { :type => "Avery6572", :vertical_text => true } ) do |pdf, seq|
  frontlabel(pdf, seq)
  backlabel(pdf, seq)
end

If I use only one function in the loop (either frontlabel() or backlabel()) then I get either the front or the back labels for every sequence value. If I use both functions, I get the EmptyGraphicStateStack error I mentioned above. I could generate two separate PDFs, but that would waste labels. Does this make sense? I can post a pastebin of the entire program if you like.

Jack.

Sounds like you need to create an array of objects, two for each name, one for the front and one for the back.

class FrontLabel
  def initialize(name)
    @name = name
  end

  def render(pdf)
    pdf.text @name
    # etc ...
  end
end

class BackLabel
  def initialize(name)
    @name = name
  end

  def render(pdf)
    pdf.text @name
    # etc ...
  end
end

names = ["Jordan", "Jack"]
labels  = names.map {|name| [ FrontLabel.new(name), BackLabel.new(name) ] }

Prawn::Labels.generate("sideways.pdf", labels) do |pdf, label|
  label.render
end

Hope that helps.

That worked almost as written. All it needed was "labels.flatten!". Thanks for the help!

Jack.