I was also having some major alignment issues with this. I used the function for the 5160 script and made some modifications. I have now successfully got it to line up with the 5160 format. Here is the function with modifications to the python script:from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
import os, glob, sys, time
import textwrap
#Logic for averyLabel 5160
def avery5160(outfile, font, font_size, addressInput):
print 'Creating labels in Avery 5160 format'
# PDF vars
if os.path.exists(outfile):
os.remove(outfile)
out_pdf = canvas.Canvas(outfile, pagesize = letter)
out_pdf.setFont(font, font_size)
addressItem = addressInput.split('$')
hs = 0.25
vs = 10.3
horizontal_start = hs * inch #staring point of label horizontally
vertical_start = vs * inch #starting point of label vertically
count = 0 #initially the count is 0
for item in addressItem:
if count > 0 and count % 30 == 0:
out_pdf.showPage()
out_pdf.setFont(font, font_size)
horizontal_start = hs * inch
vertical_start = vs * inch
elif count > 0 and count % 10 == 0:
horizontal_start = horizontal_start + 2.8 *inch
vertical_start = vs * inch
label = out_pdf.beginText()
label.setTextOrigin(horizontal_start, vertical_start)
details = item.split('~')
for detail in details:
if len(detail) > 45:
name = detail[0:44]
label.textLine(name)
name = detail[44:60]
label.textLine(name)
else:
label.textLine(detail)
out_pdf.drawText(label)
vertical_start = vertical_start - 1.05 * inch
count = count + 1
out_pdf.showPage()
out_pdf.save()
print '\nCreated %s\n' %outfile
I have this working together with another script to generate labels (I'm not using it as part of a JS web map service) for the staff in the Assessor's office for my county. The staff runs a query on parcels they want mailing addresses for and I have a dictionary that pulls the addresses out for the selected parcels and puts them in a text file and generates the labels using the above script. It took a lot of playing around with the numbers, but finally just got it working properly this morning. Here's the pdf:EDIT: I just want to add a small disclaimer. This works well in my unique situation and I am not sure how well it would work in other situations. I have this as part of a script that works with another script that builds a text file to add the proper delimiters, etc. I was using a relatively small font (size 9) with the 'Helvetica' typeface. I spent a lot of time playing around with the different intervals(and banging my head against the desk) to ensure the text was properly fitted inside all the slots for the Avery 5160 sheet with my specific fontsize/type. So my advice to anyone else is to just kind of play around with it and see what kind of outputs you get. One thing I know, I am definitely sick of looking at this code! 🙂