learn from web2py book, it seems generate pdf function using reportlab must 
be landed on the page (the page must be visited so that it can be generated 
as expected, the pdf is generated but can't be opened). Already tried with 
onvalidation, redirect, modules, after_insert callback. the only thing that 
work is when using redirect(URL()), perhaps i miss something, any thought 
or suggestion ?
e.g.
*models/db.py*
from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch, mm
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib import colors
from cgi import escape
import os

*controllers/default.py*
def pdf_email(form):
title = "This The Doc Title"
heading = "First Paragraph"
text = 'bla '* 10000
styles = getSampleStyleSheet()
tmpfilename=os.path.join(request.folder, 'private' , 'test.pdf' )
doc = SimpleDocTemplate(tmpfilename)
story = []
story.append(Paragraph(escape(title),styles["Title"] ) )
story.append(Paragraph(escape(heading),styles["Heading2"] ) )
story.append(Paragraph(escape(text),styles["Normal"] ) )
story.append(Spacer(1,2*inch) )
doc.build(story)
data = open(tmpfilename,"rb").read()
os.unlink(tmpfilename)
response.headers['Content-Type']='application/pdf'
return data

# quotation
def quotation():
fields = ['salutation', 'name', 'address', 'phone', 'email', 'product']
form = SQLFORM(db.quotation, fields = fields)
"""
* # work because the function is visited*
if form.process().accepted:
response.flash = 'form success'
redirect(URL('pdf_and_mail') )
* # not work because the function is not visisted, just call the function*
if form.process(onvalidation = pdf_email).accepted:
response.flash = 'form success'
* # not work because the function is not visisted**, just call the function* 
if form.process().accepted:
quotation_transaction.pdf_mail()
response.flash = 'form success'
"""
* # not work test using after_insert database callback*
if form.process().accepted:
response.flash = 'form success'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill out the form'
return dict(form=form)

def pdf_and_mail():
title = "This The Doc Title"
heading = "First Paragraph"
text = 'bla '* 10000
styles = getSampleStyleSheet()
tmpfilename=os.path.join(request.folder, 'private' , 'test.pdf' )
doc = SimpleDocTemplate(tmpfilename)
story = []
story.append(Paragraph(escape(title),styles["Title"] ) )
story.append(Paragraph(escape(heading),styles["Heading2"] ) )
story.append(Paragraph(escape(text),styles["Normal"] ) )
story.append(Spacer(1,2*inch) )
doc.build(story)
data = open(tmpfilename,"rb").read()
os.unlink(tmpfilename)
response.headers['Content-Type']='application/pdf'
return data

ref:
http://web2py.com/books/default/chapter/29/10/services#ReportLab-and-PDF

thanks and best regards,
stifan

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to