Start new page automatically when using stacked bounding boxes in Prawn

安稳与你 提交于 2019-12-06 06:59:56

问题


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?

来源:https://stackoverflow.com/questions/13624463/start-new-page-automatically-when-using-stacked-bounding-boxes-in-prawn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!