For those of you who, like me, several times ago, came here to find a way to make PDF :: Writer work in rails 3 application, perhaps redirecting an existing Rails 2.x application, I finally reached PDF-writer , version 1.1.8, to work in the rails 3.2 application.
In the gemfile :
gem 'pdf-writer'
I have this in config / initializers / pdfwriter_template.rb
module ActionView module Template::Handlers class PDFWriter def call(template) require_engine "pdf = ::PDF::Writer.new( :paper => 'A4' );" + "pdf.compressed = true if Rails.env.to_s == 'production';" + template.source + ";pdf.render" end protected def require_engine @required ||= begin require 'pdf/writer' require 'pdf/simpletable' true end end end end end ActiveSupport.on_load(:action_view) do ActionView::Template.register_template_handler :rpdf, ActionView::Template::Handlers::PDFWriter.new end
then in the controller action I can write something like:
def pdf_test @record = Record.find(params[:id]) fileout = render_to_string :layout=>false send_data(fileout, :type => "application/pdf", :filename => 'test_record.pdf') end
having the pdf_test.rpdf template file :
pdf.text "Hello from record number #{@record.number}!!"
!!!!! CAUTION: This is not the most efficient way to render PDF in Rails !!!!!! Use PDFKit or Shrimp Instead !!!!!
source share