Forum: Ruby on Rails PDF::Writer Simpletable Iteration

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fa06489a97b0efe3c3b7d249982878d3?d=identicon&s=25 David Hughes (divotdave)
on 2006-03-10 23:44
Hello All:

I have been working with <a
href="http://ruby-pdf.rubyforge.org/pdf-writer/index.htm...
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
Fa06489a97b0efe3c3b7d249982878d3?d=identicon&s=25 Guest (Guest)
on 2006-03-11 00:20
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 =)
Fa06489a97b0efe3c3b7d249982878d3?d=identicon&s=25 David Hughes (Guest)
on 2006-03-11 00:21
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 =)
B09a3f6cdc4797532647d2d264b5df49?d=identicon&s=25 Jodi Showers (jshow)
on 2006-03-11 00:34
(Received via mailing list)
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
Fa06489a97b0efe3c3b7d249982878d3?d=identicon&s=25 David Hughes (Guest)
on 2006-03-12 09:24
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!
0115912e6d90a0a1c4728900248ba10f?d=identicon&s=25 Bouchon Team (Guest)
on 2007-01-18 15:21
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,
> 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
This topic is locked and can not be replied to.