Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wicked_pdf_image_tag given undefined pathname for image

Hi i am using wicked_pdf for generating images after i save image when i generate pdf and used this tag for display image like this

  <%= wicked_pdf_image_tag(@image.snap.url(:original)) unless @image.blank? %>

it give me this unknown error

      ActionView::Template::Error (undefined method `pathname' for nil:NilClass):

while puts @image.inspect give me right path below

       "/system/snaps/7/original/flake.jpg"

Can any one help

Thanks....

like image 677
Kashiftufail Avatar asked Aug 29 '12 14:08

Kashiftufail


3 Answers

I too faced the same problem. In app/helpers/application_helper.rb file write the following lines of code:

module ApplicationHelper
  def wicked_pdf_image_tag_for_public(img, options={})
    if img[0] == "/"
      new_image = img.slice(1..-1)
      image_tag "file://#{Rails.root.join('public', new_image)}", options
    else
      image_tag "file://#{Rails.root.join('public', 'images', img)}", options
    end
  end
end


And in view page where you want to add an image write:

<%= wicked_pdf_image_tag_for_public @logo.url(:small) %>

@logo.url gives the image path in public stored by paperclip.

I hope that will help.

like image 176
rupsray Avatar answered Sep 21 '22 23:09

rupsray


I had the same problem, turns out that the WicketPdf helper is for assets served by your app rather than uploaded attachments.. The only thing you need to do for these is add the host.. You can do this by doing a URI.join on the image-url combined with the request.url..

Here's an example which works in PDF generation:

= image_tag( URI.join( request.url, model.attachment.url ) )
like image 22
Tim Baas Avatar answered Sep 22 '22 23:09

Tim Baas


It looks like this error springs from this line in wicked_pdf:

https://github.com/mileszs/wicked_pdf/blob/master/lib/wicked_pdf_helper.rb#L59

find_asset(source) is nil for some reason for you. Maybe you need to get something together with your asset pipeline. I'll admit I don't have a lot of experience with it yet.

But, you can see that wicked_pdf_image_tag is actually a very simple helper:

https://github.com/mileszs/wicked_pdf/blob/master/lib/wicked_pdf_helper.rb#L14

You can easily just write your own and throw it in application_helper.rb if the included one isn't working for you. The important thing it is doing is using a file:// path for your image, because wkhtmltopdf will render a whole lot faster if it doesn't have to invoke network traffic while generating your PDF.

like image 36
Unixmonkey Avatar answered Sep 19 '22 23:09

Unixmonkey