Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Send dynamicly generated pdf as attachement
11 Posts
Send dynamicly generated pdf as attachement

Austin, Thanks to the start you gave me, I've managed to get this working today:

Here's the code from my model that generates a PDF from a Prawnto view and saves it to a file:

av = ActionView::Base.new(Rails::Configuration.new.view_path)
av.extend ApplicationController.master_helper_module
av.controller = ActionController::Base.new
av.controller.request = ActionController::Request.new({ "SERVER_PROTOCOL" => "", "REQUEST_URI" => "", "action_controller.request.request_parameters" => {:format => :pdf} })
av.controller.response = ActionController::Response.new
av.controller.headers = Rack::Utils::HeaderHash.new
av.controller.prawnto :prawn => { :page_size      => 'A4' } # your Prawto settings here....
pdf = Prawn::Document.new(av.controller.instance_variable_get(:@prawnto_options)[:prawn])
pdf_body = av.render(:file => 'reports/render_me.pdf.prawn', :locals => {:pdf => pdf}, :layout => false )
File.open("MGPtest", 'w') {|f| f.write(pdf_body) }

This is a first draft of a working version, so can probably be cleaned up a litte (or lot!).

Of course, YMMV, and this is a total cludge of the MVC principles - but sometimes you've just got to...

Hope this helps out there.

11 Posts
Login to add your message