[reportlab-users] laser labels in platypus?

Tim Roberts timr at probo.com
Mon Oct 23 14:00:19 EDT 2006


Chris Withers wrote:

> Hi All,
>
> Has anyone done anything that has involved printing sheets of
> repeating patterns in platypus?
>
> I'm thinking specifically of avery laser labels here, but any examples
> that involve printing regular flows of sized boxes with text inside
> them will likely help ;-)


I do all of my labels in ReportLab, but not Platypus.  I only reach for
Platypus when I need a document with flowing sections, like a newsletter
or a reasonably word-wrapped paragraph.

Here's the basic outline of a program to write Avery 5160 mailing labels
in 8.5x11 form, along with my stream-of-consciousness notes on
measurements.  You'd want to remove the rectangle and scoot the text a
bit to the right, but it shows the basic framework.  This code goes
across first, then down; if you want down then across, change the divmod
line to
    x,y = divmod( ordinal,10 )

IMPORTANT NOTE!  Most people run the Acrobat viewer with "Auto-rotate
and Center" turned on in the print dialog.  Few things will screw up
your label alignment more than that helpful switch.  Be sure to turn it off!

    import sys
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import LETTER
    from reportlab.lib.units import inch

    canv = canvas.Canvas( 'Labels5160.pdf', pagesize=LETTER )
    canv.setPageCompression( 0 )

    # Spacing in points, bottom left = 0, 0
    # top right = 612, 792
    #
    # Avery 5160 labels have 1/2 inch top/bottom margins, 0.18 inch
    left/right
    # margins.  Labels are 2.6" by 1".  Labels abut vertically, but
    there is
    # a .15" gutter horizontally.
    #
    # Top to bottom:
    # 0.5  1.0  1.0  1.0  0.5
    # Left to right:
    # inches: 0.18 2.6 0.15 2.6 0.15 2.6 0.18
    # points: 14 187 11 187 11 187 14
    #
    # One inch margin: 72,72 - 540,720

    LABELW = 2.6 * inch
    LABELSEP = 2.75 * inch
    LABELH = 1 * inch

    def LabelPosition(ordinal):
        y,x = divmod(ordinal, 3)
        x = 14 + x * LABELSEP
        y = 756 - y * LABELH
        return x, y

    for pos in range( 0, 30 ):
        x, y = LabelPosition( pos )
        canv.rect( x, y, LABELW, -LABELH )
        tx = canv.beginText( x, y-11 )
        tx.setFont( 'Times-Roman', 11, 11 )
        tx.textLines( "string 1\nstring 2\nstring 3\nstring 4\nstring 5" )
        canv.drawText( tx )

    canv.showPage()
    canv.save()

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list