Created
January 5, 2015 20:14
-
-
Save artschwagerb/6f3b3d20d85a5b9abd7b to your computer and use it in GitHub Desktop.
barcode model property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from base64 import b64encode | |
from reportlab.lib import units | |
from reportlab.graphics import renderPM | |
from reportlab.graphics.barcode import createBarcodeDrawing | |
from reportlab.graphics.shapes import Drawing | |
class barcode: | |
def get_barcode(self, value, width, barWidth = 0.05 * units.inch, fontSize = 30, humanReadable = True): | |
barcode = createBarcodeDrawing('Code128', value = value, barWidth = barWidth, fontSize = fontSize, humanReadable = humanReadable) | |
drawing_width = width | |
barcode_scale = drawing_width / barcode.width | |
drawing_height = barcode.height * barcode_scale | |
drawing = Drawing(drawing_width, drawing_height) | |
drawing.scale(barcode_scale, barcode_scale) | |
drawing.add(barcode, name='barcode') | |
return b64encode(renderPM.drawToString(drawing, fmt = 'PNG')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@property | |
def barcode(self): | |
from barcode import barcode as barcode | |
data = barcode().get_barcode(value = str(self.id), width = 600) | |
return u'data:image/png;base64,%s' % (data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment