1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# frozen_string_literal: true class PdfGenerator def content PDFKit.new(html).to_pdf end def html() template = OfflineTemplate.new template.render_to_string( template: "some/path/template.html.erb", locals: { ... }, layout: false ) end end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class OfflineTemplate < AbstractController::Base include AbstractController::Logger include AbstractController::Rendering include ActionView::Layouts include AbstractController::Helpers include AbstractController::Translation include AbstractController::AssetPaths include ActionDispatch::Routing::UrlFor include Rails.application.routes.url_helpers helper ApplicationHelper helper_method :protect_against_forgery? # configure the different paths correctly def initialize(*args) super() lookup_context.view_paths = Rails.root.join('app', 'views') config.javascripts_dir = Rails.root.join('public', 'javascripts') config.stylesheets_dir = Rails.root.join('public', 'stylesheets') config.assets_dir = Rails.root.join('public') end # we are not in a browser, no need for this def protect_against_forgery? false end # so that your flash calls still work def flash {} end def params {} end # same asset host as the controllers self.asset_host = ActionController::Base.asset_host end |
source: https://gist.github.com/juggy/977181