Rails 4 - Wicked pdf文件通过电子邮件发送

时间:2016-08-09 08:52:44

标签: ruby-on-rails ruby email pdf

在rails 4中,我正在使用gem 'wicked_pdf', '1.0.3'gem 'wkhtmltopdf-binary', '0.9.9.3'用于pdf生成。我正在将其下载为文件,

 respond_to do |format|
  format.html
  format.pdf do
    render :pdf => "#{@user.id}_file",
      :disposition => "attachment",
      :save_to_file => Rails.root.join('public/download_pdfs', "#{@user.id}_file.pdf"),
      :layout => false
  end
end

在这里,我通过在控制器Email.deliver_sample_file_mail(@user, "#{Rails.root}/public/download_pdfs/#{@user.id}_file.pdf")

中添加此行来通过邮件发送此文件

在邮件中,

def send_invoice_pdf_mail(user, file_path)
  filename = file_path.split("/").last
  attachments["#{filename}"] = File.read(file_path)
  mail(:to =>user.email, :subject => "File has been sent") do |format|
    format.html { render :partial =>"/emails/users/sample", :locals=>{:user=>user}, :layout=>"email"}
  end
end

现在问题是下载文件和电子邮件发送一起发生。

如何避免文件下载功能?我如何生成.pdf文件,以便它只发送到电子邮件ID?

1 个答案:

答案 0 :(得分:1)

您可以保存文件,无需下载

pdf = render_to_string pdf: "#{@user.id}_file", encoding: "UTF-8"

# then save to a file
save_path = Rails.root.join('public/download_pdfs','#{@user.id}_file.pdf')
File.open(save_path, 'wb') do |file|
  file << pdf
end

希望,这会帮助你。

相关问题