import os
from gluon.contrib.fpdf import FPDF, HTMLMixin
from gluon.sanitizer import sanitize
filename = '%s/%s.html' % (request.controller,request.function)
html=response.render(filename)
def image_map(path):
if path.startswith('/%s/static/' % request.application):
return os.path.join(request.folder, path.split('/', 2)[2])
return 'http%s://%s%s' % (request.is_https and 's' or '', request.env.http_host, path)
class MyFPDF(FPDF, HTMLMixin):
pass
pdf = MyFPDF()
pdf.add_page()
# pyfpdf needs some attributes to render the table correctly:
html = sanitize(
html, allowed_attributes={
'a': ['href', 'title'],
'img': ['src', 'alt'],
'blockquote': ['type'],
'td': ['align', 'bgcolor', 'colspan', 'height', 'width'],
'tr': ['bgcolor', 'height', 'width'],
'table': ['border', 'bgcolor', 'height', 'width'],
}, escape=False)
pdf.write_html(html, image_map=image_map)
return XML(pdf.output(dest='S'))