Prawn PDF with Rails mailer?

I successfully created an email that sends Kase to create, but now I need to attach the PDF file created by Prawn and Prawno. Basically, when you visit kase, for example application.com/kase/1, you simply add the URL from .pdf ie application.com/kase/1.

I spent a lot of time getting a PDF file to work and see how I wanted, but I cannot figure out how to add a PDF to automatic email, mainly because I cannot figure out how to give it a link because it is automatically generated.

Has anyone managed to get this to work?

Thanks,

Danny

+4
source share
4 answers

I suppose it would be better if you stored the created PDF file somewhere - for caching purposes, etc. But with the current configuration, you can read the generated page with Net::HTTP and attach the 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 
+3
source

You really should think that you are not using Prawnto and subclassing Prawn :: Document to do what you need. Then, both in your controller and in your mail code, it should only be:

MyReport.new.render

See shrimp documentation:

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

+3
source

For newer ones, you really don't need to send the request again when you can:

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

Instead of this:

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

just do this:

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

Not send_data , just render this pdf in your email application as indicated in the first snippet.

In fact, I just wrote Gist on github.

0
source

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
source

Source: https://habr.com/ru/post/1308293/


All Articles