{ Josh Rendek }

<3 Ruby & Go
 1class RenderHelper
 2  class << self
 3    def render(assigns, options, request = {})
 4      request = {
 5        "SERVER_PROTOCOL" => "http",
 6        "REQUEST_URI" => "/",
 7        "SERVER_NAME" => "localhost",
 8        "SERVER_PORT" => 80
 9      }.merge(request)
10
11      av = ActionView::Base.new(ActionController::Base.view_paths, assigns)
12
13      av.config = Rails.application.config.action_controller
14      av.extend ApplicationController._helpers
15      av.controller = ActionController::Base.new
16      av.controller.request = ActionController::Request.new(request)
17      av.controller.response = ActionController::Response.new
18      av.controller.headers = Rack::Utils::HeaderHash.new
19
20      av.class_eval do
21        include Rails.application.routes.url_helpers
22      end
23
24      av.render options
25    end
26  end
27end

Usage

1 html_output = RenderHelper.render({:instance_variable1 => "foo",
2                                                :instance_variable2 => "bar"},
3                                                :template => 'view_to/render')

You can then use you’re favorite PDF generator (I use PDFKit) to take the html output and parse it to a PDF.

comments powered by Disqus