Hello All:
I have been working with Austin
Ziegler’s PDF::Writer as a means to produce PDF documents from my
Rails app. I am stumped on how to iterate through a collection of items
and output them in a table format. The way I have tried it so far only
returns the last item in the collection - every other variation I have
tried errors out.
All of the documentation I can find only deals with static data,
manually entered in the controller when using the Simpletable class. It
is working, except only the last item in the collection shows in the
generated PDF. Any ideas?
My code follows:
def pdf
@item = Item.find(:all)
_p = PDF::Writer.new
_p.select_font 'Times-Roman'
PDF::SimpleTable.new do |tab|
tab.title = "Item List"
tab.column_order.push(*%w{item_code item_name item_desc
item_cost})
tab.columns["item_code"] =
PDF::SimpleTable::Column.new(“item_code”) { |col| col.heading = “Code” }
tab.columns[“item_name”] =
PDF::SimpleTable::Column.new(“item_name”) { |col| col.heading = “Item” }
tab.columns[“item_desc”] =
PDF::SimpleTable::Column.new(“item_desc”) { |col| col.heading =
“Description” }
tab.columns[“item_cose”] =
PDF::SimpleTable::Column.new(“item_cost”) { |col| col.heading = “Cost” }
tab.orientation = :center
tab.bold_headings = true
@items.each do |item|
@item_data = [
{ "item_code" => item.code,
"item_name" => item.name,
"item_desc" => item.desc,
"item_cost" => item.cost},
]
end
tab.data.replace @data
tab.render_on(_p)
end
send_data _p.render, :filename => "Item_List.pdf", :type =>
“application/pdf”
end
David H. wrote:
def pdf
@item = Item.find(:all)
Sorry everyone…that line should read:
@items = Item.find(:all)
A little typo when I copied it in to the forum post…Cheers =)
David H. wrote:
def pdf
@item = Item.find(:all)
Sorry everyone…that line should read:
@items = Item.find(:all)
A little typo when I copied it in to the forum post…Cheers =)
Thanks Jodi…I was gone for a couple of days…I’ll try it on Monday
and let you know how it works out…Cheers!
Just have a brief moment to help out, this code snippet is from my
own code (obviously! lol).
from Controller:
@work_order_items = WorkOrderItem.find(:all)
render :template => “work_orders/work_order”, :type =>
“rpdf”, :layout => false
from View:
@work_order_items_table = PDF::SimpleTable.new
@work_order_items_table.column_order = [ “Quantity”, “Part #”,
“Description”, “Price”, “Extended”]
@work_order_items.each {|item| @work_order_items_table.data << Hash
[“Part #”, item.products_and_services.part_number,
“Description”, item.description, “Quantity”,item.quantity,
“Price”, number_to_currency(item.price),
“Extended”, number_to_currency(item.sub_total)]}
@work_order_items_table.render_on(pdf) if !
@work_order_items_table.data.empty?
Hope the above helps.
I’ll back online in a couple of hours. Let me know if I can help more.
cheers,
Jodi
Jodi S. wrote:
Just have a brief moment to help out, this code snippet is from my
own code (obviously! lol).
from Controller:
@work_order_items = WorkOrderItem.find(:all)
render :template => “work_orders/work_order”, :type =>
“rpdf”, :layout => false
from View:
@work_order_items_table = PDF::SimpleTable.new
@work_order_items_table.column_order = [ “Quantity”, “Part #”,
“Description”, “Price”, “Extended”]
@work_order_items.each {|item| @work_order_items_table.data << Hash
[“Part #”, item.products_and_services.part_number,
“Description”, item.description, “Quantity”,item.quantity,
“Price”, number_to_currency(item.price),
“Extended”, number_to_currency(item.sub_total)]}
@work_order_items_table.render_on(pdf) if !
@work_order_items_table.data.empty?
Hope the above helps.
I’ll back online in a couple of hours. Let me know if I can help more.
cheers,
Jodi
Mrs…check this
######Cuarta Tabla para ver informacion de muestras
@muestra_data = []
@i = 0
PDF::SimpleTable.new do |table4|
table4.column_order.push(*%w{CANTIDAD DESCRIPCION PRECIO TOTAL})
for @muestra in @factura_proforma.muestra_factura_proforma
@muestra_data[@i] = { "CANTIDAD" => @muestra.cantidad,
"DESCRIPCION" => @muestra.producto_anio.producto.descripcion,
"PRECIO" => @muestra.precio,
"TOTAL" => @muestra.cantidad * @muestra.precio}
@i = @i+1
end
table4.data.replace @muestra_data
table4.width = 550
table4.show_headings = true
table4.show_lines = :none
table4.shade_rows = :none
table4.render_on(pdf)
end
######Fin Cuarta Tabla para ver informacion de muestras