On Friday, 8 December 2017 02:18:32 UTC+1, edbo....@gmail.com  wrote:
> 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.

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.

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/f7d52ca2-981c-403f-bd4f-6a2a1f101a2f%40googlegroups.com.

Reply via email to