Rails 3 Render Prawn pdf in ActionMailer

How to make shrimp pdf as an application in ActionMailer? I use delayed_job and don’t understand how I could make a pdf file in the action mailer (and not in the controller). Which format should I use?

+7
source share
3 answers

You just have to say that Prawn displays the PDF in a line and then adds this as an attachment to the email. For more information on attachments, see ActionMailer Docs .

Here is an example:

class ReportPdf def initialize(report) @report = report end def render doc = Prawn::Document.new # Draw some stuff... doc.draw_text @report.title, :at => [100, 100], :size => 32 # Return the PDF, rendered to a string doc.render end end class MyPdfMailer < ActionMailer::Base def report(report_id, recipient_email) report = Report.find(report_id) report_pdf_view = ReportPdf.new(report) report_pdf_content = report_pdf_view.render() attachments['report.pdf'] = { mime_type: 'application/pdf', content: report_pdf_content } mail(:to => recipient_email, :subject => "Your report is attached") end end 
+7
source

I followed RailsCasts for PRAWN . Taking what has already been said and what I am trying to do in a similar way, I set the name of the attachment and then created a PDF.

InvoiceMailer:

  def invoice_email(invoice) @invoice = invoice @user = @invoice.user attachments["#{@invoice.id}.pdf"] = InvoicePdf.new(@invoice, view_context).render mail(:to => @invoice.user.email, :subject => "Invoice # #{@invoice.id}") end 
+3
source

My decision:

 render_to_string('invoices/show.pdf', :type => :prawn) 

The PDF was corrupted because I did not write a block for the mail function, and the multi-component email was incorrect.

0
source

All Articles