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


Mariano Reingart
http://www.sistemasagiles.com.ar
http://reingart.blogspot.com


On Wed, May 21, 2014 at 2:04 PM, Carlos Cesar Caballero Díaz <
[email protected]> wrote:

>  Thanks Mariano and Carlos, there is a simple code:
>
> controller default.py:
>
> def pdf_test:
>     return dict(hello="hello")
>
> view default/pdf_test.html:
>
> <body>
>     <h1>{{=hello}}</h1>
>     <p>This is a text</p>
>     <table width="100%">
>         <thead>
>             <tr>
>                 <th width="40%">name</th>
>                 <th width="60%">lastame</th>
>             </tr>
>         </thead>
>         <tbody>
>             <tr>
>                 <td width="40%">pepe</td>
>                 <td width="60%">paco</td>
>             </tr>
>         </tbody>
>     </table>
> </body>
>
>
> "http://localhost:8000/myapp/default/pdf_test.pdf";<http://localhost:8000/myapp/default/pdf_test.pdf>shows
>  a "Table column/cell width not specified, unable to continue" error.
> and removing the table, return a blank pdf
>
>
> El 21/05/14 11:33, Mariano Reingart escribió:
>
> Yes, pyfpdf has a basic html parser (based on python stdlib) and needs
> some conventions to translate tables to PDF.
>
> Could you make a minimal example to test and debug it?
> That way it would be easy to reproduce and see how to adapt the html to be
> rendered.
>
>  You can look at the documented examples, using <thead> and <th> tags
> will help, and you need to specify the total table and cell widths:
>
>  https://code.google.com/p/pyfpdf/wiki/WriteHTML
>
>  Best regards,
>
>
> Mariano Reingart
> http://www.sistemasagiles.com.ar
> http://reingart.blogspot.com
>
>
> On Wed, May 21, 2014 at 12:26 PM, Carlos Costa <[email protected]>wrote:
>
>> It uses pyfpdf to convert it.
>> But there are some restrictions as I remember.
>> You check it here https://code.google.com/p/pyfpdf/
>>
>>
>> 2014-05-21 12:13 GMT-03:00 Carlos Cesar Caballero Díaz <
>> [email protected]>:
>>
>>  Hi, I need some help, when I call a view with ".pdf" this:
>>>
>>> <html>
>>>     <head>
>>>         <title>Report</title>
>>>     </head>
>>>     <body>
>>>         <table>
>>>             <tr>
>>>                 <td width="50%">name</td>
>>>                 <td width="50%">pepe</td>
>>>             </tr>
>>>         </table>
>>>     </body>
>>> </html>
>>>
>>> or this:
>>>
>>> <body>
>>>     <table>
>>>         <tr>
>>>             <td width="50%">name</td>
>>>             <td width="50%">pepe</td>
>>>         </tr>
>>>     </table>
>>> </body>
>>>
>>> returns a blank one page pdf. Now if I put the content before the body
>>> tag, it is rendered, but the table allways throw a
>>> "Table column/cell width not specified, unable to continue" error.
>>>  --
>>> 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.
>>>
>>
>>
>>
>>  --
>> Att.
>>
>>  Carlos J. Costa
>>  Cientista da Computação
>> Esp. Gestão em Telecom
>>
>> EL MELECH NEEMAN!
>> אָמֵן
>>
>>   --
>> 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.
>>
>
>  --
> 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.
>
>
>  --
> 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.
>

-- 
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