Reportlab - How to Create Barcodes in Your PDFs with Python

The Reportlab library is a great way to generate PDFs in Python. Recently, I noticed that it has the ability to do barcodes. I had heard about it being able to generate QR codes, but I hadn't really dug under the covers to see what else it could do. In this tutorial, we'll take a look at some of the barcodes that Reportlab can generate. If you don't already have Reportlab, go to their website and get it before jumping into the article.

Reportlab's Barcode Library

Reportlab provides for several different types of bar codes: code39 (i.e. code 3 of 9), code93, code 128, EANBC, QR, and USPS. I saw one called "fourstate" as well, but I couldn't figure out how to get it to work. Underneath some of these types, there are sub-types such as Standard, Extended or MultiWidth. I didn't have much luck getting the MultiWidth one to work for the code128 bar code as it kept giving me an attribute error, so we'll just ignore that one. If you know how to do it, ping me in the comments or via my contact form and let me know. I'll update the article if anyone can show me how to add that or the fourstate barcode.

Anyway, the best way to learn is to just write some code. Here's a pretty straight-forward example:

from reportlab.graphics.barcode import code39, code128, code93
from reportlab.graphics.barcode import eanbc, qr, usps
from reportlab.graphics.shapes import Drawing 
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF

#----------------------------------------------------------------------
def createBarCodes():
    """
    Create barcode examples and embed in a PDF
    """
    c = canvas.Canvas("barcodes.pdf", pagesize=letter)
    
    barcode_value = "1234567890"
    
    barcode39 = code39.Extended39(barcode_value)
    barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1)
    
    # code93 also has an Extended and MultiWidth version
    barcode93 = code93.Standard93(barcode_value)
    
    barcode128 = code128.Code128(barcode_value)
    # the multiwidth barcode appears to be broken 
    #barcode128Multi = code128.MultiWidthBarcode(barcode_value)
    
    barcode_usps = usps.POSTNET("50158-9999")
    
    codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps]
    
    x = 1 * mm
    y = 285 * mm
    x1 = 6.4 * mm
    
    for code in codes:
        code.drawOn(c, x, y)
        y = y - 15 * mm
    
    # draw the eanbc8 code
    barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value)
    bounds = barcode_eanbc8.getBounds()
    width = bounds[2] - bounds[0]
    height = bounds[3] - bounds[1]
    d = Drawing(50, 10)
    d.add(barcode_eanbc8)
    renderPDF.draw(d, c, 15, 555)
    
    # draw the eanbc13 code
    barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value)
    bounds = barcode_eanbc13.getBounds()
    width = bounds[2] - bounds[0]
    height = bounds[3] - bounds[1]
    d = Drawing(50, 10)
    d.add(barcode_eanbc13)
    renderPDF.draw(d, c, 15, 465)
    
    # draw a QR code
    qr_code = qr.QrCodeWidget('www.mousevspython.com')
    bounds = qr_code.getBounds()
    width = bounds[2] - bounds[0]
    height = bounds[3] - bounds[1]
    d = Drawing(45, 45, transform=[45./width,0,0,45./height,0,0])
    d.add(qr_code)
    renderPDF.draw(d, c, 15, 405)
    
    c.save()
    
if __name__ == "__main__":
    createBarCodes()

Let's break this down a bit. The code39.Extended39 doesn't really accept much beyond the value itself. On the other hand, code39.Standard39, code93.Standard93 and code128.Code128 all have basically the same API. You can change the barWidth, barHeight, turn on the start/stop symbols and add "quiet" zones. The usps bar code module provides two types of bar code: FIM and POSTNET. FIM or Facing ID Marks only encodes one letter (A-D) which I personally didn't find it very interesting. So I just show the POSTNET version which should be pretty familiar to people in the United States as it appears on the bottom of most envelopes. POSTNET encodes the zip code!

The next three bar codes use a different API to draw them on the PDF that I discovered via StackOverflow. Basically you create a Drawing object of a certain size and then add the bar code to the drawing. Finally you use the renderPDF module to place the drawing on the PDF. It's pretty convoluted, but it works pretty well. The EANBC codes are ones you'll see on some manufactured products, such as tissue boxes.

If you'd like to see the result of the code above, you can download the PDF here.

Wrapping Up

At this point you should be able to go forth and create your own bar codes in your PDFs. Reportlab is pretty handy and I hope you'll find this additional tool helpful in your endeavors.

Additional Reading

Get the Source!

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary