[reportlab-users] Struggling with SimpleDocTemplate

Charlie Clark reportlab-users@reportlab.com
Thu, 15 Apr 2004 13:23:32 +0200


Further to my mail this morning I'm including the source.

I think I have solved the problem with the image: it actually seems to be 
down to setting the row height in which case Platypus seems to enforce 
vAlign = bottom for the cells.

The task is to create ten 50mm x 90mm badges per page in two columns with a 
4mm border between columens and 3mm between rows. The page is A4 and the 
margins are 13mm and 15mm respectively.

I've had to use tables in tables to solve some issues: Images and Text in 
the same table cell and it seems at the moment that the best thing to do is 
to use Spacers in table cells rather than relying on rowHeights. I'd 
appreciate alternative solutions.

With BoundaryOn and Grid enabled it is possible to see that the main table 
is indented about 2 mm from the left and top of the enclosing frame. How do 
I get rid of this having already set padding for the document to be 0?

Help appreciated

Charlie Clark

from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Image, 
Frame, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import mm, pica
from reportlab.lib import colors
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.fonts import addMapping

pdfmetrics.registerFont(TTFont("Humanist777", "Humanist777.ttf"))
addMapping("Humanist777", 0, 0, "Humanist777")

p_height = defaultPageSize[1]
p_width = defaultPageSize[0]
c_width = 90 * mm
c_height = 50 * mm
col_gap = 4.5 * mm
row_gap = 4 * mm

styles = getSampleStyleSheet()

I = Image("begeistert-logo.gif")
I.drawWidth = 80 * mm
I.drawHeight = 17.8 * mm

filename = raw_input("filename?")
f_in = open(filename, "r")
lines = f_in.readlines()[1:]
f_in.close()
lines.sort()

cells = []
row = []

count = 0

for line in lines:
	line = unicode(line, "latin-1")
	line = line.encode("utf-8")
	count += 1
	record = line.split(";")
	content = "%s %s\n\n\n\n%s      %s" %(record[1], record[0], record[2], 
count +20)
	cell = Table([[I], [content]])
	cell.hAlign = "LEFT"
	cell.vAlign = "TOP"
	cell.leftPadding = 0
	cell.topPadding = 0
	cell.setStyle(TableStyle([
                   ('LEFTPADDING', (0,0), (-1, -1),  1 * mm),
                   ('TOPPADDING', (0,0), (-1, -1), 2.5 * mm),
                   ('BOTTOMPADDING', (-1, -1), (-1, -1), 3 * mm),
                   ('FONTSIZE', (0,0), (-1,-1), 12),
                   ('FONTNAME', (0,0), (-1,-1), "Humanist777"),
                   ('GRID', (0,0), (-1, -1), 1, colors.black)

                   ]))
	cell._argW[0] = 90 * mm
	row.append(cell)
	if not count % 2 and count % 10:
		cells.append(row)
		cells.append([Spacer(0, 3.2 * mm), Spacer(0, 3 * mm), Spacer(0, 3 * mm)])
		row = []
	elif not count % 10:
		cells.append(row)
		row = []
	else:
		row.append(Spacer(0, 42.5 * mm))

def go():
    Story = []
    style = styles["Normal"]
    t = Table(cells, colWidths=(c_width, col_gap, c_width), rowHeights=None)
    t.hAlign = 'LEFT'
    t.vAlign = 'TOP'
    t.leftPadding = 0
    t.topPadding = 0
    t.setStyle(TableStyle([
                   ('LEFTPADDING', (0,0), (-1, -1), 0),
                   ('TOPPADDING', (0,0), (-1, -1), 0),
                   ('GRID', (0,0), (-1, -1), 1, colors.black)
                   ]))
                   
    Story.append(t)

    doc = SimpleDocTemplate("badges.pdf", pagesize= defaultPageSize)
    doc.leftMargin = 13 * mm
    doc.bottomMargin = 13 * mm
    doc.topMargin = 15 * mm
    doc.rightMargin = 11 * mm
    doc.leftPadding = 0
    doc.rightPadding = 0
    doc.topPadding = 0
    doc.bottomPadding = 0
    doc.showBoundary = 1
    doc.build(Story)

go()