# PDF::Writer Simpletable Iteration

**URL:** <https://www.ruby-forum.com/t/pdf-writer-simpletable-iteration/54647>\
**Category:** Rails\
**Created:** [March 10, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/pdf-writer-simpletable-iteration/54647 "2006-03-10T23:44:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [March 10, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/pdf-writer-simpletable-iteration/54647/1 "2006-03-10T23:44:17Z")

</div>

Hello All:

I have been working with [Austin  
Ziegler’s PDF::Writer](http://ruby-pdf.rubyforge.org/pdf-writer/index.html) 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

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [March 11, 2006, 12:20am UTC](https://www.ruby-forum.com/t/pdf-writer-simpletable-iteration/54647/2 "2006-03-11T00:20:39Z")

</div>

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 =)

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [March 11, 2006, 12:21am UTC](https://www.ruby-forum.com/t/pdf-writer-simpletable-iteration/54647/3 "2006-03-11T00:21:57Z")

</div>

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 =)

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [March 12, 2006, 9:24am UTC](https://www.ruby-forum.com/t/pdf-writer-simpletable-iteration/54647/4 "2006-03-12T09:24:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [March 11, 2006, 12:34am UTC](https://www.ruby-forum.com/t/pdf-writer-simpletable-iteration/54647/5 "2006-03-11T00:34:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [January 18, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/pdf-writer-simpletable-iteration/54647/6 "2007-01-18T15:21:14Z")

</div>

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
