Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Label Maker FPDF, Reportlab
#1
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()
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#2
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()
jamesaarr likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 621 Aug-23-2023, 01:21 PM
Last Post: NeilUK
  Help with flowchart maker program bluebouncyball 2 768 Aug-08-2023, 10:25 PM
Last Post: deanhystad
  Right to left alignment in python report using Reportlab jalal0034 1 1,847 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  fpdf orientation not working properly KatMac 1 3,350 May-02-2021, 10:47 AM
Last Post: Pedroski55
  fpdf star character issue KatMac 3 3,008 May-01-2021, 06:22 PM
Last Post: KatMac
  fpdf adding a new font to my report KatMac 0 2,184 Apr-23-2021, 02:19 PM
Last Post: KatMac
  ModuleNotFoundError: No module named 'fpdf' KatMac 4 11,207 Apr-19-2021, 01:23 PM
Last Post: KatMac
  FPDF question DPaul 2 2,792 Oct-27-2020, 08:26 AM
Last Post: DPaul
  cyrillic symbols in tables in reportlab. hiroz 5 11,492 Sep-10-2020, 04:57 AM
Last Post: bradmalcom
  Using Reportlab to create a landscape pdf SmukasPlays 2 5,458 Aug-09-2020, 09:31 PM
Last Post: SmukasPlays

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020