Syntax for complex, individually formatted, multiline, colspan table headers with variables

164 views
Skip to first unread message

Peter

unread,
Dec 2, 2009, 1:04:29 AM12/2/09
to Prawn
I am building a Rails app that features a complex one-page nine-
column .pdf financial table that I am creating with Prawn and Prawnto.

I want to have four two-line (i.e., two-row) colspan headers (three
two-column headers, plus one three-column header). These headers need
to display some variables, like formatted dates. I also want to be
able to format each header individually (i.e., font_size, font_style,
background color for the span, bottom border, etc.)

I can’t seem to get the syntax right. Yes, I did try to RTFM. The
Prawn documentation is not clear to me and I have tried many
combinations with no luck.

Basic table setup (without ‘:header =>’ stuff) looks like:

pdf.table table_data, :font_size => 9,
:padding => 3

I would appreciate seeing an example of how to make up these complex
individual header formats. The Cracklabs website is down, so I can’t
look at the demos there. Darn!

Gregory Brown

unread,
Dec 2, 2009, 1:14:14 AM12/2/09
to prawn...@googlegroups.com
On Wed, Dec 2, 2009 at 1:04 AM, Peter <peter...@bloombusiness.com> wrote:

> I can’t seem to get the syntax right.  Yes, I did try to RTFM. The
> Prawn documentation is not clear to me and I have tried many
> combinations with no luck.

I suggest you try iText. When you install Prawn it tells you that it
expects you to read source. If you're not comfortable with that, go
with a post-production PDF library.

Peter Seebach

unread,
Dec 2, 2009, 1:12:00 AM12/2/09
to prawn...@googlegroups.com
In message <cd630086-7c9f-4d3a...@j4g2000yqe.googlegroups.com>,
Peter writes:
>I want to have four two-line (i.e., two-row) colspan headers (three
>two-column headers, plus one three-column header). These headers need
>to display some variables, like formatted dates. I also want to be
>able to format each header individually (i.e., font_size, font_style,
>background color for the span, bottom border, etc.)

I tried to use tables a bit and gave up because they didn't work with
the <b>...</b> stuff, but maybe I should give it another look.

>I would appreciate seeing an example of how to make up these complex
>individual header formats. The Cracklabs website is down, so I can't
>look at the demos there. Darn!

I don't know about that. When I was doing this, I was passing in arrays
of hashes for each row of the table, and the hashes were things like

{ :text => "foo", :font_size => 9 }

-s

Henrik Nyh

unread,
Dec 2, 2009, 3:35:07 AM12/2/09
to prawn...@googlegroups.com
On Wed, Dec 2, 2009 at 07:04, Peter <peter...@bloombusiness.com> wrote:
> I am building a Rails app that features a complex one-page nine-
> column .pdf financial table that I am creating with Prawn and Prawnto.
>
> I want to have four two-line (i.e., two-row) colspan headers (three
> two-column headers, plus one three-column header).  These headers need
> to display some variables, like formatted dates.  I also want to be
> able to format each header individually (i.e., font_size, font_style,
> background color for the span, bottom border, etc.)

I did something like this. Since I wanted two rows of headers, I
didn't specify any :headers, only used table cells for everything.
That means they won't repeat for every new page like :headers do.

Here's an example: http://pastie.textmate.org/private/mpbzwaae4ltimxogmjxekg
Message has been deleted
Message has been deleted

Peter

unread,
Dec 2, 2009, 1:44:14 PM12/2/09
to Prawn
Thanks Henrik! That worked great! I didn't think of the hash
merge...duh!

Only change from your pastie example is that in my Rails app the
options variable must begin
with lower case (i.e., not be a constant), otherwise I get a 'dynamic
constant assignment (SyntaxError)'. so...(simplified example)...

TOP_HEADER_OPTIONS = { :background_color => 'CC0000',
:font_style => :bold,
:colspan => 2 }

...should be...

top_header_options = { :background_color => 'CC0000',
:font_style => :bold,
:colspan => 2 }

Here's the full pastie, just in case it becomes unavailable later...

<<
require "rubygems"
require "prawn"
require "prawn/layout"

TOP_HEADER_OPTIONS = {
:background_color => 'CC0000',
:align => :center,
:font_style => :bold,
:colspan => 2
}

BOTTOM_HEADER_OPTIONS = {
:background_color => '0000CC',
:align => :center,
:font_style => :italic
}

tbody = [
[{ :text => "A" }.merge(TOP_HEADER_OPTIONS), { :text => "B" }.merge
(TOP_HEADER_OPTIONS)],
[{ :text => "A1" }.merge(BOTTOM_HEADER_OPTIONS), { :text =>
"A2" }.merge(BOTTOM_HEADER_OPTIONS), { :text => "B1" }.merge
(BOTTOM_HEADER_OPTIONS), { :text => "B2" }.merge
(BOTTOM_HEADER_OPTIONS)],
["x", "y", "z", "w"]
]

Prawn::Document.new do

table tbody
render_file '/tmp/table.pdf'

end

`open /tmp/table.pdf` # OS X
>>

On Dec 2, 12:35 am, Henrik Nyh <hen...@nyh.se> wrote:
Reply all
Reply to author
Forward
0 new messages