我想创建一个带有code128条形码的标签(57*32 to )。这一切都很好,但条形码太小=(我如何才能使这个条形码更大?
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()
发布于 2017-01-28 12:16:02
通过在GitHub上查找reportlab的源代码,我发现这个条形码对象有一个宽度属性。通过简单地使用
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()
你可以获得你想要的确切大小。
发布于 2015-03-19 00:08:48
您可以通过barHeight和barWidth来设置条码大小:
barcode = code128.Code128("123456789",barHeight=.9*inch,barWidth = 1.2)
发布于 2018-08-18 08:44:24
条形码的总宽度是bar_width * total_char_widths +静音空间,所以..可以通过以下方式确定正确的barWidth
from reportlab.graphics.barcode import code128
final_size = 100 # arbitrary
# setting barWidth to 1
initial_width = .1
barcode128 = code128.Code128(barcode_value, humanReadable=True, barWidth=initial_width,
barHeight=1)
# creates the barcode, computes the total size
barcode128._calculate()
# the quiet space before and after the barcode
quiet = barcode128.lquiet + barcode128.rquiet
# total_wid = barWidth*charWid + quiet_space
# char_wid = (total_width - quiet) / bar_width
char_width = (barcode128._width - quiet) / barcode128.barWidth
# now that we have the char width we can calculate the bar width
bar_width = (final_size - quiet) / char_width
# set the new bar width
barcode128.barWidth = bar_width
# re-calculate
barcode128._calculate()
# draw the barcode on the canvas
wid, hgt = barcode128._width, barcode128._height
x_pos = y_pos = final_size # arbitrary
barcode128.drawOn(your_canvas, x_pos, y_pos)
https://stackoverflow.com/questions/28745703
复制相似问题