Wednesday, 29 April 2015

render html in pdf in and then sent it as atachment in mail ruby on rails

Add gems:- 

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'

Run bundle install

In Controller:- 

def generate_pdf
          html = render_to_string(:action => :pdf_file, :layout => false)

      pdf = WickedPdf.new.pdf_from_string(html)
      save_path = Rails.root.join('public','users.pdf')
      File.open(save_path, 'wb') do |file| 
        file << pdf
      end

      @mail = ReminderMailer.share_email(params).deliver

      flash[:notice] = 'Email sent successfully.'
      render 'show', layout: 'simple' if !mobile_device?
  end

No comments:

Post a Comment