PDF::Writer and Ruby on Rails 2.2.2
If you followed the instructions here and used the method named PDF::Writer (Austin Ziegler), you we’re out of luck when Rails 2.1 appeared.
With Rails 2.2.2 once again the rendering mechanism seems to have changed big time and my previous post on how to make the pdf/writer gem work with a custom template handler doesn’t work anymore.
With the help of Josh Peek i was able to fix this. He gave me the following code to enable a rpdf template handler with pdf-writer:
module ActionView # :nodoc: require 'pdf/writer' class PDFRender < ActionView::TemplateHandler PAPER = 'A4' include ApplicationHelper include ActionView::Helpers::TranslationHelper include ActionView::Helpers::AssetTagHelper include ActionView::Helpers::TextHelper include ActionView::Helpers::TagHelper include ActionView::Helpers::UrlHelper def self.call(template) "ActionView::PDFRender.new(self).render(template, local_assigns)" end def initialize(action_view) @action_view = action_view end # 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)) end pdf = ::PDF::Writer.new( :paper => PAPER ) pdf.compressed = true if RAILS_ENV != 'development' eval template.source, nil, '' pdf.render end end end ActionView::Template.register_template_handler 'rpdf', ActionView::PDFRender |
Just drop this under config/initializers and you’re fine.