'Wicked-PDF not showing images, 'wicked_pdf_image_tag' undefined

I want to generate a PDF with our department logo in it. When I try to use the WickedPdf class in my controller (using the method described at https://github.com/mileszs/wicked_pdf):

def some_action
  image_tag_string = image_tag('logo.jpg')
  pdf = WickedPdf.new.pdf_from_string(image_tag_string)

  save_path = Rails.root.join('testpdfs','logotest.pdf')
  File.open(save_path, 'wb') do |file|
    file << pdf
  end
end

...the application saves the PDF to the target directory, but it has a blue-and-white '?' mark where the image should be.

If I do this instead:

  image_tag_string = wicked_pdf_image_tag('logo.jpg')
  pdf = WickedPdf.new.pdf_from_string(image_tag_string)

I get the following error:

NoMethodError:
   undefined method `wicked_pdf_image_tag' for #<...

It would appear that my Rails app is also missing / not linking to a helper file belonging to the wicked-pdf gem.

Answers to similar questions on StackOverflow recommend writing a custom "image-tag" helper to locate the image or installing wkhtmltopdf. For me, image-tag shows the logo just fine when placed in a View (whatever.html.erb). "logo.jpg" is already located in both the asset pipeline and #{RailsRoot}/public/images. Finally, I am using wkhtmltopdf 0.9.9, wicked-pdf 0.11.0, and rails 4 on Ubuntu 14.04.

In a nutshell - what am I doing wrong that causes WickedPDF to fail to render the image?



Solution 1:[1]

Use the wicked_pdf_image_tag helper in your view and reference the image with asset_url if your image is in public/images or use asset_pack_url if the image is in public/packs/media/images

<%= wicked_pdf_image_tag asset_url('/images/footer_logo.png') %>

or

<%= wicked_pdf_image_tag asset_pack_url('media/images/footer_logo.png') %>

Solution 2:[2]

I converted image url that 'http' from 'https'. Than worked.

Heroku-18
Rails 4.2
wicked_pdf (1.1.0)
wkhtmltopdf-binary (0.12.4)

Solution 3:[3]

In my case, I am using carrierwave, the solution was taken from this post

<img src="<%= root_url + "/" +file.service_url %>">

This worked on rails 5.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akaspick
Solution 2 Murat
Solution 3 Shuaib Zahda