[reportlab-users] Appending Tables

Saketh Bhamidipati saketh.bhamidipati at gmail.com
Sun Aug 27 13:22:00 EDT 2006


On 8/27/06, Andy Robinson <andy at reportlab.com> wrote:
>
> > How can I get all of the tables to stack one under the other, and, if
> > the page is filled, get them to go on the next page?
>
> Are you using BaseDocTemplate (or SimpleDocTemplate) and a flowing
> document model, or did you explicitly create a frame and draw on the
> canvas?  Normally it will proceed to the next page automatically.  Can
> you produce a complete self-contained script?
>
> Thanks,
>
>
> Andy Robinson
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at reportlab.com
> http://two.pairlist.net/mailman/listinfo/reportlab-users


Here's the self-contained script:

#!/usr/bin/python

import os
import datetime

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer,
Flowable, Table, TableStyle
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
from reportlab.platypus.frames import Frame
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.styles import ParagraphStyle, PropertySet
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT
from reportlab.lib.units import inch
from reportlab.lib.colors import black

PAGE_HEIGHT = defaultPageSize[1]; PAGE_WIDTH = defaultPageSize[0]
styles = getSampleStyleSheet()

def bullet(text):
   return (u"\N{BULLET} " + unicode(text))

now = datetime.datetime.now()

class NotalonTemplate(BaseDocTemplate):
    """
    Direct interface class to ReportLab's Platypus library.
    """
    _invalidInitArgs = ('pageTemplates',)
    def __init__(self, filename, title, author, **kw):
        self.textframe = textframe = Frame(2 * inch, 1 * inch, 6 * inch, 9 *
inch, id = 'TF', showBoundary = 0)
        self.headingframe = headingframe = Frame(0.5 * inch, 1 * inch, 1.5 *
inch, 9 * inch, id = 'HF', showBoundary = 0)
        self.allowSplitting = 1
        apply(BaseDocTemplate.__init__,(self,filename),kw)

        self.title = title
        self.author = author

        self.addPageTemplates(PageTemplate('normal',[textframe,headingframe],
self.myPageTemplate))

    def myPageTemplate(self, canvas, doc):
        PAGE_WIDTH, PAGE_HEIGHT = canvas._pagesize
        canvas.saveState()
        canvas.setFont('Times-Roman', 12)
        canvas.drawCentredString(PAGE_WIDTH / 2.0, PAGE_HEIGHT - 108,
self.title)
        canvas.drawString(PAGE_WIDTH - 2 * inch, PAGE_HEIGHT - inch,
self.author)
        canvas.drawString(PAGE_WIDTH - 2 * inch, PAGE_HEIGHT - inch - 12,
now.strftime("%d %B %Y"))
        canvas.setFont('Times-Roman', 10)
        canvas.drawCentredString(PAGE_WIDTH / 2.0, inch / 2.0, "%d" %
doc.page)
        canvas.restoreState()

class NotalonPDF:
    """
    Wrapper and utility class to make PDF generation easier. The application
    interfaces with this class, and this class in turn interfaces with
Platypus.
    """
    def __init__(self, filename, title, author):
        self.title = title
        self.author = author
        self.doc = NotalonTemplate(filename, title, author)
        self.story = []
        self.style = TableStyle([
            ('TOPPADDING', (0, 0), (-1, -1), 0),
            ('BOTTOMPADDING', (0, 0), (-1, -1), 0),
            ('LEADING', (0, 0), (-1, -1), 10),
            ('FONTSIZE', (0, 0), (-1, -1), 10),
            ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
            ('INNERGRID', (0,0), (-1,-1), 0.25, black),
            ('BOX', (0,0), (-1,-1), 0.25, black),
            ('VALIGN', (0, 0), (-1, -1), 'TOP')])

    def addTextAndHeading(self, text, heading):
        texts = []
        headings = []

        for line in text.strip().splitlines():
            texts.append(Paragraph(bullet(line), style=styles["Normal"]))

        for line in heading.strip().splitlines():
            headings.append(Paragraph(line, style=styles["Normal"]))

        data = [(headings, texts)]
        t = Table(data, colWidths=(1.5 * inch, 5.5 * inch),
                          style=self.style)
        t.hAlign = 'RIGHT'
        for i in range(0, 30):
            self.story.append(t)

    def construct(self):
        self.doc.build(self.story)


test = NotalonPDF("test.pdf", "A Test", "Test")

test.addTextAndHeading("It <i>almost</i> works! \n"*4, "heading")

test.construct()

os.system("start test.pdf")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://two.pairlist.net/pipermail/reportlab-users/attachments/20060827/d0bfc26a/attachment.htm


More information about the reportlab-users mailing list