PDF::Writer and Ruby on Rails 2.1

June 4, 2008 by Michael

Some days ago, Ruby On Rails 2.1 saw the light of day and as usual, i eagerly updated my Daily Fratze project.

I had some minor problems due to an old version of will_paginate and some major ones with my use of PDF::Writer.

The PDF::Writer library still works very well but the the instructions here (under PDF::Writer (Austin Ziegler) on how the register an “rpdf” template handler don’t apply anymore due to changes in the Rails template system but can easily be fixed:

In environment.rb change

ActionView::Base.register_template_handler 'rpdf', ActionView::PDFRender


ActionView::Template.register_template_handler 'rpdf', ActionView::PDFRender

And also change your ActionView::PDFRender class to:

module ActionView # :nodoc:
  require 'pdf/writer'
  class PDFRender
    PAPER = 'A4'
    include ApplicationHelper
    include ActionView::Helpers::AssetTagHelper
    include ActionView::Helpers::TextHelper      
    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::UrlHelper
    def initialize(action_view)
      @action_view = action_view
    # Render the PDF
    def render(template, local_assigns = {})
      @action_view.controller.headers["Content-Type"] ||= 'application/pdf'
      # Retrieve controller variables
      @action_view.controller.instance_variables.each do |v|
        instance_variable_set(v, @action_view.controller.instance_variable_get(v))
      pdf = ::PDF::Writer.new( :paper => PAPER )
      pdf.compressed = true if RAILS_ENV != 'development'
      eval template.source, nil, "#{@action_view.base_path}/#{@action_view.first_render}.#{@action_view.finder.pick_template_extension(@action_view.first_render)}" 
    def self.compilable?
    def compilable?

And enjoy happy PDF generation 😉


  1. Hugh Kelsey wrote:

    Thanks for this post. Where should I put the changes to the ActionView::PDFRender Class?

    Posted on June 11, 2008 at 3:54 PM | Permalink
  2. Michael wrote:

    Hey Hugh,
    you’re welcome.

    This is the complete PDFRender class.

    I have it in a file “PDFRender.rb” in $APP_HOME/lib and include it in environment.rb via “require ‘PDFRender'”

    Posted on June 11, 2008 at 4:29 PM | Permalink
  3. Graeme wrote:

    Michael – thanks for this.

    Posted on June 18, 2008 at 3:06 PM | Permalink
  4. Michael wrote:

    Graeme: You’re welcome 🙂

    Posted on June 20, 2008 at 7:04 PM | Permalink
  5. teena wrote:

    Thanks for this post
    I am using pdf writer to generate pdf in rails application.
    i need how to create or set headers and footers for all pages in pdf
    Please guide me.


    Posted on October 21, 2008 at 8:39 AM | Permalink
  6. Hugh Kelsey wrote:

    Check out “loose content objects” in http://ruby-pdf.rubyforge.org/.....manual.pdf

    Posted on October 21, 2008 at 4:05 PM | Permalink
  7. Sven Tantau wrote:

    Hi Michael,

    if you are interested in rendering PDFs out of OpenOffice or LibreOffice templates, please take a look at my side project:


    Aside from normal text substitution it offers support for images and lists. (And all the nice stuff like header/footer or clean page breaks that such word processors offer.)
    The API is currently free for low volume usage. I would be very happy to get some feedback.

    Best regards,

    Posted on February 27, 2014 at 2:57 AM | Permalink
One Trackback/Pingback
  1. […] here and used the method named PDF::Writer (Austin Ziegler), you we’re out of luck when Rails 2.1 […]

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *