开发者

Prawn PDF with Rails mailer?

I have successfully created an email that sends on creation of a Kase, but now I need to attach a PDF that is created on the fly by Prawn and Prawno. Basically when you visit a kase such as application.com/kase/1 you just append the URL with .pdf i.e. application.com/kase/1.

I spent ages getting t开发者_StackOverflowhe PDF to work and look how I wanted, but I can't figure out how to add the PDF to an auto sending email - mainly because I cannot work out how to give it a link as it's auto generated.

Has anyone ever managed to get this to work?

Thanks,

Danny


I suppose it would be better if you store generated pdf somewhere - for caching purposes, etc. But with current configuration, you can read generated page with Net::HTTP and attach response:

require 'net/http'

def your_mailer_method(record)
  #...
  attachment "application/pdf" do |a|
    a.body = Net::HTTP.get('yourdomain.com', "/kase/#{record.id}.pdf")
    a.filename="your_pdf_name.pdf"
  end  
end


You really should consider just not using Prawnto, and creating a subclass of Prawn::Document to do what you need. Then, in both your controller and your mailer code, it should just be:

MyReport.new.render

See the Prawn documentation on this:

http://wiki.github.com/sandal/prawn/using-prawn-in-rails


For the newer ones, you dont really need to send a request again, when you can ::

mail.attachments["invoice.pdf"] = {:mime_type => "application/pdf" , :content => pdf_generator}

Instead of doing this ::

send_data pdf.render , :filename => file_name_here , :type => "application/pdf"

just do this ::

pdf.render , :filename => file_name_here , :type => "application/pdf"

Do not send_data, just render that pdf in your email attachment as mentioned in the first snippet.

In fact, i just wrote a Gist on github.


This code works for me

def send_file(file, subject, text, to_email)
      @subject = subject
      @text = text
      attachments["#{invoice.invoice_number}.pdf"] = file
      from_email = abc@xyz.com      
mail(:to => to_email, :from => from_email, :subject=> subject)
  end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜