On 28 May 2015 at 07:40, Jason (spot) Brower <[email protected]> wrote:

> Pdf reporting can take a lot of work. Is this for a single page report or
> many pages. How are formated? And do you have an examole report in pdf
> format?
>
>
I have used it once and it works.  Here is the code as example:

In the controller:

from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch, mm
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib import colors
from cgi import escape
from reportlab.lib.pagesizes import inch, A4, landscape
from reportlab.pdfgen import canvas

def doccenter_pdf():
#    import pdb
#    pdb.set_trace()

    query = request.vars.query
    data = db(query).select(db.doccenter.doc_nr,
                            db.doccenter_location.location,
                            db.doccenter.author, db.doccenter.title,
                            db.doccenter.institution,
                            db.doccenter.reference, db.doccenter.source,
                            db.doccenter.publication_date,
                            db.doccenter.keywords,
                            db.doccenter.anual_report,
                            db.doccenter_category.category,
                            orderby = db.doccenter.publication_date |
db.doccenter.author)

    title = T('Doccenter search result')
    styles = getSampleStyleSheet()
    tmpfilename = os.path.join(request.folder, 'private', str(uuid4()))


    normalStyle = styles['Normal']

    cols = [('doccenter', 'doc_nr'),
            ('doccenter_location', 'location') ,
            ('doccenter', 'author') ,
            ('doccenter', 'title'),
            ('doccenter', 'institution'),
            ('doccenter', 'reference'),
            ('doccenter', 'source'),
            ('doccenter', 'publication_date'),
            ('doccenter', 'keywords'),
            ('doccenter', 'anual_report'),
            ('doccenter_category', 'category')]

    hds = ['doc_nr', 'location', 'author', 'title', 'institution',
           'reference', 'source', 'date',
            'keywords', 'anual_report', 'category']


    headings = [Paragraph('''
    <para align=left spaceb=3><b>%s</b> </para>''' % '
'.join(c.split('_')).capitalize(),
                          styles["BodyText"]) for c in hds]

    lst = [headings]
    for item in data:

        rec = []
        for ikey in cols:
            #import pdb; pdb.set_trace()
            rec.append(par_from_item(item[ikey[0]][ikey[1]], styles))

        lst.append(rec)


    elements = []
    elements.append(Paragraph(escape(title), styles['Title']))


    # First the top row, with all the text centered and in Times-Bold,
    # and one line above, one line below.
    ts = [('ALIGN', (1, 1), (-1, -1), 'CENTER'),
          ('LINEABOVE', (0, 0), (-1, 0), 1, colors.purple),
          ('LINEBELOW', (0, 0), (-1, 0), 1, colors.purple),
          ('FONT', (0, 0), (-1, 0), 'Times-Bold'),

          # The bottom row has one line above, and three lines below of
          # various colors and spacing.
          ('LINEABOVE', (0, -1), (-1, -1), 1, colors.purple),
          ('LINEBELOW', (0, -1), (-1, -1), 0.5, colors.purple,
           1, None, None, 4, 1),
          ('LINEBELOW', (0, -1), (-1, -1), 1, colors.red),
          ('FONT', (0, -1), (-1, -1), 'Times-Bold')]

    #  Alternate row background
    for i, _ in enumerate(lst):
        if odd(i):
            ts.append(("BACKGROUND", (0, i), (-1, i), colors.cyan))
    t = Table(lst, style = ts)
    # Adjust column width
    t._argW[3] = 1.8 * inch
    t._argW[5] = 1.0 * inch
    t._argW[0] = 1.0 * inch

    t.setStyle(TableStyle())

    elements.append(t)

    doc = SimpleDocTemplate(tmpfilename, pagesize = landscape(A4))
    doc.build(elements, canvasmaker = NumberedCanvas)
    pdf = open(tmpfilename, "rb").read()
    os.unlink(tmpfilename)
    response.headers['Content-Type'] = 'application/pdf'
    return pdf

As you can see there was a lot of work and experimenting involved.

Regards
Johann
-- 
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)

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