Prawn PDF: I Need to Generate Nested Tables

prawn PDF: I need to generate nested tables

No support for this exists in released versions, but in the master branch of http://github.com/sandal/prawn you'll find our revamped table support which has nested tables. Take a look at the examples/ dir.

Prawn pdf nested tables - can't find examples

do:

gem which prawn

that would give you location of the prawn gem. It includes the examples dir. You can find table/bill.rb there.

table generation issue in pdf generation using prawn

Have you tried using WickedPDF. It's a PDF generation tool, but you write the HTML instead of learning new syntax etc. It also has the added bonus of being able to use Webkit syntax so html5 is consistant throughout

Prawn PDF table mixed formats

Prawn::Document.generate("test.pdf") do |pdf|
table_data = [[Prawn::Table::Cell::Text.new( pdf, [0,0], :content => "<b>1. Row example text</b> \n\nExample Text Not Bolded", :inline_format => true), "433"],
[Prawn::Table::Cell::Text.new( pdf, [0,0], :content => "<b>2. Row example text</b>", :inline_format => true), "2343"],
[Prawn::Table::Cell::Text.new( pdf, [0,0], :content => "<b>3. Row example text</b>", :inline_format => true), "342"],
[Prawn::Table::Cell::Text.new( pdf, [0,0], :content => "<b>4. Row example text</b>", :inline_format => true), "36"]]

pdf.table(table_data,:width => 500)
end

You can also do

Prawn::Document.generate("test.pdf") do |pdf|
table_data = [["<b>1. Row example text</b>\n\nExample Text Not Bolded", "<b>433<b>"],
["<b>2. Row example text</b>", "<b>2343</b>"],
["<i>3. Row example text</i>", "<i>342</i>"],
["<b>4. Row example text</b>", "<sub>36</sub>"]]

pdf.table(table_data, :width => 500, :cell_style => { :inline_format => true })
end

Prawn's inline_format supports <b>, <i>, <u>, <strikethrough>, <sub>, <sup>, <font>, <color> and <link>

How do I format and style a sub table in Prawn?

OK, here's how it was fixed. I needed to use "make_table" and apply the formatting there like so:

  def line_items
move_down 15
data = line_item_rows

table(data) do
row(0).font_style = :bold
columns(0).width = 160
columns(1).width = 300
columns(2).align = :right
columns(2).valign = :bottom
row(0).columns(2).valign = :top
row(0).columns(2).align = :left
self.header = true
end
end

def line_item_rows
[["Description", "Items" ,"Price ex GST"]] +
@line_items.map do |item|
[item.description,
sub_items(item),
price(item.charge_ex_gst)]
end +
[["","Grand Total", price(@project.charge_ex_gst)]]
end

def sub_items(item)
sub_data = sub_item_rows(item)
make_table(sub_data) do
columns(0).width = 200
columns(1).width = 100
columns(1).align = :right
#columns(0).borders = []
end
end

def sub_item_rows(item)
item.sub_items.map do |sub_item|
["#{sub_item.quantity} x #{sub_item.name}", price(sub_item.total_charge_ex_gst)]
end +
[["","Total"]]
end

Prawn - Links inside table cells

You can specify inline_format for the cell and create the link yourself:

cell_email = pdf.make_cell(
:content => "<link href='mailto:#{booking.user_email}'>#{booking.user_email}</link>",
:inline_format => true
)

You can specify inline_format for the whole table, too:

table data, :cell_style => { :inline_format => true }

Prawn's inline_format supports <b>, <i>, <u>, <strikethrough>, <sub>, <sup>, <font>, <color> and <link>.



Related Topics



Leave a reply



Submit