jordanbyron / prawn-labels

Prawn/Labels: A simple helper to generate labels for Prawn PDFs
Other
91 stars 41 forks source link

How to generate multiple labels per name? #10

Closed mathuin closed 13 years ago

mathuin commented 13 years ago

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.

jordanbyron commented 13 years ago

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: https://github.com/jordanbyron/prawn-labels/issues/10

mathuin commented 13 years ago

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.

jordanbyron commented 13 years ago

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.

mathuin commented 13 years ago

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

Jack.