Changes between Initial Version and Version 1 of Barcodes

06/06/2011 06:46:55 AM (8 years ago)



  • Barcodes

    v1 v1  
     1= Creating bitmap Barcodes in Django with the !ReportLab library =
     3This describes a simple way to create dynamic bitmap Barcodes in Django. 
     5!ReportLab's PDF library is well known, but less people are aware that it contains a
     6mature graphics package able to create most kinds of business charts, as well as custom graphical widgets.
     8= Installation =
     9You will need the !ReportLab library from
     11Also make sure you have compiled the ''_rl_accel'' and ''_renderPM'' packages.  The latter
     12is a sophisticated bitmap renderer with anti-aliasing, font handling and so on.
     13!ReportLab's download page has a checker script to tell you what extensions
     14are installed.
     16= Creating a Barcode =
     17The reportlab/graphics framework provides a Drawing object and many Shape objects,
     18which include primitive shapes, and high-level objects like charts, barcodes and legends.  You
     19can easily write your own too if you want to make dynamic bitmap buttons or fancy
     20dashboard apps.
     22The usual pattern we follow is to create a Drawing class with all of your widgets,
     23some 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.
     27from import Drawing, String
     28from import HorizontalBarChart
     30class MyBarcodeDrawing(Drawing):
     31    def __init__(self, text_value, *args, **kw):
     32        barcode = createBarcodeDrawing('Code128', value=text_value,  barHeight=10*mm, humanReadable=True)
     33        Drawing.__init__(self,barcode.width,barcode.height,*args,**kw)       
     34        self.add(barcode, name='barcode')
     37if __name__=='__main__':
     38    #use the standard 'save' method to save barcode.gif, barcode.pdf etc
     39    #for quick feedback while working.
     40    MyBarcodeDrawing("HELLO WORLD").save(formats=['gif','pdf'],outDir='.',fnRoot='barcode')
     44Paste this into a file and execute it; you should get 4 chart files written to
     45disk in different formats.
     47= Integrating into Django =
     49Now we add a view to our ''''.  This will examine the request
     50for any dynamic parameters, since there's not much point serving
     51a chart that doesn't vary.  We'll allow the user to pass in 4
     52things as GET or POST parameters:  a title, a comma-separated list of numbers,
     53and the overall width and height of the image.  Everything has a default in
     54our Drawing class anyway, so we only pass through parameters which are
     57You then ask the Drawing to render itself to your favourite bitmap format
     58and generate a response with the right content-type
     61def barcode(request):
     62    #instantiate a drawing object
     63    import mybarcode
     64    d = mybarcode.MyBarcodeDrawing("HELLO WORLD")
     65    binaryStuff = d.asString('gif')
     66    return HttpResponse(binaryStuff, 'image/gif')
     70Finally, you need a URL mapping.  In this case I have added this:
     73    (r'^barcode/$', 'myapp.views.barcode'),
     77Now you can start Django, point your browser at the URL with no arguments,
     78and should see the barcode. 
     79  http://localhost:8000/barcode/
     82= Disclaimer =
     83The author took this from the django [wiki:Charts] example. However he welcomes feedback.
     85= Learning more about the chart library =
     86The available chart types, widgets and properties are covered in the two
     87Graphics manuals on this page.  Enjoy!
     90If you have questions about the barcodes rather than the Django integration,
     91ask on the reportlab-users list:
Back to Top