Page size issue PDF creation of barcodes using reportlab

只谈情不闲聊 提交于 2020-01-03 06:15:50

问题


Okay i created a script today that takes an item# or any number for that purpose to generate a barcode. Now i wanted to print 60 same barcodes in 4 columns, that would make it a matrix of (15 X 4) only making it easy to understand. Now i successfully achieved it with a custom sized page (900*850) and fitted in 15 rows and 4 columns of barcodes generated by reportlab code128.

Here is the code:

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

The issue is that now I am restricted to using only US Letter size and no other custom size. I tried few variations but none worked.

Tries:

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

and here is the required format that it must fit in. Would be nice to have to help.


回答1:


You code requires a lot of improvement

  1. Use the letter size from reportlab.lib.pagesizes import letter
  2. Set your margins and other variables as specified in the document :

    margin_x        =  7.526
    margin_y        =  13.876
    padding_x       =  7.526
    font_size       =  15
    width, height   =  letter
    
  3. Calculate the total size of the generated codebar

    bars_width   = (float(width-margin_x*2)-3*padding_x)/4
    bars_height  = float(height-margin_y*2)/15
    
  4. The width value passed to the function Code128 is the width of a single bar within the codebar and not the whole codebar, you should keep this value below 1.1

    bar_height   = bars_height - font_size
    bar_width    = 1
    
  5. your loops would be better this way :

    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. this is the final script :

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


来源:https://stackoverflow.com/questions/44727229/page-size-issue-pdf-creation-of-barcodes-using-reportlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!