Hello All: I have been working with <a href="http://ruby-pdf.rubyforge.org/pdf-writer/index.html">Austin Ziegler''s PDF::Writer</a> 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 -- Posted via http://www.ruby-forum.com/.
David Hughes 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 =) -- Posted via http://www.ruby-forum.com/.
David Hughes 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 =) -- Posted via http://www.ruby-forum.com/.
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)]} <snip other code> @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 On 10-Mar-06, at 5:44 PM, David Hughes wrote:> Hello All: > > I have been working with <a > href="http://ruby-pdf.rubyforge.org/pdf-writer/index.html">Austin > Ziegler''s PDF::Writer</a> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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! -- Posted via http://www.ruby-forum.com/.
Jodi Showers 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)]} > > <snip other code> > > @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, > JodiMrs...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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---