如何在Ruby on Rails中将pdf文件转换为二进制文件

时间:2013-02-06 10:29:32

标签: ruby-on-rails

我使用pdf_forms gem填写pdf,现在我想将该pdf转换为二进制文件,并希望通过电子邮件发送。

2 个答案:

答案 0 :(得分:1)

您不需要将pdf转换为二进制文件以将其附加到电子邮件中。只需在邮件程序的方法中添加此行

即可

attachments["filename.pdf"] = File.read("/path/to/filename.pdf")

答案 1 :(得分:1)

您可能只想将pdf作为附件发送。官方的Rails ActionMailer有一个如何做到这一点的例子

class UserMailer < ActionMailer::Base
  def welcome_email(user)
    @user = user
    @url  = user_url(@user)
    attachments['terms.pdf'] = File.read('/path/terms.pdf')
    mail(:to => user.email,
         :subject => "Please see the Terms and Conditions attached")
  end
end

http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments