ksugiarto
ksugiarto

Reputation: 951

Create a sub-total each page n prawn rails

So I generate my report using prawn on my rails with some data of accounting. I do my prawn with create a file.pdf.prawn an put the layout of the report there, not from a model. I need so my report have a sub total each page on the bottom. Which is different from all_total that generate from controller.

I mean, like I need to know how much data that would be printed on one page and then I can sum that data.

Thanks and regard

Upvotes: 0

Views: 530

Answers (1)

andrej
andrej

Reputation: 4753

If the height of rows is equal you can simply test how many of them fits on your paper and then count it by your own.

If your rows are not equally sized you can try to count size of rows similar the Table and Cell classes does it. You have only to add condition for defining one row:

# this code probably won't work because it's not tested
# I just typed it right here to show concept

module Prawn
  class Cell
    def custom_row_height(row_id)
      each do |cell|
        index = cell.send(:row)
        if index == row_id do
          result = cell.send(:height_ignoring_span)].compact.send(:max) 
        end
      end
      result 
    end

code from https://github.com/prawnpdf/prawn/blob/master/lib/prawn/table/cells.rb and https://github.com/prawnpdf/prawn/blob/master/lib/prawn/table/cell.rb:

module Prawn
  class Cell
    # Sum up a min/max value over rows or columns in the cells selected.
    # Takes the min/max (per +aggregate+) of the result of sending +meth+ to
    # each cell, grouped by +row_or_column+.
    #
    def aggregate_cell_values(row_or_column, meth, aggregate)
      values = {}
      each do |cell|
        index = cell.send(row_or_column)
        values[index] = [values[index], cell.send(meth)].compact.send(aggregate)
      end
      values.values.inject(0, &:+)
    end

module Prawn
  class Table
    class Cell
      # Returns the total height of all rows in the selected set.
      def height
        aggregate_cell_values(:row, :height_ignoring_span, :max)
      end

Upvotes: 0

Related Questions