使用sidekiq worker生成pdf文件

时间:2014-09-30 06:53:21

标签: ruby-on-rails sidekiq wicked-pdf

我正在尝试从sidekiq worker中生成一个pdf文件。 虽然我正在创建一个ActionView的新实例,包括AppHelper和Routes,但我仍然没有pdf中的任何样式(不考虑布局)。

以下是代码:

offer_worker.rb:

def perform(offer_token)
  @offer = Offer.unscoped.find_by_offer_token!(offer_token)
  view = html = ActionView::Base.new(Rails.root.join('app/views'))
  view.class.include ApplicationHelper
  view.class.include Rails.application.routes.url_helpers

  rendered = view.render(
    :template => "displays/offer_template_one.html.erb",
    :layout => "layouts/templates.html.erb",
    :locals => { :@offer => offer}
  )
  pdf = WickedPdf.new.pdf_from_string(rendered)
  save_path = Rails.root.join('public', 'offer.pdf')
  File.open(save_path, 'wb') do |file|
    file << pdf
  end
    save_path
end

控制器方法:

def download
  OfferWorker.perform_async(@offer.offer_token)
  redirect_to root_path
end

我做错了什么?

0 个答案:

没有答案