Skip to content

Commit

Permalink
Only draw table header on 1st page if >=1 body row
Browse files Browse the repository at this point in the history
Check, before rendering the table, to be sure that we can draw the first
body row (excluding any header). If we can't, start the entire table on
the next page.

Closes prawnpdf#152.
  • Loading branch information
bradediger committed Sep 25, 2010
1 parent efdee43 commit fe79c39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/prawn/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,20 @@ def draw
# because even if we are in a stretchy bounding box, flowing to the next
# page will not buy us any space if we are at the top.
if @pdf.y > @pdf.bounds.height + @pdf.bounds.absolute_bottom - 0.001
# we're at the top of our bounds
started_new_page_at_row = 0
else
started_new_page_at_row = -1

# If there isn't enough room left on the page to fit the first data row
# (excluding the header), start the table on the next page.
needed_height = row(0).height
needed_height += row(1).height if @header
if needed_height > @pdf.y - ref_bounds.absolute_bottom
@pdf.bounds.move_past_bottom
offset = @pdf.y
started_new_page_at_row = 0
end
end

@cells.each do |cell|
Expand Down
15 changes: 15 additions & 0 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
}.should.not.raise
end

it "should allow a table with a header but no body" do
lambda { @pdf.table([["Header"]], :header => true) }.should.not.raise
end

# TODO: pending colspan
xit "should accurately count columns from data" do
# First data row may contain colspan which would hide true column count
Expand Down Expand Up @@ -469,6 +473,17 @@
end.page_count.should == 2
end

it "should only draw first-page header if the first body row fits" do
pdf = Prawn::Document.new

pdf.y = 60 # not enough room for a table row
pdf.table [["Header"], ["Body"]], :header => true

output = PDF::Inspector::Page.analyze(pdf.render)
# Ensure we only drew the header once, on the second page
output.pages[0][:strings].should.be.empty
output.pages[1][:strings].should == ["Header", "Body"]
end
end

describe "#style" do
Expand Down

0 comments on commit fe79c39

Please sign in to comment.