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')
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')
I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in
13 comments
Hi, I’m interested in your fpdf::table stuff. Unfortunately, all links on this page 404 – I cannot get the files.
thanks, -daduke
daduke, you should be able to download files now.
best, Bojan
I think there’s a typo in your coding. In order to set the alignment of a column you say to use:
However, alignment is spelled ‘alignment’ not ‘aligment’.
Other than that, thanks for this!
Cheers, ABES
Hi please, I don’t execute this code, could you send me the whole project? I don’t know as making with the FPDF lib. Please cecci666@gmail.com I don’t speak english very well
@Bojan Mihelac
I appreciate effort taken.
But I think, some more detail clarification needed.
Also Source file also included, Bcoz it lacs many more implementation details.
Thanks !
Speak your mind: