4

我想创建一个带有 code128 条形码的标签 (57*32mm)。一切都很好,但条形码太小=(我怎样才能让这个条形码更大?

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()
4

4 回答 4

3

条码的总宽度是 bar_width * total_char_widths + quiet space 所以..正确的 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)
于 2018-08-18T00:44:24.003 回答
3

通过在 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()

您可以获得所需的确切尺寸。

于 2017-01-28T04:16:02.420 回答
2

您可以通过 barHeight 和 barWidth 设置条码大小:

barcode = code128.Code128("123456789",barHeight=.9*inch,barWidth = 1.2)
于 2015-03-18T16:08:48.293 回答
1

我终于用其他不工作的代码片段找到了答案

from reportlab.graphics.barcode import code128
from reportlab.lib.units import mm, inch, cm, pica
from reportlab.pdfgen import canvas

code = "asdasda" #remove this if in function
c = canvas.Canvas(f"{code}.pdf")
page_width = 550  # page width
# add specific unit here as x= num*unit
# pica,mm and no unit works, I don't know why the 2 other don't
page_height = 200  # page height
# add specific unit here as x= num*unit
# pica,mm and no unit works, I don't know why the 2 other don't
margin_y = 10  # top/bottom margin
# add specific unit here as x= num*unit
# pica,mm and no unit works, I don't know why the 2 other don't

bar_height = page_height - (margin_y * 2)  # barcode line height

bar_width = page_width / (11 * len(str(code)) + 55)  # barcode individual width has the formula
# page width / (11*string_length) + 55   ##(I also saw +35 but in my test it was not working)


c.setPageSize((page_width, page_height))  # set page to said mesure
humanReadable = True  # with or without text
barcode = code128.Code128(code,
                          barHeight=bar_height,
                          barWidth=bar_width,
                          humanReadable=humanReadable)

drawon_x = 0  # x value for drawing already has a margin (not like Y) bar with formula account for that
if humanReadable:
    drawon_y = page_height - margin_y - bar_height  # if text reduce bar height to hace the correct value
else:
    drawon_y = page_height - bar_height  # set draw point to the top of the page - the height of the drawn barcode
barcode.drawOn(c, drawon_x, drawon_y)  # do the drawing

c.save()  # save pdf

如果您想要多个单独的条形码传递这样的代码列表

def createSinglePDFBarcode(code):
    #code from above here

if __name__ == "__main__":
    import random
    import string

    num = 5

    # Generate {num} random numbers between 10 and 30
    # printing uppercase
    letters = string.ascii_uppercase
    randomSList =[] #list of code you want as barcode
    for x in range(num):
        randomSList.append(''.join(random.choice(letters) for i in range(10)))
    for x in randomSList: # for each code make a barcode
        createSinglePDFBarcode(x) 
于 2021-12-27T22:46:02.700 回答