how to know height of table

453 views
Skip to first unread message

mattia lipreri

unread,
Oct 27, 2009, 5:58:46 PM10/27/09
to Prawn
Hi,
I need to know the table height before to print it beacuse I have to decide if my page has enough space to host it or if I have to start a new page.
Anybody know how to get it?
Right now I print the table in this way

pdf.table items

isn't there anything like

height_of(pdf.table items)

thank you
--
while ( ! ( succeed = try() ) );

Mattia Lipreri

Henrik Nyh

unread,
Oct 27, 2009, 7:11:53 PM10/27/09
to prawn...@googlegroups.com
On Tue, Oct 27, 2009 at 22:58, mattia lipreri <mattia....@gmail.com> wrote:
> Hi,
> I need to know the table height before to print it beacuse I have to decide
> if my page has enough space to host it or if I have to start a new page.
> Anybody know how to get it?

I believe there should be a method like

group do
# stuff to keep on one page
end

in "edge" Prawn (on GitHub). Haven't used it myself yet.

Brad Ediger

unread,
Oct 27, 2009, 7:17:04 PM10/27/09
to prawn...@googlegroups.com
Yep, that's a nice high-level API if you want to group the whole table onto the current/next page. It's not going to give you a lot of flexibility beyond that, though.

We have a few discussions ongoing about how to best refactor prawn-layout so that the sort of height calculation you're talking about would be easier. I have some work toward that on my bradediger/prawn-layout@nested-tables fork, but it's definitely not ready for prime time. I only mention it in case you may have an interest in how it would be done.

For now, I hope group() is close enough to what you need.

Brad


Bryce Mecum

unread,
Oct 28, 2009, 1:22:54 AM10/28/09
to prawn...@googlegroups.com
I have no idea if I'm doing something I shouldn't but I figured out
table row height using font.ascender, font.descender and
font.line_gap. This height multiplied by the number of rows in your
table will give you your table's height.

http://gist.github.com/220265

row_height = font.ascender - font.descender + font.line_gap

I haven't read up on how Prawn handles all of the moving parts here so
if I'm up to no good someone should let me know.

Brad Ediger

unread,
Oct 28, 2009, 1:26:53 AM10/28/09
to prawn...@googlegroups.com
On Wed, Oct 28, 2009 at 12:22 AM, Bryce Mecum <petr...@gmail.com> wrote:

I have no idea if I'm doing something I shouldn't but I figured out
table row height using font.ascender, font.descender and
font.line_gap. This height multiplied by the number of rows in your
table will give you your table's height.

http://gist.github.com/220265

That's only going to work if you have single-line table rows. prawn-layout already has the machinery to calculate this accurately; it's just not exposed in a friendly way. One of the objectives of our refactoring is to try to make stuff like this easier. 

Brad

Bryce Mecum

unread,
Oct 28, 2009, 1:33:57 AM10/28/09
to prawn...@googlegroups.com
It works fine to calculate multi-line rows. Just multiply by two.

What's the machinery in prawn/layout I can use instead?

Brad Ediger

unread,
Oct 28, 2009, 1:41:37 AM10/28/09
to prawn...@googlegroups.com
On Wed, Oct 28, 2009 at 12:33 AM, Bryce Mecum <petr...@gmail.com> wrote:

It works fine to calculate multi-line rows. Just multiply by two.

What I mean is that then the userland code has to know how many lines there are in the table, not just how many rows. (A "row" can have multiple lines of text.) I'm not at all disputing that this works for you, just saying that there's probably a more elegant solution coming down the line.
 
What's the machinery in prawn/layout I can use instead?

Like I said, there's really nothing publicly exposed yet. I did some refactoring as part of my work on nested tables that I haven't merged into master yet, the gist of which is that we can calculate all of the cell metrics prior to rendering a table. It's basically a structured version of what you were doing above. Simply, I just split generate_table() out into the parts that don't render (the cell_blocks method) and the parts that do (the new generate_table). This makes the Table#height method super simple:


You are of course welcome to use this, expand on it, or throw it away. But I make no guarantees about whether or not this is going to be the way prawn-layout does things in the future. That's still under discussion.

Brad

Bryce Mecum

unread,
Oct 28, 2009, 1:47:04 AM10/28/09
to prawn...@googlegroups.com
Thanks for the run-down. That refactor looks like it'll be useful.

mattia lipreri

unread,
Oct 28, 2009, 3:42:27 AM10/28/09
to prawn...@googlegroups.com
Thank you guys for the inspiration, so there ins't a way to do it yet.
If I'll use this

group do
  # stuff to keep on one page
end

how can I check the group height and render the group just if its height is lesser of page height?
--

while ( ! ( succeed = try() ) );

Mattia Lipreri


Gregory Brown

unread,
Oct 28, 2009, 10:33:32 AM10/28/09
to prawn...@googlegroups.com
On Wed, Oct 28, 2009 at 3:42 AM, mattia lipreri
<mattia....@gmail.com> wrote:
> Thank you guys for the inspiration, so there ins't a way to do it yet.
> If I'll use this
> group do
>   # stuff to keep on one page
> end
> how can I check the group height and render the group just if its height is
> lesser of page height?

That's what group does for you. you don't need to worry about it.

-greg

Reply all
Reply to author
Forward
0 new messages