Topic: Loop trough grid Prawn

Hello everyone,

I want to generate a grid with labels. A barcode and some text should be generated on each label. The user must be able to insert an order with an array of products (lineitems):

- Product 1: 23 labels
- Product 2: 16 labels
- Product 3: 50 labels

So these three products should be printed in a pdf document. I have accomplished this. The only problem is that after the array of each product is printed, a new page is started. I would like one array. A new page must be started when the page is full. Here's my code:

pdf = Prawn::Document.new(:page_size => "A3", :page_layout => :portrait, :margin => 22)

pdf.define_grid(:columns => 8, :rows => 5, :column_gutter => 0, :row_gutter => 0)

#-----------------------------------------

@order.lineitems.each do |lineitem|

  lineitem.quantity.times do |i|       
 
    pos = i % 40                    # pos = label's position on the page (0-39)   
   
    box = pdf.grid(pos / 8, pos % 8)    # lay labels out in 4 columns
    # (print label in box)       

    pdf.start_new_page if pos == 0

    pdf.bounding_box box.top_left, :width => box.width, :height => box.height do         

      pdf.draw_text "LTB", :at => [10,170], :size => 15, :style => :bold   

      pdf.draw_text lineitem.label.product_number, :at => [88,182], :rotate => 270, :style => :bold         

      #generate barcode with Barby library 
      lineitem.label.barcode.annotate_pdf(pdf, :xdim => 0.5, :height => 40, :x => 25, :y => 60)

      #generate EAN-code underneath the barcode
      pdf.draw_text lineitem.label.ean.to_s, :at => [23,51], :size => 7                 
   
    end     
 
  end   
 
end



Maybe it's very simple, but I'm stuck with this for ages, so it seems...
I appreciate your help!

Rens