Ok so the above plan works fine*. *In the interest of completeness, the original demo code can be made to work sensibly on modern web2py (2.9.11). So you can remove the code in the controller and (essentially) paste it into a view. But a few corrections are needed - the major ones being adding =XML() around the pdf.output call and just using {{ instead of {{{ . A diff is attached.
-- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
--- default.py.orig 2014-11-11 12:19:31.175296193 +1300 +++ default.py 2014-11-11 12:19:53.827296764 +1300 @@ -69,38 +69,7 @@ TR(TD("World"),TD("40")))] table = TABLE(*rows, _border="0", _align="center", _width="50%") - if request.extension=="pdf": - from gluon.contrib.pyfpdf import FPDF, HTMLMixin - - # create a custom class with the required functionalities - class MyFPDF(FPDF, HTMLMixin): - def header(self): - "hook to draw custom page header" - logo=os.path.join(request.env.web2py_path,"gluon","contrib","pyfpdf","tutorial","logo_pb.png") - self.image(logo,10,8,33) - self.set_font('Arial','B',15) - self.cell(65) # padding - self.cell(60,10,response.title,1,0,'C') - self.ln(20) - - def footer(self): - "hook to draw custom page header (printing page numbers)" - self.set_y(-15) - self.set_font('Arial','I',8) - txt = 'Page %s of %s' % (self.page_no(), self.alias_nb_pages()) - self.cell(0,10,txt,0,0,'C') - - pdf=MyFPDF() - # create a page and serialize/render HTML objects - pdf.add_page() - pdf.write_html(str(XML(table, sanitize=False))) - pdf.write_html(str(XML(CENTER(chart), sanitize=False))) - # prepare PDF to download: - response.headers['Content-Type']='application/pdf' - return pdf.output(dest='S') - else: - # normal html view: - return dict(chart=chart, table=table) + return dict(chart=chart, table=table) def listing(): response.title = "web2py sample listing"
--- report.pdf.bak 2010-09-10 23:18:17.000000000 +1200 +++ report.pdf 2014-11-11 12:26:55.975307395 +1300 @@ -1,20 +1,24 @@ -{{{ +{{ from gluon.contrib.fpdf import FPDF, HTMLMixin class MyFPDF(FPDF, HTMLMixin): def header(self): - self.image('tutorial/logo_pb.png',10,8,33) + #self.image('tutorial/logo_pb.png',10,8,33) self.set_font('Arial','B',15) self.cell(80) self.cell(30,10,'Title',1,0,'C') self.ln(20) - + pass + def footer(self): self.set_y(-15) self.set_font('Arial','I',8) txt = 'Page %s of %s' % (self.page_no(), self.alias_nb_pages()) self.cell(0,10,txt,0,0,'C') + pass + + pass pdf=MyFPDF() #First page @@ -22,6 +26,6 @@ pdf.write_html(str(chart)) pdf.write_html(str(table)) response.headers['Content-Type']='application/pdf' -return pdf.output(dest='S') +=XML(pdf.output(dest='S')) -}}} +}}