Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prawnto displaying tables that don't break when new page

I have a variable number of tables with variable number of rows and I want to have them displaying one after the other but if a table doesn't fit on the current page put it on the next then continue on. I have put the table in a transaction so I can roll back then print it if the height will fit on curent page but how do I get the table height?

I have this code at the moment

pdf.transaction do 

pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]

pdf.move_down 20

#pdf.rollback 
end

Any help on this would be great. Or any other way to do this ?

Best Regards Rick

like image 681
richard moss Avatar asked Jan 17 '10 16:01

richard moss


1 Answers

4 years later... :)

As @m-x wrote it, rollback was disabled for security reason, like "group", and is still not implemented. So, here how I deal with break pages for tables :

Big and simple table (one row per data)

Just use header option

pdf.table @data,
  header: true, # You can use 'header: 2' if your header take two rows
  font_size: 12, 
  border_style: :grid,
  horizontal_padding: 10,
  vertical_padding: 3,
  border_width: 2,
  position: :left,
  row_colors: ["FFFFFF","DDDDDD"]

Small table or complexe table

  • make table
  • check if you need break page
  • draw table

With your example :

t = pdf.make_table @data,
  font_size: 12, 
  border_style: :grid,
  horizontal_padding: 10,
  vertical_padding: 3,
  border_width: 2,
  position: :left,
  row_colors: ["FFFFFF","DDDDDD"]

if cursor - t.height < 0
  start_new_page
end

t.draw

Hope that helps

like image 100
Florent L. Avatar answered Oct 27 '22 12:10

Florent L.