Exportar partidos a PDF

Issue #2 resolved
Santiago Avendaño
repo owner created an issue

Comments (5)

  1. Santiago Avendaño reporter

    It seems to be a web2py issue: generic view is sanitizing the html to much, so it is removing needed attributes (align, widht, etc.).

    A workaround to bypass generic html sanitization is calling pyfpdf directly:

    def pdf_test(): 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'))
    

    I've made a Pull Request with the fix:

    https://github.com/web2py/web2py/pull/447

    Best regards

  2. Log in to comment