Table colspan/rowspan added to Prawn master

464 views
Skip to first unread message

Brad Ediger

unread,
Dec 10, 2011, 11:44:49 PM12/10/11
to prawn-ruby
Hi Prawn users,
I took some time today and implemented colspan / rowspan for Prawn
tables. This feature has been on the wish list for quite a while and
is finally a reality. Please try it out and let me know if you find
any issues. There is a manual page outlining how to use this new
feature. The changes are on Prawn's master branch; the diff is here:

https://github.com/sandal/prawn/compare/c8218f36...ebd13e51

Because we're nearing completion on 1.0, this feature will probably go
out with Prawn 1.1.

-be

Randy Parker

unread,
Dec 14, 2011, 5:49:24 PM12/14/11
to prawn...@googlegroups.com
Thank you Brad!

I've been fooling with it for a table today that has 150 row triplets (450 rows).
The table has a maximum of 4 columns, all of which are actually used only on row 1 of each triplet:
1) names, account-type, account-num, court-date
2) {:content => address, :colspan => 4}
3) just a spacer to the next meaningful row-pair.  It exists only to make the table more readable, so clerks can scan meaningful row-pairs more easily. This doesn't have to be an actual row really, I guess I could apply below-padding to the even-number rows or something.

Anyhow, it works, and is much simpler than triple nesting each component row in its own table!
But when I specify the column-widths of row 1, the table throws the error:

Table's width was set too small to contain its contents (min width = 472.5, requested width = 477)

I'm confused by this error, as there is no way for me to get either of the specific numbers it mentions by any combination of the widths I specify.  And the delta between "min" and "requested" changes in a way I can't understand as I vary specific columns widths.

I'll look in the source tomorrow - I have to go right now.

Thanks again,

- Randy
-- 
http://mobiledyne.com

Brad Ediger

unread,
Dec 14, 2011, 5:54:19 PM12/14/11
to prawn...@googlegroups.com
On Wed, Dec 14, 2011 at 4:49 PM, Randy Parker <randy.j...@gmail.com> wrote:
>
> I've been fooling with it for a table today that has 150 row triplets (450
> rows).
> The table has a maximum of 4 columns, all of which are actually used only on
> row 1 of each triplet:
> 1) names, account-type, account-num, court-date
> 2) {:content => address, :colspan => 4}
> 3) just a spacer to the next meaningful row-pair.  It exists only to make
> the table more readable, so clerks can scan meaningful row-pairs more
> easily. This doesn't have to be an actual row really, I guess I could apply
> below-padding to the even-number rows or something.
>
> Anyhow, it works, and is much simpler than triple nesting each component row
> in its own table!
> But when I specify the column-widths of row 1, the table throws the error:
>
> Table's width was set too small to contain its contents (min width = 472.5,
> requested width = 477)

I wouldn't rule out the possibility of a bug in Prawn. The sizing code
in particular is complicated and it can be tricky to figure out what
it's doing. If you can reduce this to an example that you can post
online for me to run, I'll be happy to take a look.

Cheers,
-be

Randy Parker

unread,
Dec 15, 2011, 12:58:28 PM12/15/11
to prawn...@googlegroups.com
On Wed, Dec 14, 2011 at 5:54 PM, Brad  wrote:
I wouldn't rule out the possibility of a bug in Prawn. The sizing code
in particular is complicated and it can be tricky to figure out what
it's doing. If you can reduce this to an example that you can post...


Here's a puzzling example.  I've formatted the table options so that you
can turn features on and off with a comment character.

When I run the program without specifying widths, it runs
successfully and prints the width array [236, 30, 50, 199].  But if I use the
box-selection tool in the Mac OSX app "Skim" to actually measure what
is drawn on the PDF, I get [168, 59, 59, 200] (plus or minus a point or
two, depending on my mousing accuracy).

Now if I re-run the program with the puts()-alleged widths as an input
spec, it fails with this error:

Table's width was set too small to contain its contents (min width 692.5, requested 540.0) (Prawn::Errors::CannotFit)

The sum of my measurements is 486, (which includes default 5pt padding)
The sum of my  specification  is  515 + 40pt (padding) = 545  (not so far from 540, so I
probably misunderstood something...)

I don't understand how the min width can be 692 when it prints out just fine in
a measured width of <500 when I comment out my width specification array?

require 'rubygems'
require 'prawn'

Prawn.debug = true

Prawn::Document.generate('table_span.pdf') do
  text "Randy's colspan test with specific column widths"
  cnum = 'cm1112______________________'
  rows = [['Randy Parker, Herb Schwartz',  'dp#', '880400', cnum]]
  rows << [{:content => '1605 Chantilly Dr, Atlanta, GA, 30324', :colspan => 4}]
  rows << ['Kellee Wheate, Holli Pippin ', 'dp#', '890500', cnum]
  rows << [{:content => '38 Prince Charles Dr, Harwich, MA, 02645', :colspan => 4}]

  table(rows, 
        #:cell_style => {:borders => []},
        :column_widths => [236, 30, 50, 199]
        ) do
          puts 4.times.inject([]){|array, i| array << column(i).width.to_i}.inspect
  end
end

# The output printed by the puts() statement: [236, 30, 50, 199]
# But the widths measured from Mac Skim are:  [168, 59, 59, 200]
# My measurement of col-3 is likely off by one, but what about cols 0 and 1 ?? 

Randy Parker

unread,
Dec 15, 2011, 1:24:25 PM12/15/11
to prawn...@googlegroups.com
On Thu, Dec 15, 2011 at 12:58 PM, Randy  wrote:

... I've formatted the table options so that you
can turn features on and off with a comment character....


[toy program]   - replace the one-liner "puts()" in the table with:

widths = 4.times.inject([]){|array, i| array << column(i).width.to_i}
puts "#{widths.inspect} = #{widths.inject(0,&:+)}"
 
Reply all
Reply to author
Forward
0 new messages