This snippet of code produces unexpected results:
require 'prawn'
Prawn::Document.generate("test.pdf") do
move_down 50
bounding_box [0, cursor], width: bounds.width do
20.times do
bounding_box [0,0], width: bounds.width, height: 50 do
stroke_bounds
end
end
end
end
The bounding boxes are stacked, one below each other, even though their coordinates are [0,0]. I don’t seem to grasp why this happens… Shouldn’t they be overlapping?
I figured it out:
Prawn’s coordinate system starts at the bottom left corner, which I knew, but somehow I overlooked it in this example.
When the first box of those 20 renders, the surrounding bounding_box is enlarged to fit it. Thus the 0,0 coordinates are now pointing to the bottom left of the containing bounding box, which is exactly at the bottom of the first box.
Then when the next box renders, the 0,0 is still at the bottom left, so it renders at the bottom of the first box, and so on…
Note: I was actually trying to start a new page once the boxes reach the end of the page, and I have posted another question for that.