【问题标题】:python reportlab barcode code128 sizepython reportlab条码code128大小
【发布时间】:2015-04-29 00:31:33
【问题描述】:

我想创建一个带有 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()

【问题讨论】:

  • 补充:尺寸应该是固定的。所以不同值的条码大小应该是一样的。

标签: python-3.x barcode reportlab code128


【解决方案1】:

通过在GitHub上查找reportlab的源代码,我发现barcode对象有一个width属性。 只需使用

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

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

【讨论】:

    【解决方案2】:

    条码的总宽度为 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)
    

    【讨论】:

      【解决方案3】:

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

      barcode = code128.Code128("123456789",barHeight=.9*inch,barWidth = 1.2)
      

      【讨论】:

      • 嗨,真的没有办法将条形码设置为固定大小吗??
      【解决方案4】:

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

      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) 
      

      【讨论】:

        猜你喜欢
        • 2019-03-13
        • 2017-06-19
        • 1970-01-01
        • 2023-04-11
        • 2017-01-09
        • 2013-02-03
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多