Shruthi R Shruthi R - 4 months ago 25
Ruby Question

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

In rails 4, I am using

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

Used Gems are,

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


In initializers,

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


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
}
end


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
s3
image in pdf file?

Answer

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('//s3.amazonaws.com/bucket_name/image.png') %>
Comments