jordanbyron / prawn-labels

Prawn/Labels: A simple helper to generate labels for Prawn PDFs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any tips on positioning items within a label?

jfrux opened this issue · comments

commented

First off, thanks for building this - loving it so far.

Trying to figure out how to position things relative to the position of the label itself.

Prawn has "bounds" method that I would think would reveal this but it's not available within my code.

labels = Prawn::Labels.render(records, type: "Avery7160") do |pdf, record|
     bounds # nil
end

Hey @joshuairl,

Thanks! Looks like you forgot to call bounds on pdf:

labels = Prawn::Labels.render(records, type: "Avery7160") do |pdf, record|
  pdf.bounds # WIN!
end

You'll need to use the pdf variable to access any of prawn's methods.

commented

Yeah, how do I get the bounds relative to the label though... it seems that pdf.bounds returns all same for each label.
For instance... bounds.top is always 133 in my particular instance even though there are different labels.
Make sense?

Thanks again for the help!

On Dec 1, 2015, at 4:09 PM, Jordan Byron notifications@github.com wrote:

Hey @joshuairl https://github.com/joshuairl,

Thanks! Looks like you forgot to call bounds on pdf:

labels = Prawn::Labels.render(records, type: "Avery7160") do |pdf, record|
pdf.bounds # WIN!
end
You'll need to use the pdf variable to access any of prawn's methods.


Reply to this email directly or view it on GitHub #34 (comment).

commented

is Prawn::Labels.render vs .generate different in any way? Would it affect the block usage at all?
I mean, I know they are different... but are they different in any way significant to what I'm trying to do?

commented

Ideally I'd like to be able to place an image with an "at:" position relative to the label box, not relative to the entire page. Is this possible?

@joshuairl yes it is possible. Just use pdf.bounds. For example if you want to place your image at the top right of each label do the following:

fancy_names = ["Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg",
               "Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg",
               "Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg",
               "Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg",
               "Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg"]
image = open("http://jordanbyron.com/me.png")
image_size = 48

Prawn::Labels.generate(pdf_path, fancy_names, type: "Avery5160") do |pdf, name|
  pdf.stroke_bounds
  pdf.text name
  pdf.image image,
    at: [pdf.bounds.right - image_size, pdf.bounds.top ]
end

screen shot 2015-12-02 at 3 00 47 pm

commented

Thanks for the extensive explanation. You rock.
However, attempting to do the same with a text box or with pdf.bounds.top+20 and for some reason the higher the number I add to the pdf.bounds.top the more negative it goes outside of the box above. Confusing me!! haha

@joshuairl No problem. Head over to http://prawnpdf.org/api-docs/2.0/ and familuarlize yourself with Prawn's docs. That should get you unstuck. Best of luck!