Shruthi R
Shruthi R

Reputation: 1923

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?

Upvotes: 1

Views: 1748

Answers (2)

apr
apr

Reputation: 696

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') %>

Upvotes: 2

Sonam Shah
Sonam Shah

Reputation: 41

I have worked on the similar functionality but using PDFKit gem. But I think rendering logic will be almost similar.

Below is the code where I rendered my partial in controller

def print_batch
  batch = Batch.find(params[:id])
  respond_to do |format|
    format.pdf {
      html = render_to_string("_batch",:formats => [:html], layout: false , locals: { batch: batch })
      Rails.logger.debug(html.inspect)
      kit = PDFKit.new(html)
      send_data(kit.to_pdf, :filename => "file_name_#{batch.id}.pdf", :type => 'application/pdf') and return
    }
    format.html
    format.json { render json: {id: batch.id, processed: batch.processed?} }
  end     

end

In _batch.html.haml. You can see I have used person.s3_logo for rendering the pdf image.

  - logo_image_pdf = person.logo.present? ? person.s3_logo : default_credit_logo 
  - logo_image_html = person.logo.present? ? image_path(Person.logo.thumb('100x100').url) : image_path('default_credit_logo.png')
  - logo_image = params[:format] == 'pdf' ? logo_image_pdf : logo_image_html

  .bucks           
    %img.logo{src: logo_image }

In Person.rb model. Anyway we cannot directly render image in PDF file directly from s3. So any gem will first download it in /tmp folder and render it. This is how I have done it in my model file.

  def s3_logo
    file = open(self.logo.remote_url)
    file.path if file
  end

Upvotes: 2

Related Questions