jordanbyron/prawn-labels

Using Prawn Labels as part of a prawn document

Opened this issue · 3 comments

I'm generating invoicing pdfs, and I'd like the last page to be a page with the mailing label for a single customer or all customers. I have the pdf generating the invoice, but the page that i expect the labels to render on is just a blank page. My method that creates the pdf is this:

def to_pdf

    if @pilgrims.size > 1

      @pilgrims.each do |p|
        render_invoice @trip.first, p
        start_new_page unless p == @pilgrims.last
      end
      start_new_page
      addresses = ["Hello", "World"]
      Prawn::Labels.render(addresses, :type => "Avery5160") do |pdf, address|
        pdf.text address
      end
    else
      render_invoice @trip.first, @pilgrims.first
      start_new_page
     addresses = ["Hello", "World"]
      Prawn::Labels.render(addresses, :type => "Avery5160") do |pdf, address|
        pdf.text address
      end
    end
    render
  end

I might be incorrectly assuming that render can be used instead of generate, since I don't want to generate a completely new document just a page with the labels.

What am i doing wrong?

@Weimar27 have you read the source code to see what Prawn::Labels.render does? Take a dive inside and see if you can answer your own question. Luckily this project is super tiny so cracking open the code isn't that scary. Let me know if you need a little guidance and I'd be more than happy to walk you through the steps I took to answer your question. 😄

In the meantime I am going to close this ticket since it isn't related to any problems with the project itself. Even though it's closed please post your progress back here as this question may be helpful to others browsing the archives. Thanks ❤️

i did look at the source code, and the above code is what i came up with based looking at the source code. I'm passing in data (addresses, the options of type and on the bottom of shrink to fit, and block is pdf, address). And i'm still getting a blank page.

The problem you are going to run into is located here on line 56 of lib/prawn/labels.rb

@document = Document.new  options[:document] ....

Prawn labels creates its own Prawn document and renders the labels to it. So you can grab that document and play with it after Prawn Labels renders the labels to the document, but I don't know what it would take to render pages before the labels.

You have a few options:

  1. Render two different PDFs and stitch them together using pdftk:
pdftk path/to/generated/*.pdf cat output combined_pdfs.pdf compress
  1. Grab the generated document after Prawn Label's does its thing using the Prawn::Label#document accessor.
label = Prawn::Label.new(data, type: 'some-label-type') do |pdf|
  label_pdf.text "Hi"
end

pdf = label.document

pdf.text "This is writing on my labels!!"

Something like the above should do the trick. Personally I think option one is your best bet. Good luck!