[reportlab-users] Confused? SimpleDocTemplate?

Robin Becker reportlab-users@reportlab.com
Fri, 20 Feb 2004 15:56:15 +0000


In article <51B62EFFBC83D6118ADA00096BB030A107CADDAF@usamcms7.mc.usa.xer
ox.com>, Schollnick, Benjamin <Benjamin.Schollnick@usa.xerox.com> writes

well actually the constructor for BaseDocTemplate (and by inheritance)
SimpleDocTemplate allows for a pagesize argument.

These are the defaults.

                {   'pagesize':defaultPageSize,
                    'pageTemplates':[],
                    'showBoundary':0,
                    'leftMargin':inch,
                    'rightMargin':inch,
                    'topMargin':inch,
                    'bottomMargin':inch,
                    'allowSplitting':1,
                    'title':None,
                    'author':None,
                    'invariant':None,
                    '_pageBreakQuick':1}


SimpleDocTemplate disallows pageTemplates as these are set in the class
itself. Actually PageTemplates have a pagesize as well to allow dynamic
changing of the canvas pagesize via Canvas.setPageSize.

As for a simple example the doctemplate.py module can be run as a script
and it runs this code.

    def myFirstPage(canvas, doc):
        canvas.saveState()
        canvas.setStrokeColor(red)
        canvas.setLineWidth(5)
        canvas.line(66,72,66,PAGE_HEIGHT-72)
        canvas.setFont('Times-Bold',24)
        canvas.drawString(108, PAGE_HEIGHT-108, "TABLE OF CONTENTS
DEMO")
        canvas.setFont('Times-Roman',12)
        canvas.drawString(4 * inch, 0.75 * inch, "First Page")
        canvas.restoreState()

    def myLaterPages(canvas, doc):
        canvas.saveState()
        canvas.setStrokeColor(red)
        canvas.setLineWidth(5)
        canvas.line(66,72,66,PAGE_HEIGHT-72)
        canvas.setFont('Times-Roman',12)
        canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page)
        canvas.restoreState()

    def run():
        objects_to_draw = []
        from reportlab.lib.styles import ParagraphStyle
        #from paragraph import Paragraph
        from doctemplate import SimpleDocTemplate

        #need a style
        normal = ParagraphStyle('normal')
        normal.firstLineIndent = 18
        normal.spaceBefore = 6
        from reportlab.lib.randomtext import randomText
        import random
        for i in range(15):
            height = 0.5 + (2*random.random())
            box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
            objects_to_draw.append(box)
            para = Paragraph(randomText(), normal)
            objects_to_draw.append(para)

        SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw,
            onFirstPage=myFirstPage,onLaterPages=myLaterPages)

    run()


>Folks,
>
>       Have I missed something?
>
>       I usually just make a canvas, and DrawStringText...
>       But for the report generator that I am making, I can't 
>       pre-determine the size of the document...
>
>       So I thought I would use a template...
>
>       But I don't see where or how, you can set the Document/page
>       size via the SimpleDocTemplate?
>
>       For example,  the GFE example in ../demos/Gadfly...
>
>def myFirstPage(canvas, doc):
>    canvas.saveState()
>    #canvas.setStrokeColorRGB(1,0,0)
>    #canvas.setLineWidth(5)
>    #canvas.line(66,72,66,PAGE_HEIGHT-72)
>    canvas.setFont('Times-Bold',16)
>    canvas.drawString(108, PAGE_HEIGHT-108, Title)
>    canvas.setFont('Times-Roman',9)
>    canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo)
>    canvas.restoreState()
>
>def myLaterPages(canvas, doc):
>    #canvas.drawImage("snkanim.gif", 36, 36)
>    canvas.saveState()
>    #canvas.setStrokeColorRGB(1,0,0)
>    #canvas.setLineWidth(5)
>    #canvas.line(66,72,66,PAGE_HEIGHT-72)
>    canvas.setFont('Times-Roman',9)
>    canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page,
>pageinfo))
>    canvas.restoreState()
>
>def go():
>    Elements.insert(0,Spacer(0,inch))
>    doc = SimpleDocTemplate('gfe.pdf')
>    doc.build(Elements,onFirstPage=myFirstPage, 
>                       onLaterPages=myLaterPages)
>
>It appears we are declaring the SimpleDocTemplate, with the filename, but I
>do not see in the documentation any initialization options for the pagesize?
>
>I have gone through the API reference, and users guide..  But still have not
>found any method to do this...  
>
>Any assistance would be appreciated...
>
>               - Benjamin
>_______________________________________________
>reportlab-users mailing list
>reportlab-users@reportlab.com
>http://two.pairlist.net/mailman/listinfo/reportlab-users
>

-- 
Robin Becker