# Generating file for download from controller method on demand

**URL:** <https://www.ruby-forum.com/t/generating-file-for-download-from-controller-method-on-demand/138735>\
**Category:** Rails\
**Created:** [May 27, 2008, 10:25am UTC](https://www.ruby-forum.com/t/generating-file-for-download-from-controller-method-on-demand/138735 "2008-05-27T10:25:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [May 27, 2008, 10:25am UTC](https://www.ruby-forum.com/t/generating-file-for-download-from-controller-method-on-demand/138735/1 "2008-05-27T10:25:48Z")

</div>

Hello

I’d like to implement in my app functionality of generating text files  
for download (like excel files etc.) generated on demand (user clicks  
“download a copy” and rails app should generate it and let user  
download, or at least start downloading by user)

Is there any possibility to do it as simple as possible? 🙂

Best regards and thank you in advance for your responds  
Pawel K.

---

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [May 27, 2008, 10:54am UTC](https://www.ruby-forum.com/t/generating-file-for-download-from-controller-method-on-demand/138735/2 "2008-05-27T10:54:13Z")

</div>

that’s my controller code for downloading a pdf file generated in the  
method write\_invoice\_pdf with pdf-writer

def pdf  
write\_invoice\_pdf  
send\_file(File.join(PDF\_PATH\_INVOICES, “result.pdf”), :filename =\>  
‘result.pdf’, :disposition =\> ‘attachment’, :type =\> ‘application/pdf’)  
end

that’s basically all to it. generate the file, store it ‘somewhere’,  
PDF\_PATH\_INVOICES in that case defines the target folder and is set in  
development.rb.  
after that it needs only to be sent and the browser will open the dialog  
to ask if you want to open it or save to disk.
