All threads / How do I send Prawn pdf via email
Ask A Question


You’re not receiving notifications from this thread.

How do I send Prawn pdf via email

Neil Patel asked in Rails

Hello Everyone

how do i attaced a prawn pdf to email . I have implemented the prawn gem
so when you generate a new form and add the .pdf ot gives me the PDF , which works. but now when the form is created i want it to send an email with the PDF attached?

def new
    coach.build_referral(referrer: @current_referrer)

    respond_to do |format|
      format.pdf do
        pdf =
        send_data pdf.render, filename: 'referrer.pdf',
                              type: 'application/pdf',
                              disposition: :inline

but i would like to send an email when the form is submitted , i was thinking of doing this in the create method, This my current create method.

  def create
    coach.build_referral(referrer: @current_referrer)

        thinking adding code here?
      redirect_to referrers_root_path
      render :new

Hey Neil!

Attachments are pretty easy. You use the attachments method on the email and give it a file object.

class ReferralMailer < ActionMailer::Base
  def referral(recipient)
    attachments['referral_form.pdf'] =
    mail(:to => recipient, :subject => "New account information")

Since you're rendering your own file, you just pass that in and Rails should take care of the rest.

ok cool thanks Chris

how would I call the ReferralMailer method in the my controller?
with the new and create action?

Just like you would do with sending any other email. You can read more on the guides page:

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 70,078+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.