| 1 | = Creating bitmap Barcodes in Django with the !ReportLab library = |
| 2 | |
| 3 | This 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 |
| 6 | mature graphics package able to create most kinds of business charts, as well as custom graphical widgets. |
| 7 | |
| 8 | = Installation = |
| 9 | You will need the !ReportLab library from http://www.reportlab.com/ |
| 10 | |
| 11 | Also make sure you have compiled the ''_rl_accel'' and ''_renderPM'' packages. The latter |
| 12 | is 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 |
| 14 | are installed. |
| 15 | |
| 16 | = Creating a Barcode = |
| 17 | The reportlab/graphics framework provides a Drawing object and many Shape objects, |
| 18 | which include primitive shapes, and high-level objects like charts, barcodes and legends. You |
| 19 | can easily write your own too if you want to make dynamic bitmap buttons or fancy |
| 20 | dashboard apps. |
| 21 | |
| 22 | The usual pattern we follow is to create a Drawing class with all of your widgets, |
| 23 | 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. |
| 24 | |
| 25 | {{{ |
| 26 | # mybarcode.py |
| 27 | from reportlab.graphics.shapes import Drawing, String |
| 28 | from reportlab.graphics.charts.barcharts import HorizontalBarChart |
| 29 | |
| 30 | class 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 | |
| 37 | if __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 | |
| 44 | Paste this into a file and execute it; you should get 4 chart files written to |
| 45 | disk in different formats. |
| 46 | |
| 47 | = Integrating into Django = |
| 48 | |
| 49 | Now we add a view to our ''views.py''. This will examine the request |
| 50 | for any dynamic parameters, since there's not much point serving |
| 51 | a chart that doesn't vary. We'll allow the user to pass in 4 |
| 52 | things as GET or POST parameters: a title, a comma-separated list of numbers, |
| 53 | and the overall width and height of the image. Everything has a default in |
| 54 | our Drawing class anyway, so we only pass through parameters which are |
| 55 | present. |
| 56 | |
| 57 | You then ask the Drawing to render itself to your favourite bitmap format |
| 58 | and generate a response with the right content-type |
| 59 | |
| 60 | {{{ |
| 61 | def 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 | |
| 70 | Finally, you need a URL mapping. In this case I have added this: |
| 71 | |
| 72 | {{{ |
| 73 | (r'^barcode/$', 'myapp.views.barcode'), |
| 74 | |
| 75 | }}} |
| 76 | |
| 77 | Now you can start Django, point your browser at the URL with no arguments, |
| 78 | and should see the barcode. |
| 79 | http://localhost:8000/barcode/ |
| 80 | |
| 81 | |
| 82 | = Disclaimer = |
| 83 | The author took this from the django [wiki:Charts] example. However he welcomes feedback. |
| 84 | |
| 85 | = Learning more about the chart library = |
| 86 | The available chart types, widgets and properties are covered in the two |
| 87 | Graphics manuals on this page. Enjoy! |
| 88 | http://www.reportlab.org/os_documentation.html |
| 89 | |
| 90 | If you have questions about the barcodes rather than the Django integration, |
| 91 | ask on the reportlab-users list: |
| 92 | http://two.pairlist.net/mailman/listinfo/reportlab-users |