From cecdf8007965248425fa3420b32fe9710e0d1222 Mon Sep 17 00:00:00 2001 From: Evan Gallup <41955146+egallup02360@users.noreply.github.com> Date: Sat, 10 Aug 2024 22:02:22 -0400 Subject: [PATCH 1/7] Defined Headers --- lib/prawn/table.rb | 27 ++++++++++++++++++++++++--- prawn-table.gemspec | 1 + spec/table_spec.rb | 11 +++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/prawn/table.rb b/lib/prawn/table.rb index 718fe698..c1196cc1 100644 --- a/lib/prawn/table.rb +++ b/lib/prawn/table.rb @@ -142,10 +142,15 @@ def initialize(data, document, options={}, &block) @pdf = document @cells = make_cells(data, table_opts.delete(:cell_style) || {}) @header = false + @headers = nil table_opts.each do |k, v| send("#{k}=", v) if respond_to?("#{k}=") end + unless @headers.nil? + @headers = make_cells(@headers, table_opts.delete(:cell_style) || {}) + end + if block block.arity < 1 ? instance_eval(&block) : block[self] end @@ -229,6 +234,8 @@ def height # attr_writer :header + attr_accessor :headers + # Accepts an Array of alternating row colors to stripe the table. # attr_writer :row_colors @@ -296,6 +303,10 @@ def draw # page is finished. cells_this_page = [] + if !@headers.nil? && @header && @pdf.page_count == 1 + add_header(@pdf.cursor, cells_this_page) + end + @cells.each do |cell| if start_new_page?(cell, offset, ref_bounds) # draw cells on the current page and then start a new one @@ -506,7 +517,11 @@ def fits_on_page?(needed_height, use_reference_bounds = false) def header_rows header_rows = Cells.new number_of_header_rows.times do |r| - row(r).each { |cell| header_rows[cell.row, cell.column] = cell.dup } + if @headers.nil? + row(r).each { |cell| header_rows[cell.row, cell.column] = cell.dup } + else + headers.row(r).each { |cell| header_rows[cell.row, cell.column] = cell.dup } + end end header_rows end @@ -673,13 +688,19 @@ def position_cells # Calculate x- and y-positions as running sums of widths / heights. x_positions = column_widths.inject([0]) { |ary, x| ary << (ary.last + x); ary }[0..-2] - x_positions.each_with_index { |x, i| column(i).x = x } + x_positions.each_with_index do |x, i| + column(i).x = x + headers.column(i).x = x unless headers.nil? || headers.column(i).nil? + end # y-positions assume an infinitely long canvas starting at zero -- this # is corrected for in Table#draw, and page breaks are properly inserted. y_positions = row_heights.inject([0]) { |ary, y| ary << (ary.last - y); ary}[0..-2] - y_positions.each_with_index { |y, i| row(i).y = y } + y_positions.each_with_index do |y, i| + row(i).y = y + headers.row(i).y = y unless headers.nil? || headers.row(i).nil? + end end # Sets up a bounding box to position the table according to the specified diff --git a/prawn-table.gemspec b/prawn-table.gemspec index 31b20573..f0aaffd8 100644 --- a/prawn-table.gemspec +++ b/prawn-table.gemspec @@ -28,6 +28,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency('prawn-dev', '~> 0.3.0') spec.add_development_dependency('prawn-manual_builder', ">= 0.2.0") spec.add_development_dependency('pdf-reader', '~>1.2') + spec.add_development_dependency('matrix') spec.homepage = "https://github.com/prawnpdf/prawn-table" spec.description = < true, :headers => headers) + output = PDF::Inspector::Text.analyze(@pdf.render) + expect(@pdf.page_count).to eq 2 + expect(output.strings).to eq headers.flatten + data.flatten[0..-3] + headers.flatten + + data.flatten[-2..-1] + end + it "draws headers at the correct position" do data = [["header"]] + [["foo"]] * 40 From 736f95ed22be7ed56a23ce1d6fcabf713f586a0e Mon Sep 17 00:00:00 2001 From: Evan Gallup <41955146+egallup02360@users.noreply.github.com> Date: Sat, 10 Aug 2024 22:23:42 -0400 Subject: [PATCH 2/7] Comments for added headers option --- lib/prawn/table.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/prawn/table.rb b/lib/prawn/table.rb index c1196cc1..a4ac0236 100644 --- a/lib/prawn/table.rb +++ b/lib/prawn/table.rb @@ -69,6 +69,10 @@ module Errors # numbering (for styling and other row-specific options) always indexes # based on your data array. Whether or not you have a header, row(n) always # refers to the nth element (starting from 0) of the +data+ array. + # +headers+:: + # If set to a multidimensional array of data, the defined header will be + # repeated on every page. + # +header+ must also be set to +true+ for this to function. # +column_widths+:: # Sets widths for individual columns. Manually setting widths can give # better results than letting Prawn guess at them, as Prawn's algorithm @@ -234,6 +238,10 @@ def height # attr_writer :header + # If set to a valid multidimensional array of data the data specified here + # will be used on each page as the header. +header+ must be set to +true+ + # for this to function. + # attr_accessor :headers # Accepts an Array of alternating row colors to stripe the table. From aaa161b0cdef24f56a67a5aa75f982368c797007 Mon Sep 17 00:00:00 2001 From: Evan Gallup <41955146+egallup02360@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:19 -0400 Subject: [PATCH 3/7] Don't try to add the first line --- lib/prawn/table.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/prawn/table.rb b/lib/prawn/table.rb index a4ac0236..7250c8e0 100644 --- a/lib/prawn/table.rb +++ b/lib/prawn/table.rb @@ -311,10 +311,6 @@ def draw # page is finished. cells_this_page = [] - if !@headers.nil? && @header && @pdf.page_count == 1 - add_header(@pdf.cursor, cells_this_page) - end - @cells.each do |cell| if start_new_page?(cell, offset, ref_bounds) # draw cells on the current page and then start a new one From 5aaf8ab214c96ee5c9f14b0442bf75205a1ff2bf Mon Sep 17 00:00:00 2001 From: Evan Gallup <41955146+egallup02360@users.noreply.github.com> Date: Mon, 12 Aug 2024 01:01:40 -0400 Subject: [PATCH 4/7] Update spec --- spec/table_spec.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spec/table_spec.rb b/spec/table_spec.rb index 43056049..26344945 100644 --- a/spec/table_spec.rb +++ b/spec/table_spec.rb @@ -1154,13 +1154,14 @@ end it "should repeat defined headers across pages" do - data = [["foo","bar"]] * 31 - headers = [["baz","foobar"]] + data = [["foo","bar"]] * 30 + first_page_headers = [["bazbar", "foobaz"]] + repeating_headers = [["baz","foobar"]] @pdf = Prawn::Document.new - @pdf.table(data, :header => true, :headers => headers) + @pdf.table(first_page_headers + data, :header => true, :headers => repeating_headers) output = PDF::Inspector::Text.analyze(@pdf.render) expect(@pdf.page_count).to eq 2 - expect(output.strings).to eq headers.flatten + data.flatten[0..-3] + headers.flatten + + expect(output.strings).to eq first_page_headers.flatten + data.flatten[0..-3] + repeating_headers.flatten + data.flatten[-2..-1] end From b1addf6a7d34ff29c5c454d3ffa1046bc16c712c Mon Sep 17 00:00:00 2001 From: Evan Gallup <41955146+egallup02360@users.noreply.github.com> Date: Mon, 12 Aug 2024 11:53:31 -0400 Subject: [PATCH 5/7] Inherit widths and heights --- lib/prawn/table.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/prawn/table.rb b/lib/prawn/table.rb index 7250c8e0..6dc7e9e6 100644 --- a/lib/prawn/table.rb +++ b/lib/prawn/table.rb @@ -676,6 +676,9 @@ def set_column_widths column_widths.each_with_index do |w, col_num| column(col_num).width = w end + if !headers.nil? + headers.column(col_num).width = w + end end # Assigns the row heights to each cell. This ensures that every cell in a @@ -683,6 +686,9 @@ def set_column_widths # def set_row_heights row_heights.each_with_index { |h, row_num| row(row_num).height = h } + if !headers.nil? + row_heights.each_with_index { |h, row_num| headers.row(row_num).height = h } + end end # Set each cell's position based on the widths and heights of cells From 666e29327c7eff95816422da0c8b8bfbce62432e Mon Sep 17 00:00:00 2001 From: Evan Gallup <41955146+egallup02360@users.noreply.github.com> Date: Mon, 12 Aug 2024 12:44:13 -0400 Subject: [PATCH 6/7] Fix --- lib/prawn/table.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/prawn/table.rb b/lib/prawn/table.rb index 6dc7e9e6..567c9e0c 100644 --- a/lib/prawn/table.rb +++ b/lib/prawn/table.rb @@ -675,6 +675,7 @@ def natural_width def set_column_widths column_widths.each_with_index do |w, col_num| column(col_num).width = w + headers.column(col_num).width = w if !headers.nil? && !headers.column(col_num).nil? end if !headers.nil? headers.column(col_num).width = w @@ -685,9 +686,9 @@ def set_column_widths # row is the same height. # def set_row_heights - row_heights.each_with_index { |h, row_num| row(row_num).height = h } - if !headers.nil? - row_heights.each_with_index { |h, row_num| headers.row(row_num).height = h } + row_heights.each_with_index do |h, row_num| + row(row_num).height = h + headers.row(row_num).height = h if !headers.nil? && !headers.row(row_num).nil? end end From bf4c861e435e10d0616f8dedebe85f7579a5f66a Mon Sep 17 00:00:00 2001 From: Evan Gallup <41955146+egallup02360@users.noreply.github.com> Date: Mon, 12 Aug 2024 12:45:37 -0400 Subject: [PATCH 7/7] Bad conflict resolution --- lib/prawn/table.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/prawn/table.rb b/lib/prawn/table.rb index 567c9e0c..f4511e0a 100644 --- a/lib/prawn/table.rb +++ b/lib/prawn/table.rb @@ -677,9 +677,6 @@ def set_column_widths column(col_num).width = w headers.column(col_num).width = w if !headers.nil? && !headers.column(col_num).nil? end - if !headers.nil? - headers.column(col_num).width = w - end end # Assigns the row heights to each cell. This ensures that every cell in a