页面大小问题 PDF 使用 reportlab 创建条形码

Page size issue PDF creation of barcodes using reportlab

好的,我今天创建了一个脚本,该脚本使用项目# 或任何数字来生成条形码。现在我想在 4 列中打印 60 个相同的条形码,这将使它成为一个 (15 X 4) 的矩阵,只使其易于理解。现在我用一个自定义大小的页面 (900*850) 成功地实现了它,并安装了由 reportlab code128 生成的 15 行和 4 列条形码。

代码如下:

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

#----------------------------------------------------------------------#
def createBarCodes():
    codeName = "NOT_C17"

    c = canvas.Canvas(codeName+".pdf")
    c.setPageSize((900, 850))

    barcode_value = codeName
    barcode128 = code128.Code128(
                            barcode_value,
                            barHeight=20,
                            barWidth=1.05,
                            fontSize=15,
                            humanReadable = True
                        )

    x = 15 * mm
    for i in range(4):
        y = 275 * mm
        i=0
        while i < 15:
            barcode128.drawOn(c, x, y)
            y = y - 18 * mm
            i+=1
        x=x+(70*mm)
    c.save()

if __name__ == "__main__":
    createBarCodes()

A file generated by this script

问题是现在我只能使用美国信纸尺寸,不能使用其他自定义尺寸。我尝试了一些变体,但 none 奏效了。

尝试次数:

from reportlab.lib.pagesizes import letter
c = canvas.Canvas(codeName+".pdf", pagesize=letter)

barcode_value = codeName
barcode128 = code128.Code128(
                          barcode_value,
                          barHeight=16.7564*mm,
                          barWidth=44.45*mm,
                          fontSize=15,
                          humanReadable = True
                     )

x = 7.526 * mm
for i in range(4):
    y = 265.524 * mm
    i=0
    while i < 15:
        barcode128.drawOn(c, x, y)
        y = y - 18 * mm
        i+=1
        break
    x=x+(70*mm)
    break
c.save()

here 是它必须符合的必需格式。能提供帮助会很高兴。

您的代码需要大量改进

  1. 使用字母大小from reportlab.lib.pagesizes import letter
  2. 按照文档中的说明设置边距和其他变量:

    margin_x        =  7.526
    margin_y        =  13.876
    padding_x       =  7.526
    font_size       =  15
    width, height   =  letter
    
  3. 计算生成codebar的总大小

    bars_width   = (float(width-margin_x*2)-3*padding_x)/4
    bars_height  = float(height-margin_y*2)/15
    
  4. 传递给函数Code128的宽度值是codebar中单个bar的宽度,而不是整个codebar,你应该把这个值保持在1.1以下

    bar_height   = bars_height - font_size
    bar_width    = 1
    
  5. 这样你的循环会更好:

    for i in range(0,4):
        for j in range(0,15):
            x = margin_x + i * (bars_width+padding_x)
            y = margin_y + j * bars_height
            barcode128.drawOn(c, x , y)
    
  6. 这是最终脚本:

    from reportlab.graphics.barcode import code128
    from reportlab.lib.units import mm
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    
    #----------------------------------------------------------------------#
    def createBarCodes():
        codeName = "NOT_C17"
    
        c = canvas.Canvas(codeName+".pdf",pagesize=letter)
    
        margin_x        =  7.526
        margin_y        =  13.876
        padding_x       =  7.526
        font_size       =  15
        width, height   =  letter
        extra_padding   =  20
    
        bars_width   = (float(width-margin_x*2)-3*padding_x)/4
        bars_height  = float(height-margin_y*2)/15
    
        bar_height   = bars_height - font_size
        #For alphanumeric values, the total number of bars is calculated as:
        #total = (11*string_length+35)
        bar_width    = (bars_width-extra_padding)/(len(codeName)*11+35)
    
    
        barcode_value = codeName
        barcode128 = code128.Code128(
                                barcode_value,
                                barHeight=bar_height,
                                barWidth=bar_width,
                                humanReadable = True
                            )
    
        for i in range(0,4):
            for j in range(0,15):
                x = margin_x + i * (bars_width+padding_x)
                y = margin_y + j * bars_height
                barcode128.drawOn(c, x , y)
    
        c.save()
    
    if __name__ == "__main__":
        createBarCodes()