Adding images in reports

175 views
Skip to first unread message

edbo....@gmail.com

unread,
Dec 7, 2017, 7:55:08 AM12/7/17
to tryton-dev
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?

Thanks!

Sergi Almacellas Abellana

unread,
Dec 7, 2017, 8:02:49 AM12/7/17
to tryto...@googlegroups.com
El 07/12/17 a les 13:35, edbo....@gmail.com ha escrit:
Which library are you using to generate the barcode?

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",

states={

'invisible': ~Eval('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.

[1] https://pypi.python.org/pypi/pyBarcode/0.8b1


--
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

edbo....@gmail.com

unread,
Dec 7, 2017, 8:18:32 PM12/7/17
to tryton-dev
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.

edbo....@gmail.com

unread,
Dec 8, 2017, 3:50:07 AM12/8/17
to tryton-dev
Got, it. It seems that you have to anchor the frame to a paragraph and not to the page or character. So, to sum up:

# import the libraries
import io
import treepoem

# add a field to your model which generates the barcode
barcode = fields.Function(fields.Binary("Barcode", 'get_2dbarcode')

# define your function which creates the barcode
def get_2dbarcode(self, name):
fd = io.BytesIO()
# generate 2D-barcode
image = treepoem.generate_barcode(
'datamatrix',
self.code,
{},
)
image.save(fd, format="png")
fd.seek(0)
return fields.Binary.cast(fd.read())

And in your report:
Add a frame and make sure:
1. Anchor is on paragraph (tab "Type")
2. Name is (<your_record.barcode>, "image/png") (tab "Options")

I'm going to remove the field, because the code is auto-generated on first save action and never changes.

Maxime Richez

unread,
Feb 19, 2018, 10:48:03 AM2/19/18
to tryton-dev

> import barcode
>
> from barcode.writer import ImageWriter
>
>
> class Product:
>
> barcode = fields.Function(fields.Binary("Barcode",
>
> states={
>
> 'invisible': ~Eval('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.
>
> [1] https://pypi.python.org/pypi/pyBarcode/0.8b1

Hi,

I try your code to generate a barcode on a report.
I had to install "Pillow" (pip install Pillow) to avoid an NoneType error.
How display this new field 'barcode' inside LibreOffice writer ? I try to add a frame but don't know how to link my binary field and relatorio documentation seems outdated...

Maxime Richez

unread,
Feb 19, 2018, 10:57:35 AM2/19/18
to tryton-dev

> Hi,
>
> I try your code to generate a barcode on a report.
> I had to install "Pillow" (pip install Pillow) to avoid an NoneType error.
> How display this new field 'barcode' inside LibreOffice writer ? I try to add a frame but don't know how to link my binary field and relatorio documentation seems outdated...

Found the solution :-)
I add a frame and in the Options tab and in the name field, i set:

image: (your_record.barcode, "image/png")

Reply all
Reply to author
Forward
0 new messages