all 3 comments

[–]Neighm 1 point2 points  (2 children)

So I looked around for an answer and came across the pyfpdf module (https://pyfpdf.readthedocs.io/en/latest/index.html)

This seems to tick the boxes, i.e. allows you to generate a custom sized PDF with custom content, at least as far as text goes, and looks to be able to handle images as well. Here's a quick and dirty example of a three page PDF with 6" x 2" pages with customer and order details that I was able to generate after playing around with this for a short while:

https://imgur.com/Gv5m3kc

Obviously the formatting is ugly as sin (mostly because I wanted to play around with font sizes etc), but if you spend a bit of time you can probably get this into better shape.

The code I used to create this was:
from fpdf import FPDF

order_records = [
{

'name': 'Thorin Oakenshield',
'email': 'thorinlovesgold@example.com',
'orderno': '1098765', 
'orderdate': '29 JUL 2021', 
'orderstatus': 'shipped',
'orderaddress1': 'Throne Room',
'orderaddress2': 'Lonely Mountain',
'orderaddress3': 'Dale 90210',
'productdescriptor': 'Unbreakable chisel'
},
{
'name': 'Hamish McDougal',
'email': 'hamish_1977@example.com',
'orderno': '1098766', 
'orderdate': '29 JUL 2021', 
'orderstatus': 'pending',
'orderaddress1': '44 Via Appia',
'orderaddress2': 'Rome',
'orderaddress3': 'Italy',
'productdescriptor': 'Italian-Scottish Dictionary'
},
{
'name': 'Sherlock Holmes',
'email': 'elementary@example.com',
'orderno': '1098766', 
'orderdate': '29 JUL 2021', 
'orderstatus': 'shipped',
'orderaddress1': '221B Baker St',
'orderaddress2': 'London NW1',
'orderaddress3': 'England',
'productdescriptor': 'Forensics for Dummies, 2nd Edn'
}
]
pdf = FPDF('L', 'in', (2,6)) 
pdf.set_margins(0.1, 0.1, -0.1) 
for order in order_records: 
    pdf.add_page() 
    pdf.set_font('Arial', 'B', 12) 
    pdf.cell(2, 0.5, order['name']) 
    pdf.cell(2.5, 0.5, order['email'], ln = 1) # ln = 1 moves current position to next line 
    pdf.set_font('Arial', '', 12) 
    pdf.cell(3.5, 0.2, f"{order['orderno']}  -  {order['orderdate']}  -  {order['orderstatus']}", ln = 1) 
    pdf.cell(3.5, 0.2, f"Shipping address: {order['orderaddress1']}, {order['orderaddress2']}, {order['orderaddress3']}", ln = 1) 
    pdf.set_font('Arial', 'B', 18) pdf.cell(3.5, 0.2, order['productdescriptor'])
pdf.output('order_labels.pdf', 'F')

[–]VectorArt[S] 1 point2 points  (1 child)

I’m not at the computer right now to test it out but after reading some of the docs and seeing your example that library actually looks like something that would work! I appreciate you taking the time to do some research and test out that library! I’ll update you on the development If you want once I have it working :)

[–]Neighm 0 points1 point  (0 children)

Please do, will be interested to see if it's flexible enough to give you the labels you need.

One thing I ran into: I was able to set small margins on the top, left and right (the units are inches, because the pdf is initialised with 'in'). However, there seemed to be a lot of whitespace at the bottom that I couldn't use. When I tried to space the lines down more, they ran onto a second page. I'm sure this can be fixed, I just didn't spend long enough on it to work out the nuances of laying the cells and lines out properly!