prawnto displaying tables that don't break when new page

后端 未结 4 743
面向向阳花
面向向阳花 2021-02-10 09:46

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

相关标签:
4条回答
  • 2021-02-10 10:10

    I'm a Prawn beginner, so this might not be the best solution, but it should work.

    You can get the table height if you consider the font size and vertical padding and the number of records you have in @data and you can get the current cursor position by calling Prawn::Document.cursor method.

    Having these two numbers you should be able to check whether the table fits on this page or not. If not, just start a new one (by calling Prawn::Document.start_new_page method).

    Otherwise the table will break automatically and will continue on the next page.

    0 讨论(0)
  • 2021-02-10 10:12

    Thanks Igor

    I am currently setting the current page and then in the transaction after the new table has been rendered and before the roll back setting new_page variable. Then i can roll back and chek if the new page var > current page var and if it is start new page and print the table. See code below.

    The problem is now the pdf.start_new_page says error but if i just take the pdf.rollback line out it works. See error below.

    Any ideas any one or any easier ways, there must be one!!

    thanks rick

    @current_page = pdf.page_count
    
    pdf.transaction do 
      pdf.move_down 20
    
      pdf.table @data,
        :font_size  => 12, 
        :border_style => :grid,
        :horizontal_padding => 10,
        :vertical_padding   => 3,
        :border_width       => 2,
        :position           => :left,
        :row_colors => ["FFFFFF","DDDDDD"]
    
      @the_next_page = pdf.page_count
      pdf.rollback
    
    end 
    
    if @the_next_page > @current_page
    
      pdf.start_new_page
    
      pdf.table @data,
        :font_size  => 12, 
        :border_style => :grid,
        :horizontal_padding => 10,
        :vertical_padding   => 3,
        :border_width       => 2,
        :position           => :left,
        :row_colors => ["FFFFFF","DDDDDD"]
    end 
    

    The error

    > You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.identifier
    
    Extracted source (around line #158): 
    
    155: end
    
    
    
    RAILS_ROOT: C:/InstantRails/rails_apps/Macrotec-Application
    
    Application Trace | Framework Trace | Full Trace 
    c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:302:in `go_to_page'
    c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:128:in `finalize_all_page_contents'
    c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `each'
    c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `finalize_all_page_contents'
    c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:344:in `render'
    C:/InstantRails/rails_apps/Macrotec-Application/app/views/quotations/show.pdf.prawn:158:in `_run_prawn_app47views47quotations47show46pdf46prawn'
    
    0 讨论(0)
  • 2021-02-10 10:23

    @current_page = pdf.page_count

    @roll = pdf.transaction do 
      pdf.move_down 20
    
      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.rollback if pdf.page_count > @current_page
    
    end 
    
    if @roll == false
    
      pdf.start_new_page
    
      pdf.table @data,
        :font_size  => 12, 
        :border_style => :grid,
        :horizontal_padding => 10,
        :vertical_padding   => 3,
        :border_width       => 2,
        :position           => :left,
        :row_colors => ["FFFFFF","DDDDDD"]
    end
    

    I hope this works for you as for me :-)

    0 讨论(0)
  • 2021-02-10 10:30

    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

    0 讨论(0)
提交回复
热议问题