Read my latest article: 8 things I look for in a Ruby on Rails app (posted Thu, 06 Jul 2017 17:59:00 GMT)

Ruby FPDF on Ruby on Rails

Posted by Sat, 04 Jun 2005 14:24:00 GMT

3 comments Latest by sg Mon, 21 Aug 2006 09:57:06 GMT

I have been tinkering with Ruby FPDF for a client all night. I found the examples for it to lack in some real-world examples, so I have taken the example from the RubyOnRails wiki and added a bit more to it. I have added things like an image, links and made a generic letter template. (just an example). I didn’t get into the header/footer functions yet, may do that later.

If you are not sure how to use Ruby FPDF, check out the FPDF documentation.

Just download Ruby FPDF and unpack the archive in your libs directory.

To get Railst to load fpdf, I added this to environment.rb:

).map { |dir| "#{RAILS_ROOT}/#{dir}" }.select { |dir| }
At the top of my controller that uses this lib, I added:
require 'fpdf'

When I browse to this controller/method, I got this PDF to generate with this code.

  def pdf
    send_data gen_pdf, :filename => "robbyonrails-fpdf-test.pdf", :type => "application/pdf"

  def gen_pdf

    d =


    pdf.Cell(0,6, "PLANET ARGON", 0,1,'R')
    pdf.Cell(0,6, "2802 NE 57th Ave",0,1,'R')
    pdf.Cell(0,6, "Portland, OR 97213",0,0,'R')

    pdf.Write(5, "Jane Doe
123 ABC Street
Gilroy, CA 95020


Dear Jane Doe,

I just wanted to say...

Epsum factorial non deposit quid pro quo hic escorol. Olypian quarrels et gorilla congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Defacto lingo est igpay atinlay. Marquee selectus non provisio incongruous feline nolo contendre. Gratuitous octopus niacin, sodium glutimate. Quote meon an estimate et non interruptus stadium. Sic tempus fugit esperanto hiccup estrogen. Glorious


Robby Russell
    pdf.Cell(0,6, "PLANET ARGON", 0,1,'L',0,'')

Once again, the output. (click to view PDF)

It’s nothing special, but it’s just an example of how you can add a bit more than ‘Hello World’ to the top of a PDF. I’m still working on figuring out all the x/y stuff. Maybe I will post a better tutorial in the future with headers and footers.

Until then… have fun!