Reputation: 10885
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....
Upvotes: 8
Views: 5367
Reputation: 481
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.
Upvotes: 8
Reputation: 6185
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 ) )
Upvotes: 3
Reputation: 18784
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.
Upvotes: 2