mileszs / wicked_pdf

PDF generator (from HTML) plugin for Ruby on Rails
http://www.mileszs.com/wicked-pdf-plugin
MIT License
3.54k stars 646 forks source link

rendering pdf file from the model ignores images(uploaded with Paperclip) #338

Open bobbyworx opened 10 years ago

bobbyworx commented 10 years ago

I am rendering the file from inside the model. I actually want to render it through Sidekiq but it's the same result. Here is my code so far:

model:

def pdf_rendering(offer)
  view = ActionView::Base.new(ActionController::Base.view_paths.first, {})
  view.extend(ApplicationHelper)
  view.extend(Rails.application.routes.url_helpers)
  pdf = WickedPdf.new.pdf_from_string(
    view.render_to_string(
    :template => "displays/show_offer.pdf.erb",
    :layout => "layouts/templates.html.erb",
    :locals => { :@offer => offer }),
    :margin => { :top => 28, :bottom => 10 },
    :print_media_type => false,
    :page_size => "A4",
    :disable_smart_shrinking => true,
    :header => { :html => { :template => 'displays/offer_header.pdf.erb' }, :spacing => 0 },
    :footer => { :center => '[page] of [topage]' },
  )
  save_path = Rails.root.join('public', 'offer.pdf')
  File.open(save_path, 'wb') do |file|
    file << pdf
  end
  save_path
end

controller:

def show_offer
  @template = Template.new
  send_file @template.pdf_rendering(@offer), :disposition => 'inline'
end

In templates layout:

<%= stylesheet_link_tag "templates", media: "all" %>
<%= wicked_pdf_stylesheet_link_tag "templates" -%>

If I specify a wicked_pdf_image_tag:

<%#= wicked_pdf_image_tag 'stamp.png' %>

it gives me 'undefined method `pathname' for nil:NilClass'

When I'm rendering the pdf file from the controller, it all works fine. How can I correct this? Is it a matter of the way wicked_pdf is handling assets? Thanks!

unixmonkey commented 10 years ago

This looks to be related to #41

The asset helpers mostly deal with stuff in the asset pipeline, since these are paperclip images, they are likely elsewhere on the filesystem. I'm not sure how the pipeline sorts out the difference between paperclip images and normal assets.

Please let me know if that helps or not, and I'll dig in further if necessary.