python reportlab barcode code128 size

Steffen picture Steffen · Feb 26, 2015 · Viewed 7.1k times · Source

i want to create a label (57*32mm) with a code128 barcode. Its all fine but the barcode is to small =( How can i make this barcode bigger ?

from reportlab.graphics.barcode import code128
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas

c = canvas.Canvas("test.pdf")
c.setPageSize((57*mm,32*mm))
barcode = code128.Code128("123456789")
barcode.drawOn(c, 2*mm, 20*mm)
c.showPage()
c.save()

Answer

Guillaume Proux picture Guillaume Proux · Jan 28, 2017

By looking up the source code for reportlab on GitHub, I found out that the barcode object has a width property. By simply using

canvas.saveState()
canvas.translate(2*cm, 3*cm) # bottom left corner of the barcode
canvas.scale(15*cm / barcode.width, 2*cm / barcode.height) # resize (15 cm and 2 cm)
barcode.drawOn(canvas, 0, 0)
canvas.restoreState()

You can obtain the obtain of the exact size you desire.