Code


Version 1 (modified by tturner, 3 years ago) (diff)

--

Creating bitmap Barcodes in Django with the ReportLab library

This describes a simple way to create dynamic bitmap Barcodes in Django.

ReportLab's PDF library is well known, but less people are aware that it contains a mature graphics package able to create most kinds of business charts, as well as custom graphical widgets.

Installation

You will need the ReportLab library from http://www.reportlab.com/

Also make sure you have compiled the _rl_accel and _renderPM packages. The latter is a sophisticated bitmap renderer with anti-aliasing, font handling and so on. ReportLab's download page has a checker script to tell you what extensions are installed.

Creating a Barcode

The reportlab/graphics framework provides a Drawing object and many Shape objects, which include primitive shapes, and high-level objects like charts, barcodes and legends. You can easily write your own too if you want to make dynamic bitmap buttons or fancy dashboard apps.

The usual pattern we follow is to create a Drawing class with all of your widgets, some sensible default data, and any visual settings you want. Here is the example of a Code128 barcode however it would be easy to create a code39 Barcode etc.

#    mybarcode.py  
from reportlab.graphics.shapes import Drawing, String
from reportlab.graphics.charts.barcharts import HorizontalBarChart

class MyBarcodeDrawing(Drawing):
    def __init__(self, text_value, *args, **kw):
        barcode = createBarcodeDrawing('Code128', value=text_value,  barHeight=10*mm, humanReadable=True)
        Drawing.__init__(self,barcode.width,barcode.height,*args,**kw)       
        self.add(barcode, name='barcode')
        

if __name__=='__main__':
    #use the standard 'save' method to save barcode.gif, barcode.pdf etc
    #for quick feedback while working.
    MyBarcodeDrawing("HELLO WORLD").save(formats=['gif','pdf'],outDir='.',fnRoot='barcode')

Paste this into a file and execute it; you should get 4 chart files written to disk in different formats.

Integrating into Django

Now we add a view to our views.py. This will examine the request for any dynamic parameters, since there's not much point serving a chart that doesn't vary. We'll allow the user to pass in 4 things as GET or POST parameters: a title, a comma-separated list of numbers, and the overall width and height of the image. Everything has a default in our Drawing class anyway, so we only pass through parameters which are present.

You then ask the Drawing to render itself to your favourite bitmap format and generate a response with the right content-type

def barcode(request):
    #instantiate a drawing object
    import mybarcode
    d = mybarcode.MyBarcodeDrawing("HELLO WORLD")
    binaryStuff = d.asString('gif')
    return HttpResponse(binaryStuff, 'image/gif')

Finally, you need a URL mapping. In this case I have added this:

    (r'^barcode/$', 'myapp.views.barcode'),

Now you can start Django, point your browser at the URL with no arguments, and should see the barcode.

http://localhost:8000/barcode/

Disclaimer

The author took this from the django Charts example. However he welcomes feedback.

Learning more about the chart library

The available chart types, widgets and properties are covered in the two Graphics manuals on this page. Enjoy!

http://www.reportlab.org/os_documentation.html

If you have questions about the barcodes rather than the Django integration, ask on the reportlab-users list:

http://two.pairlist.net/mailman/listinfo/reportlab-users