On Thursday, 7 December 2017 14:02:49 UTC+1, Sergi Almacellas Abellana wrote:
> El 07/12/17 a les 13:35, ha escrit:
> > Hello All,
> >
> > We want to add dynamically created images to a report, but we can't get it working :-( . It's a generated 2D-barcode based on a product code. We added a frame an added on tab Options as name image: (record.barcode, 'image/png'), but we only get a strange line and not a 2D-barcode.
> > We also added a binary-field to the product to see if that was going to work, but we get the same result.
> >
> > Does anybody got this working and can you provide an example?
>
> Which library are you using to generate the barcode?
Because we want 2D-barcode (QR-code or Datamatrix) we cannot use pyBarcode. We use treepoem [1] which is a wrapper around and postscript barcode generator
[1]
https://github.com/YPlan/treepoem
>
> We are using pyBarcode without problems. Here is the relevant code:
>
> import barcode
>
> from barcode.writer import ImageWriter>
> class Product:
> barcode = fields.Function(fields.Binary("Barcode",
> 'get_barcode')
> def get_barcode(self, name):
> fd = BytesIO()
> barcode.generate(
> 'ean13', self.code[:12].zfill(12),
> writer=ImageWriter(), output=fd)
> fd.seek(0)
> return fields.Binary.cast(fd.read())
> This should show without problems on the report.
I've tried something similar with treepoem:
def get_2dbarcode(self, name):
fd = io.BytesIO()
# generate 2D-barcode
image = treepoem.generate_barcode(
'datamatrix',
"THA00007", # normally this will be dynamic
{},
)
image.save(fd, format="png")
fd.seek(0)
return fields.Binary.cast(fd.read())
But no luck.