I use prawn gem to generate a pdf file with tables and other sections. The text_box position are static and it is working as expected. Now I want the text_box below the table to be dynamic. Below is the part of the code I am using,
view.text_box('Notes', at: [1, 575], width: 600, style: :bold)
view.table(table_data, contact_section_options) do |table|
table.row(0).border_width = 0
table.row(0).style(size: 11, font_style: :bold)
end
view.text_box('Service Instructions', at: [1, 225], width: 600, style: :bold)
Here the table data is now dynamic and hence I can’t set a constant position for service Instructions
because it is overlapping with the above table. Is there a way we can find the bottom position of the above table and set the service Instructions
position as dynamic.
I would do a move_down
under the table, or subtract lineheight from cursor
for the location. e.g.
view.text_box('Notes', at: [1, 575], width: 600, style: :bold)
view.table(table_data, contact_section_options) do |table|
table.row(0).border_width = 0
table.row(0).style(size: 11, font_style: :bold)
end
view.move_down 12 # this should be set to whatever the standard lineheight is for your doc
view.text_box('Service Instructions', at: [1, cursor], width: 600, style: :bold)
More info on using cursor
here: https://prawnpdf.org/manual.pdf#page=6
Just keep in mind that because prawn draws these pdfs from bottom to the top, be careful of mix-and-matching move_down
with at
positions. For instance:
text_box "first cursor at: #{cursor}", at: [35, cursor]
text_box "second cursor at: #{cursor - 12}", at: [35, cursor - 12]
text_box "third cursor at: #{cursor - 24}", at: [35, cursor - 24]
creates
But that means you will need to keep track of the current position throughout your document which can get tricky. It might easier to just use move_down
, which automatically resets the cursor
, which visually produces the same result.
text_box "first cursor at: #{cursor}", at: [35, cursor]
move_down 12
text_box "second cursor at: #{cursor}", at: [35, cursor]
move_down 12
text_box "third cursor at: #{cursor}", at: [35, cursor]