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.lib.units import mm from reportlab.graphics.barcode import createBarcodeDrawing 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. At the moment this function will print a barcode with hello world on it. You can modify this view function so it passes the text you want to print on the barcode.
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.
Disclaimer
The author took this from the django Charts example. However he welcomes feedback.
Learning more about the barcode library
The available chart types, widgets and properties are covered in the two Graphics manuals on this page. Enjoy!
If you have questions about the barcodes rather than the Django integration, ask on the reportlab-users list: