# Attachment\_fu and server generated assets (PDF)

**URL:** <https://www.ruby-forum.com/t/attachment-fu-and-server-generated-assets-pdf/149224>\
**Category:** Rails\
**Created:** [September 22, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/attachment-fu-and-server-generated-assets-pdf/149224 "2008-09-22T17:51:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 22, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/attachment-fu-and-server-generated-assets-pdf/149224/1 "2008-09-22T17:51:55Z")

</div>

Hello,

I using attachment\_fu in our application for several data. attachment\_fu  
works fine for uploaded files.  
Now I generating some files (PDF) in an action. These PDF should stored  
in the filesystem as an asset directly. So the user can download her/his  
PDF documents (1…n) every time again without rendering a new PDF. Has  
anybody an idea?  
The PDF generated in this way:

```
pdf=FPDF.new
pdf.AddPage

pdf.XXX

```

Thanks for helping me,

Daniel

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 22, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/attachment-fu-and-server-generated-assets-pdf/149224/2 "2008-09-22T21:24:28Z")

</div>

Hello,

I solving the problem myself. I will describe my first solution.  
Generation the PDF document with FPDF like this.

> ```
> pdf=FPDF.new
> pdf.AddPage
> 
> pdf.XXX
> 
> ```

The way of saving the generated PDF to the Filesystem is

pdf.Output(filename)

The challenge was creating a empty attachment\_fu asset and saving the  
file to this asset.

asset = Asset.new()  
asset.filename = filename  
asset.content\_type = ‘application/pdf’  
asset.save

Then there is an empty asset, but nothing correspond with the  
asset.public\_filename. Solving this by creating the directory structure

FileUtils.mkdir\_p(“public”+asset.public\_filename.slice(0,asset.public\_filename.rindex("/")+1))

and then writing the file

pdf.Output(“public”+asset.public\_filename).

Note, a require ‘fileutils’ is needed for using mkdir\_p.

If anybody knows a more ruby-way solution, please contact me.
