Create a pdf and attach it to a mail

519 views
Skip to first unread message

jjg0

unread,
Mar 16, 2013, 5:15:39 PM3/16/13
to web...@googlegroups.com
I started looking at the MyFPDF examples and I am able to make simple pdf's.  I want to be able to make a pdf and attach it to an email.  Here is a simple example to show what I am doing and where I get lost:

def sample():
        from gluon.contrib.pyfpdf import FPDF, HTMLMixin
        class MyFPDF(FPDF, HTMLMixin):
            def header(self):  
                self.set_font('Arial','',15)           
            def footer(self):
                # Do Nothing
        pdf=MyFPDF()
        pdf.add_page()
        pdf.cell(0,10, 'Text inside pdf, blah blah blah' ,0,1,'C')

        # Here is where I get lost, the example I followed just made it so you could go to <localhost>.sample.pdf
        # and view the pdf, which works fine but it's not what I want to do so I removed that code snippet
        # I think I need to do something with the pdf to make it an attachable file and then attach it, so the rest would be

        # Do something awesome to pdf to make it useful to mail

        mail.send(to=['sam...@abc.com'],
          subject='An email with a pdf attached to it',
          message='test',
          attachments = ???  #Somehow attach my pdf?


On my test setup I can send emails fine, it's just making the pdf and adding it as an attachment I am stuck on.  I am using GAE if that matters at all.

Thanks for the help!


Alan Etkin

unread,
Mar 16, 2013, 5:51:54 PM3/16/13
to
I started looking at the MyFPDF examples and I am able to make simple pdf's.  I want to be able to make a pdf and attach it to an
email.  Here is a simple example to show what I am doing and where I get lost:

If your app has write access to the filesystem (almost any environment has except, i.e. GAE), I think you could solve this by writing the fpdf output to an actual file (fpdf supports it, look at the project wiki), then pass the new filepath to the attachment as described in the book:
http://www.web2py.com/books/default/chapter/29/08#Attachments

One caveat I can think of is that for a given traffic/requests, you'll end up with a lot of useless pdf data stored, so you could write some additional background task for ereasing them.

I suppose you could even leave the filelike object which receives the fpdf output open and use that instead as attachment input without the need to write to the filesystem. If you pass a file-like object as argument of Attachment, you have to specify also the name of the file with filename="filename.ext" (documented in the api)

This is not a new issue. I think that you could find more useful stuff with a search in this group or here:

http://www.web2pyslices.com

EDIT: sorry I did not read the GAE part of your message, so the second way should be the appropiate.

jjg0

unread,
Mar 17, 2013, 9:24:47 AM3/17/13
to web...@googlegroups.com
I'm not sure what you mean in the second option, when you say filelike object what are you referring to?  I tried:

...
...
attachments = mail.Attachment(pdf, filename='sample.pdf'))

where pdf=MyFPDF() from the original example, but that doesn't work. 

I'm sure this isn't a new issue, but I haven't been able to find any good information on what to do for this.
I did look on web2pyslices.com.  There are a few good examples of how to make a pdf and show it on a page,  but I did not find any examples of how to attach the pdf to an email and send it.
I did read the book but it only shows an example of attaching a file from a path, it doesn't cover what other arguments mail.Attachment can take or how else to use it.  Since I am on GAE it seems that isn't an option so the book isn't helpful for this.
I have searched this group but was not able to find a solution to the problem. 

Still looking for help on this.

Thanks



On Saturday, March 16, 2013 5:48:50 PM UTC-4, Alan Etkin wrote:
I started looking at the MyFPDF examples and I am able to make simple pdf's.  I want to be able to make a pdf and attach it to an
email.  Here is a simple example to show what I am doing and where I get lost:

Alan Etkin

unread,
Mar 17, 2013, 7:45:53 PM3/17/13
to web...@googlegroups.com
> I'm not sure what you mean in the second option

I meant that you could:
"... even leave the filelike object which receives the fpdf output open and use that instead as attachment input without the need to write to the filesystem ..."


> when you say filelike object what are you referring to?

http://docs.python.org/2/glossary.html#term-file-object


> attachments = mail.Attachment(pdf, filename='sample.pdf'))

What does pdf contain?

For creating the attachments, this should work for App Engine:

You should be allowed to get the fpdf output as a string with something like

pdfasastring = fpdfobject.output(dest="S")

Then store this output with a file-like object

import StringIO
sio = StringIO.StringIO()

Now store the pdf string on the StringIO object

sio.write(pdfasastring)

Perhaps you need to return the seek position in case that the Attachment class uses the .read() method
sio.seek(0)

Then, you supposedly can add an attachment with:
mail.Attachment(sio, filename="mandatory.ext")

Jaime Herrero

unread,
Jul 11, 2013, 1:22:33 AM7/11/13
to web...@googlegroups.com
Thanks a lot Alan for the idea, I just tested and I had to do some modifications to the code:

pdfasastring = fpdfobject.output(dest="S")
import StringIO
sio = StringIO.StringIO()
sio.write(pdfasastring)
sio.seek(0)

The following instruction create a mime type for the attachment and now, the object created (att1) can be used in the mail object
att1 = Mail.Attachment(sio, filename="mandatory.ext")

mail.send(to=['sam...@abc.com'],
          subject='An email with a pdf attached to it',
          message='test',
          attachments = (att1,)
)

BR
Jaime
Reply all
Reply to author
Forward
0 new messages