Informatika Mihelac
June 19 2006 pdf | rails | ruby

Creating PDF documents with tables in Ruby, Rails

I started to use Ruby FPDF to create PDF documents from our new Rails application MojGost.com. Ruby FPFD like it’s PHP counterpart does not have methods for creating tables. However, there are many examples and scripts available and that allowed me to quickly write small Ruby module which adds few methods for easy table creation.

Fpdf::Table main features:
  • word wraping text in cells, based on width of columns
  • page breaks on rows if table is too long

Download Fpdf::Table or see some examples below.

Example 1. simple table


require 'FPDF'
require 'fpdf/table'

class FPDF
  include Fpdf::Table
end

pdf = FPDF.new
pdf.AddPage
pdf.SetFont('helvetica','',10)
data = [
  ['100', 'lorem ipsum dorem'],
  ['100', 'lorem ipsum dorem'],
  ['100', 'lorem ipsum dorem'],
]
pdf.table(data)
pdf.Output('test_fpdf_table_1.pdf')

View Example 1 PDF

Example 2. width, word-wrap, aligment, page break


require 'FPDF'
require 'fpdf/table'

class FPDF
  include Fpdf::Table
end

pdf = FPDF.new
pdf.AddPage
pdf.SetFont('helvetica','',10)

data = []
30.times { |i| data << [i.to_s, '-', 'Lorem ipsum dolor sit' * 10] }

columns = [
  {:title => '#1', :aligment => 'R', :width => 20},  
  {:title => '#2', :width => 20},
  {:title => 'Text'}  
]

pdf.table(data, columns)
pdf.Output('test_fpdf_table_2.pdf')

View Example 2 PDF

comments feed

11 comments

  1. # On August 22, 2006 at 22:08 PM, Tbone607 said:
    Hi, I had a question in regards to getting everything working. Where does the following code belong? class FPDF include Fpdf::Table end Email me back at secretservice_007@Hotmail.com if you can. Thx.
  2. # On August 22, 2006 at 22:08 PM, Tbone607 said:
    Sorry, it turns out I am getting adobe acrobat reader error that says this type is not supported or the file has been damaged. Any ideas? Thx again!
  3. # On August 24, 2006 at 03:08 AM, yuka Izutsu said:
    ymsgndbfsvc
  4. # On August 24, 2006 at 03:08 AM, fdv said:
    Tbone607, Code block:
    class FPDF
      include Fpdf::Table
    end
    
    includes FPDF class and extend it with table functions.
  5. # On August 24, 2006 at 03:08 AM, fdv said:
    Tbone607, what is size of generated PDF? Did you get first example working?
  6. # On August 25, 2006 at 02:08 AM, Tbone607 said:
    Thx guys. I got everything working perfectly. I spent a bit more time and realized that it was easier than I thought. Great tool and thx again!
  7. # On September 14, 2006 at 22:09 PM, Frank said:
    Can you put all of the code in a controller? Or do you have to break it up into pieces?
  8. # On September 15, 2006 at 03:09 AM, Bojan said:
    Sure, you can use code without Rails as well.
  9. # On June 28, 2007 at 11:06 AM, daduke said:

    Hi, I’m interested in your fpdf::table stuff. Unfortunately, all links on this page 404 – I cannot get the files.

    thanks, -daduke

  10. # On June 29, 2007 at 09:06 AM, bmihelac said:

    daduke, you should be able to download files now.

    best, Bojan

  11. # On April 24, 2008 at 18:04 PM, ABES said:

    I think there’s a typo in your coding. In order to set the alignment of a column you say to use:

    :aligment => 'R'

    However, alignment is spelled ‘alignment’ not ‘aligment’.

    Other than that, thanks for this!

    Cheers, ABES

Speak your mind:

(Required)

(Required)


(You may use textile in your comments.)

About

I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in Informatika Mihelac.

Contact:

RSS feed RSS Feed

Tags