wicked_pdf_image_tag given undefined pathname for image
Asked Answered
S

3

8

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....

Shipyard answered 29/8, 2012 at 14:35 Comment(1)
I haven't verified, but I suspect it's intended to work with images in /assets/images rather than files in /publicLanti
E
8

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.

Entrepreneur answered 20/12, 2012 at 7:10 Comment(0)
T
3

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 ) )
Tactic answered 23/1, 2013 at 9:44 Comment(0)
C
2

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.

Chilli answered 6/9, 2012 at 12:35 Comment(2)
give me error when i include this in my application helper def wicked_pdf_image_tag(img, options={}) image_tag "file://#{WickedPdfHelper.root_path.join('public', 'images', img)}", options end the error is ActionView::Template::Error (undefined method `root_path' for WickedPdfHelper:Module):Shipyard
Try "file://#{Rails.root.join('public','images',img)}" to skip the asset pipeline altogether.Chilli

© 2022 - 2024 — McMap. All rights reserved.