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

2024-04-25 06:07:01 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我今天创建了一个脚本,它使用一个项目或任何数字来生成条形码。现在我想在4列中打印60个相同的条形码,这将使它成为一个(15×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

问题是,现在我只能使用美国字母大小,没有其他定制尺寸。我尝试了很少的变化,但没有成功。在

尝试:

^{pr2}$

并且here是它必须适合的必需格式。能帮忙就好了。在


Tags: 项目fromimport脚本value数字barcodecanvas
1条回答
网友
1楼 · 发布于 2024-04-25 06:07:01

你的代码需要很多改进

  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. 计算生成的代码条的总大小

    ^{2美元
  4. 传递给函数Code128的width值是codebar中单个条的宽度,而不是整个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()
    

相关问题 更多 >