Python Forum

Full Version: Label Maker FPDF, Reportlab
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all, me again,

Basically my entire program is below and I'm looking for a way to change the layout of the generated pdf (Either using FPDF or reportlabs as was suggested to me previously) but I'm struggling. I need 4 labels to a page (One top left, one top right, one bottom left and one bottom right).

from fpdf import FPDF

def main():
    print("***********************************************")
    print("   Welcome to the Label Maker Program V2.2  ")
    print("***********************************************")
    label_list = []
    total_labels = []
    print ("Please input the sending address following the prompts below.")
    print ("Line 1: ")
    send_line1 = input()
    print ("Line 2: ")
    send_line2 = input()
    print ("City/Town: ")
    send_city = input()
    print ("Postcode: ")
    send_postcode = input()

    print (send_line1 + ", " + send_line2 + ", " + send_city + ", " + send_postcode)
    print (" ")
    print ("Is this correct? Y/N: ")
    a = input()
    if a == ("N") or a ==("n"):
        main()
    if a == ("No") or a == ("no"):
        main()


    print ("Please input the recieving address following the prompts below.")
    print ("Line 1: ")
    rec_line1 = input()
    print ("Line 2: ")
    rec_line2 = input()
    print ("City/Town: ")
    rec_city = input()
    print ("Postcode: ")
    rec_postcode = input()
    
    print (rec_line1 + ", " + rec_line2 + ", " + rec_city + ", " + rec_postcode)
    print (" ")
    print ("Is this correct? Y/N: ")
    b = input ()
    if b == ("N") or b == ("n"):
        main()
    if b == ("No") or  b == ("no"):
        main()

    print ("Please input the order number for this order: ")
    order = input()
    print ("Please input the number of distinct items on the PO: ")
    i = int(input())
    a = int(0)
    z = int(0)
    y = int(2)
    while a != i:
        
        print ("Please input the style number of this item: ")
        label_list.append(input())
    
        print("Please input the colour of this item: ")
        label_list.append(input())

        print("Please input the number of labels required for each unit of this item: ")
        label_list.append(int(input()))
        
        print("Please input the quantity of this item: ")
        label_list.append(int(input()))
        
        total_labels.append(label_list[y] * label_list[y + 1])        
        
        print (label_list)
        print (total_labels)
        
        y = y + 4
        
        a = a + 1
    a = int(0)
    y = int(0)
    
    style_colour = int(0)
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size = 12)
    while a > -1:
        if y == len(total_labels):
            break
        if a < total_labels[y]:
            print("Printing")
            pdf.cell(100, 10, txt = send_line1,
                ln = 1, align = '')
            pdf.cell(100, 10, txt = send_line2,
                ln = 2, align = '')
            pdf.cell(100, 10, txt = send_city + ", " + send_postcode,
                ln = 3, align = '')
            pdf.cell(100, 10, txt = " ",
                ln = 4, align = '')
            pdf.cell(100, 10, txt = " ",
                ln = 5, align = '')
            pdf.cell(100, 10, txt = "PO#: " + order,
                ln = 6, align = '')
            pdf.cell(100, 10, txt = " ",
                ln = 7, align = '')
            pdf.cell(100, 10, txt = str(label_list[style_colour]) + " " + str(label_list[style_colour + 1]),
                ln = 8, align = '')
            pdf.cell(100, 10, txt = " ",
                ln = 9, align = '')
            pdf.cell(100, 10, txt = rec_line1,
                ln = 10, align = '')
            pdf.cell(100, 10, txt = rec_line2,
                ln = 11, align = '')
            pdf.cell(100, 10, txt = rec_city + ", " + rec_postcode,
                ln = 12, align = '')
            pdf.cell(100,10, txt = " ",
                ln = 13, align = '')
            a = a + 1
        if a == total_labels[y]:
            print("Printing Next")
            y = y + 1
            a = int(0)
            style_colour = style_colour + 4
    print("Printing Complete....")        
    pdf.output(order + ".pdf")
main()
In your last post, I did do this in FPDF.

Don't put the addresses in by hand. You can use an Excel file or csv file as a simple database.

If you don't like inches, you can import cm or whatever, or use absolute numbers. I believe 1 unit on the page in reportlab is 1/72"

Story seems to get burned up when you use it, like csv.reader(), so I keep rewriting it.

This is using reportlab. Just change the x and y values to change the positions of the frames.

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph, Frame

styles = getSampleStyleSheet()
styleN = styles['Normal']
styleH = styles['Heading2']

# make the text

def writeStory():
    story = []    
    story.append(Paragraph("Mr/Ms Customer Name", styleH))    
    story.append(Paragraph("address line 1", styleN))
    story.append(Paragraph("address line 2", styleN))
    story.append(Paragraph("address line 3", styleN))
    return story

def makePage2(canvas, x, y):    
    f1 = Frame(x*inch, y*inch, 2.5*inch, 2*inch, showBoundary=1)
    story = writeStory()
    f1.addFromList(story,canvas)
    f2 = Frame(4*x*inch, y*inch, 2.5*inch, 2*inch, showBoundary=1)
    story = writeStory()
    f2.addFromList(story,canvas)
    f3 = Frame(x*inch, (y-3)*inch, 2.5*inch, 2*inch, showBoundary=1)
    story = writeStory()
    f3.addFromList(story,canvas)
    f4 = Frame(4*x*inch, (y-3)*inch, 2.5*inch, 2*inch, showBoundary=1)
    story = writeStory()
    f4.addFromList(story,canvas)
    f5 = Frame(x*inch, (y-6)*inch, 2.5*inch, 2*inch, showBoundary=1)
    story = writeStory()
    f5.addFromList(story,canvas)
    f6 = Frame(4*x*inch, (y-6)*inch, 2.5*inch, 2*inch, showBoundary=1)
    story = writeStory()
    f6.addFromList(story,canvas)

c = Canvas('/home/pedro/pdfs/mydoc1.pdf')
makePage2(c, 1, 9)
c.save()