Shruthi R Shruthi R - 1 year ago 116
Ruby Question

Rails 4, How to add an s3 image to wicked pdf

In rails 4, I am using

gem for
file download. I have to add an image inside this pdf, right now image is rendering through
in development but in test environment image(s3) is not rendering.

Used Gems are,

gem 'wicked_pdf', '1.0.3'
gem 'wkhtmltopdf-binary', ''

In initializers,

class WickedPdf
wkhtmltopdf_path = Rails.env.production? ? "#{Rails.root}/bin/wkhtmltopdf-amd64" : "#{Rails.root}/bin/wkhtmltopdf-amd64"
:exe_path => wkhtmltopdf_path,
:wkhtmltopdf => wkhtmltopdf_path

In controller,

respond_to do |format|
format.html {
render :pdf => "sample",
:margin => {:top => 10, :bottom => 10, :left => 10, :right => 10},
:orientation => 'Portrait', # default , Landscape,
:no_background => true

In views, I have tried to load through

<%= Rails.env.development? ? wicked_pdf_image_tag("img/logo.png") : wicked_pdf_image_tag("#{Rails.root}/public/assets/img/logo.png") %>

<%= Rails.env.development? ? wicked_pdf_image_tag("img/logo.png") : wicked_pdf_image_tag("#{Rails.root}/assets/img/logo.png") %>

<%= image_tag(ActionController::Base.helpers.asset_path('img/logo.png')) %>

How can I load
image in pdf file?

Answer Source

You can put the image in a s3 bucket and make it public. After that try it like below. If you use the below code no need to use separate syntax for different environments, it will works for all .Hope it works.

<%= wicked_pdf_image_tag('//') %>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download