Start new page automatically when using stacked bo

2020-07-22 10:08发布

问题:

I want to simulate the behaviour of a table in Prawn, but can't use table because of the limitation of things I can draw inside cells.

So I am using bounding boxes in order to create contexts for the elements inside each row. The problem I'm having has to do with the rows.

I'm trying this:

require 'prawn'

Prawn::Document.generate("test.pdf") do
  move_down 50
  bounding_box [0, cursor], width: bounds.width do
    20.times do |i|
      stroke_bounds && start_new_page if (i+1) % 11 == 0
      bounding_box [0, cursor], width: bounds.width, height: 50 do
        fill_color "cccccc"
        fill_rectangle [0, bounds.height], bounds.width, bounds.height
      end
    end
    stroke_bounds
  end
end

But I feel this is very hacky and not optimal, due to the fact that the containing bounding_box can't be repositioned after start_new_page (I want it to start a bit higher up), and the fact that I have to manually specify where the page break happens (every 11th element in this case). I tried working with bounds.parent.height and with the cursor to check if it reached the end, but it seems the bounds.height increases even after the page break.

Do you have any suggestions on how to improve this solution?

标签: ruby prawn