Changes between Initial Version and Version 1 of Barcodes


Ignore:
Timestamp:
Jun 6, 2011, 6:46:55 AM (14 years ago)
Author:
tturner
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Barcodes

    v1 v1  
     1= Creating bitmap Barcodes in Django with the !ReportLab library =
     2
     3This describes a simple way to create dynamic bitmap Barcodes in Django. 
     4
     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.
     7
     8= Installation =
     9You will need the !ReportLab library from http://www.reportlab.com/
     10
     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.
     15
     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.
     21
     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.
     24
     25{{{
     26#    mybarcode.py 
     27from reportlab.graphics.shapes import Drawing, String
     28from reportlab.graphics.charts.barcharts import HorizontalBarChart
     29
     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')
     35       
     36
     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')
     41}}}
     42
     43
     44Paste this into a file and execute it; you should get 4 chart files written to
     45disk in different formats.
     46
     47= Integrating into Django =
     48
     49Now we add a view to our ''views.py''.  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
     55present.
     56
     57You then ask the Drawing to render itself to your favourite bitmap format
     58and generate a response with the right content-type
     59
     60{{{
     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')
     67
     68}}}
     69
     70Finally, you need a URL mapping.  In this case I have added this:
     71
     72{{{
     73    (r'^barcode/$', 'myapp.views.barcode'),
     74
     75}}}
     76
     77Now you can start Django, point your browser at the URL with no arguments,
     78and should see the barcode. 
     79  http://localhost:8000/barcode/
     80
     81
     82= Disclaimer =
     83The author took this from the django [wiki:Charts] example. However he welcomes feedback.
     84
     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!
     88   http://www.reportlab.org/os_documentation.html
     89
     90If you have questions about the barcodes rather than the Django integration,
     91ask on the reportlab-users list:
     92  http://two.pairlist.net/mailman/listinfo/reportlab-users
Back to Top