jordanbyron/prawn-labels

I need to have options[:document] values take precedence over type values

jcbc98 opened this issue · 5 comments

I found that I needed to have the document options for margin values that I specify take precedence over the ones set in the type. The way I did this was to switch around the merge in the Labels initialize method:

      @document = Document.new(
                  { 
                      :page_size      => type["paper_size"],
                      :top_margin     => type["top_margin"],
                      :bottom_margin  => type["bottom_margin"],
                      :left_margin    => type["left_margin"],
                      :right_margin   => type["right_margin"]

                   }.merge(options[:document]))

@jcbc98 take a look at the custom label types section of the README. I think that's what you are looking for.

I am using a custom label type, however I have a client whose printer feeds in its paper differently and so the standard Avery measurements don't work. So, I have given him form inputs for specifying the top and left margins so he can adjust as needed (I calculate the bottom margin so that the label height comes out correctly). Here's what I'm doing:

...
  Prawn::Labels.types = "#{RAILS_ROOT}/config/prawn_types.yaml"
...
    labels = Prawn::Labels.new(addresses, { :type => "Avery5162", :offset => starting_label, :shrink_to_fit => true, 
                                :document => { :top_margin => top, :bottom_margin => bottom, :left_margin => left } } ) do |pdf,address|
...

The problem is, options[:document].merge( { hash values from the type hash } ) causes the hash values from the type hash take precedence over the values I pass in through the document options.

(I used to have the ability to do an offset, so I left it in; however, I took away the form input and set starting_label to 1 for now.)

@jcbc98 rather than loading from a yaml file use the hash syntax. That's outlined in the README as well.

Something like:

custom_type = YAML.load_file("#{RAILS_ROOT}/config/prawn_types.yaml")

custom_type['custom'].merge(:top_margin => top, :bottom_margin => bottom, :left_margin => left)

Prawn::Labels.types = custom_type

OK. For that middle line, I had to do the following (! to modify the hash, and double quotes because symbols and strings are treated as different keys):

custom_type['custom'].merge!("top_margin" => top, "bottom_margin" => bottom, "left_margin" => left)

Thanks!

No problem. Glad to help!! 😄

On Jul 12, 2013, at 8:22 PM, JCB Christopher notifications@github.com wrote:

OK. For that middle line, I had to do the following (! to modify the hash, and double quotes because symbols and strings are treated as different keys):

custom_type['custom'].merge!("top_margin" => top, "bottom_margin" => bottom, "left_margin" => left)
Thanks!


Reply to this email directly or view it on GitHub.