Pyfpdf - How to fit long data descriptions in the table with proper text wrapping in rows/columns

2,754 views
Skip to first unread message

Rahul

unread,
Apr 11, 2013, 4:27:25 AM4/11/13
to web...@googlegroups.com

Hi All,
        I am having a few problems with my code to generate proper PDF reports. Basically I have the same problems with reportlab so i am giving pyfpdf a try -
Problems:
  1. Long text (example description in below code) does not fit in the boundaries. It does not wrap to next line it just exceeds the page width - please see sample report attached.
  2. I cant make zebra strips (alternate line/row color) to work properly here
  3. The HTML output does not work (I have the view rep_open_issues.html - no data)  [Live Demo for HTML pyfpdf does not work Ex - this url - http://www.web2py.com.ar/fpdf/default/listing ] Throws error - invalid view (default/listing.html)
Here is my code -

def rep_open_issues():
    response.title = "Open Issues"
    head = THEAD(TR(TH("Ticket",_width="15%"),
                    TH("Sub Cateogry",_width="20%"),
                    TH("Severity",_width="15%"),
                    TH("Description",_width="50%"),
                    _bgcolor="#A0A0A0"))
   
    foot = TFOOT(TR(TH("Fair Price - Good Day",_width="100%"),
                    _bgcolor="#E0E0E0"))
   
    querysql = (""" Select ticket_no, sub_category, severity, description from issues where status='Open' and
    created_by='"""  + logged_in_user )

    try:
        allissues = db.executesql(querysql)
    except: pass
    rows = []
   
    for issue in allissues:
        mycounter = []
        mycounter.append(issue)
       
        i = len(mycounter)
        col = i % 2 and "#F0F0F0" or "#FFFFFF"
        rows.append(issue)
   
   
   
    # make the table object
    body = TBODY(*rows)
    table = TABLE(*[head,foot, body],
                  _border="1", _align="center", _width="100%")

    if request.extension=="pdf":
        from gluon.contrib.pyfpdf import FPDF, HTMLMixin

        # define our FPDF class (move to modules if it is reused  frequently)
        class MyFPDF(FPDF, HTMLMixin):
            def header(self):
                self.set_font('Arial','B',15)
                self.cell(0,10, response.title ,1,0,'C')
                self.ln(20)
               
            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')
                   
        pdf=MyFPDF()
        # first page:
        pdf.add_page()
        pdf.write_html(str(XML(table, sanitize=False)))
        response.headers['Content-Type']='application/pdf'
        return pdf.output(dest='S')
    else:
        # normal html view:
        return dict(table=table)

Not  much is changed here- I know, i may be missing a few tricks. Please help me fill the same
    
As a Ref - I checked this thread ["Re: How to Generate the effective report by using web2py"] in google groups but without much resolution. Please suggest.

Thanks Rahul


rep_open_issues.pdf

Ovidio Marinho

unread,
Apr 11, 2013, 7:17:44 AM4/11/13
to web...@googlegroups.com
PDF paths are hard, so we tried some practical solutions. See this appreport. https://github.com/lucasdavila/web2py-appreport  After long searching found something that can permanently solve these problems that fpdf, pisa, geraldo bring us in doing pdf.

I think you, as I found the solution.

{{}}'s

      


         Ovidio Marinho Falcao Neto
                 Web Developer
             ovid...@gmail.com 
               83   8826 9088 - Oi
               83   9336 3782 - Claro
                        Brasil
              


2013/4/11 Rahul <rahul....@gmail.com>



--
 
---
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+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rahul

unread,
Apr 12, 2013, 4:29:12 AM4/12/13
to web...@googlegroups.com
Hi Ovidio,
       Thanks for this great resource. I have a few questions - Can we pass queries to this plugin? Can we select fields that we want for reporting? The wrapping thing works well with this plugin. 

Rahul.

Ovidio Marinho

unread,
Apr 12, 2013, 9:39:52 AM4/12/13
to web...@googlegroups.com

      


         Ovidio Marinho Falcao Neto
                 Web Developer
             ovid...@gmail.com 
               83   8826 9088 - Oi
               83   9336 3782 - Claro
                        Brasil
              


2013/4/12 Rahul <rahul....@gmail.com>

Rahul

unread,
Apr 14, 2013, 6:59:01 AM4/14/13
to web...@googlegroups.com
Hello  Lucas, Ovideo,
            Thanks for your help with this. I gave more time to the reportlab user manual and it gave me the answer - The point is you need to use Paragrah (<value>, styles['BodyText'] for the conflicting value and it would automatically wrap huge amount of data for you. So the report becomes like the one here in the attached screenshot.
The option given to use app_report web service is an attractive one, I'll definitely give it a try. However I dont know if I would always rely on external API (i.e excluded from my project) to use in my program.
For now I think reportlab has some tricks hidden in its sleeves. I'll definitely need time to explore it along with other API's. Again I found [http://www.geraldoreports.org/docs/examples/auto-expand-bands.html] Geraldo reports this link that explains how we can wrap text in tables. Same like what I was able to achieve here. Also geraldo has a web2py example. Thanks again to the vibrant web2py community.

Cheers, Rahul D


On Saturday, April 13, 2013 8:46:05 PM UTC+5:30, Lucas D'Avila wrote:
Hello Rahul!

If you just want to generate reports based on html code, you can use the we2py-appreport plugin, please watch this screencast [1] and read this instructions [2], this will show how to write html forms to render and filter your reports.

But, if you want to generate sophisticated reports, you can use the AppReport API [3], that is a web service released last month, it allows you to generate PDF reports using Jasper Reports lib / i-report tool, in whatever app, including apps hosted on GAE.

To use the AppReport API, you just need to install the python client [4], please watch this screencast [5] and read this instructions [6] for web2py apps.

we2py-appreport plugin
[1] vimeo.com/18601633


-

Many thanks Ovideo, for replying this post :D
done.png
Reply all
Reply to author
Forward
0 new messages