# ActionMailer - attaching a PDF

**URL:** <https://www.ruby-forum.com/t/actionmailer-attaching-a-pdf/184951>\
**Category:** Rails\
**Created:** [March 10, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/actionmailer-attaching-a-pdf/184951 "2010-03-10T21:12:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![archfiend997](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@archfiend997](https://www.ruby-forum.com/u/archfiend997)\
**Post date:** [March 10, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/actionmailer-attaching-a-pdf/184951/1 "2010-03-10T21:12:19Z")

</div>

[http://pastie.org/863834](http://pastie.org/863834)

The “a.body” appears to want a string, which is why I tried  
render\_to\_string as shown in the code linked above.

This doesn’t work, I get “undefined method for render\_to\_string”.

I basically need to attach a PDF that is generated in another  
controller, passing ‘data’ as parameters for which the PDF generating  
engine will use.

I am able to otherwise send e-mails with ActionMailer, and generate this  
PDF. I just can’t attach the PDF to the e-mail.

The API says it wants:  
a.body = generate\_your\_pdf\_here()

How to do I replace the “generate\_your\_pdf\_here()” with what is produced  
by the action ‘print\_pdf’ in the ‘report’ controller?

Any clues?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![archfiend997](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@archfiend997](https://www.ruby-forum.com/u/archfiend997)\
**Post date:** [March 11, 2010, 3:06pm UTC](https://www.ruby-forum.com/t/actionmailer-attaching-a-pdf/184951/2 "2010-03-11T15:06:06Z")

</div>

Hey, here’s an example,

def mailer\_method(mymodel)  
content\_type “multipart/alternative”  
part :content\_type =\> “text/html”, :body =\> mymodel.text  
attachment “application/pdf” do |a|  
a.filename = mymodel.pdf\_filename  
a.body = mymodel.pdf\_render.render  
end

Take a look on the “content\_type” header, you have to set the mail as a  
Multipart email, not just html.

Regards,  
Fernando

---

<div class="post-metadata">

**Author:** ![archfiend997](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@archfiend997](https://www.ruby-forum.com/u/archfiend997)\
**Post date:** [March 12, 2010, 12:12am UTC](https://www.ruby-forum.com/t/actionmailer-attaching-a-pdf/184951/3 "2010-03-12T00:12:55Z")

</div>

This definitely helps, thank you, but I still am unsure how to pass the  
parameters to the mymodel.pdf\_render action?

The only way I know of to pass parameters is as if it were a URL, such  
as with:

/mymodel/pdf\_render?data=BLAH

or something like:

:controller =\> ‘mymodel’, :action =\> ‘pdf\_render’, :data =\> “BLAH”

How could I achieve this using the syntax you gave me?

> ```
> a.body = mymodel.pdf_render.render
> 
> ```

Sorry for my newbieness, THANKS!

---

<div class="post-metadata">

**Author:** ![archfiend997](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@archfiend997](https://www.ruby-forum.com/u/archfiend997)\
**Post date:** [March 12, 2010, 3:42am UTC](https://www.ruby-forum.com/t/actionmailer-attaching-a-pdf/184951/4 "2010-03-12T03:42:19Z")

</div>

I think I got it, thanks for your help!!!
