I'm trying to generate a pdf file from inside a sidekiq worker. Although I am making a new instance of ActionView and including AppHelper and Routes, I still don't have any styles inside the pdf (no layout is considered).
Here's the code:
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
controller method:
def download
OfferWorker.perform_async(@offer.offer_token)
redirect_to root_path
end
What am I doing wrong?