


How to Create Barcodes in Your PDFs with Python

  • submit to reddit


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 import code39, code128, code93
from import eanbc, qr, usps
from import Drawing 
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from 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)
    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)
    renderPDF.draw(d, c, 15, 465)
    # draw a QR code
    qr_code = qr.QrCodeWidget('')
    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])
    renderPDF.draw(d, c, 15, 405)
if __name__ == "__main__":

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 viaStackOverflow. 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!




Shayan Elhami replied on Wed, 2013/05/22 - 7:49am

Why is this article in BIG DATA zone?

Comment viewing options
