How to get the Report in Byte?

40 views
Skip to first unread message

Markus Bala

unread,
Sep 17, 2018, 4:21:39 AM9/17/18
to tryton-dev
Hi,

Actually I have 3 questions, below is my case:

I will doing barcode label for some warehouse operation.

Case Study:
At the receiving area, during receiving & storage, it will need to print 2 labels. One label is product label. One label is storage label. The different between 2 labels are in the label's size.
Product label using 5cm x 10cm. Storage label using 15cm x 10cm.

Because of this, I will have 2 labels printer. Printer (A) for product label. Printer (B) for storage Label.

Question 1: for "Direct Print", how can set when I click on print "Product Label" it choose Printer (A) and vice versa.

Or alternative solution:
Making the printer module. Like the link below:

https://groups.google.com/forum/#!searchin/tryton-dev/printer|sort:date/tryton-dev/1Hu0AE5bjd8/0zCHyxkPq3EJ

Question 2: How to get "Tryton Client" IP from "Trytond"? the purpose to fixed the IP/PC for the label printers so other PC do not have label printer

Question 3: How to get report in byte in trytond so I can print direct to printer through network. Like the link below

https://www.zebra.com/us/en/support-downloads/knowledge-articles/ait/Network-Printing-Python-Example.html


Please help

Cédric Krier

unread,
Sep 21, 2018, 4:20:13 AM9/21/18
to tryton-dev
On 2018-09-17 01:21, Markus Bala wrote:
> Actually I have 3 questions, below is my case:
>
> I will doing barcode label for some warehouse operation.
>
> Case Study:
> At the receiving area, during receiving & storage, it will need to print 2 labels. One label is product label. One label is storage label. The different between 2 labels are in the label's size.
> Product label using 5cm x 10cm. Storage label using 15cm x 10cm.
>
> Because of this, I will have 2 labels printer. Printer (A) for product label. Printer (B) for storage Label.
>
> Question 1: for "Direct Print", how can set when I click on print "Product Label" it choose Printer (A) and vice versa.

On windows, the client use the command 'print' which let the OS decide.
So maybe you can use different file extension and configure the OS to
choose the right printer.
On MacOS, the client uses the 'open' command which just open the file.
Maybe it is possible to configure the "opener" to select the right
printer when requesting to print.
On UNIX, it uses the 'xdg-open' command. In this case, I guess you can
also customize xdg to select the right printer from the mime-type.

> Or alternative solution:
> Making the printer module. Like the link below:
>
> https://groups.google.com/forum/#!searchin/tryton-dev/printer|sort:date/tryton-dev/1Hu0AE5bjd8/0zCHyxkPq3EJ

Not sure it is the best option because it requires a connection between
the server and the printer. Usually they are not in the same physical
area.

> Question 2: How to get "Tryton Client" IP from "Trytond"? the purpose to fixed the IP/PC for the label printers so other PC do not have label printer

Since 4.8, the Transaction.context has an attribute '_request' which is
the werkzeug request object and contain the IP address of the client.
But there is no guarantee that this attribute will always be set (for
example from proteus request or cron job).

> Question 3: How to get report in byte in trytond so I can print direct to printer through network. Like the link below
>
> https://www.zebra.com/us/en/support-downloads/knowledge-articles/ait/Network-Printing-Python-Example.html

You can get the Report class from the pool and call execute on it. This
will return the bytes as second element of the response.

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Markus Bala

unread,
Sep 21, 2018, 4:35:14 AM9/21/18
to tryton-dev
On Friday, September 21, 2018 at 8:20:13 AM UTC, Cédric Krier wrote:
> On 2018-09-17 01:21, Markus Bala wrote:
> > Actually I have 3 questions, below is my case:
> >
> > I will doing barcode label for some warehouse operation.
> >
> > Case Study:
> > At the receiving area, during receiving & storage, it will need to print 2 labels. One label is product label. One label is storage label. The different between 2 labels are in the label's size.
> > Product label using 5cm x 10cm. Storage label using 15cm x 10cm.
> >
> > Because of this, I will have 2 labels printer. Printer (A) for product label. Printer (B) for storage Label.
> >
> > Question 1: for "Direct Print", how can set when I click on print "Product Label" it choose Printer (A) and vice versa.
>
> On windows, the client use the command 'print' which let the OS decide.
> So maybe you can use different file extension and configure the OS to
> choose the right printer.
> On MacOS, the client uses the 'open' command which just open the file.
> Maybe it is possible to configure the "opener" to select the right
> printer when requesting to print.
> On UNIX, it uses the 'xdg-open' command. In this case, I guess you can
> also customize xdg to select the right printer from the mime-type.

Reply: Thank, I never thought of this method. I will try this solution.
>
> > Or alternative solution:
> > Making the printer module. Like the link below:
> >
> > https://groups.google.com/forum/#!searchin/tryton-dev/printer|sort:date/tryton-dev/1Hu0AE5bjd8/0zCHyxkPq3EJ
>
> Not sure it is the best option because it requires a connection between
> the server and the printer. Usually they are not in the same physical
> area.

Reply: This will be alternative. I will try on the solution no 1.
>
> > Question 2: How to get "Tryton Client" IP from "Trytond"? the purpose to fixed the IP/PC for the label printers so other PC do not have label printer
>
> Since 4.8, the Transaction.context has an attribute '_request' which is
> the werkzeug request object and contain the IP address of the client.
> But there is no guarantee that this attribute will always be set (for
> example from proteus request or cron job).

Nice. I have been looking this. Missed to check the transaction.context.
Thank you
>
> > Question 3: How to get report in byte in trytond so I can print direct to printer through network. Like the link below
> >
> > https://www.zebra.com/us/en/support-downloads/knowledge-articles/ait/Network-Printing-Python-Example.html
>
> You can get the Report class from the pool and call execute on it. This
> will return the bytes as second element of the response.

Could you give me example on this? Iam litte bit confuse.

Currently, I am using Relatio. It should be a dirty hack. I hope can use the method directly from Trytond instead.

Below is my code:

from relatorio import Report

class BarcodeLabel(Workflow, ModelSQL, ModelView):

@classmethod
def print_lpn(cls, records):
report = Report(abspath(join(dirname(__file__), 'lpn.odt')),
ODT_MIME)
data = report(records=records).render().getvalue()
file_name = join(dirname(__file__), 'output_basic.odt')
open(file_name, 'wb').write(data)

Cédric Krier

unread,
Sep 21, 2018, 4:54:10 AM9/21/18
to tryton-dev
On 2018-09-21 01:35, Markus Bala wrote:
> > > Question 3: How to get report in byte in trytond so I can print direct to printer through network. Like the link below
> > >
> > > https://www.zebra.com/us/en/support-downloads/knowledge-articles/ait/Network-Printing-Python-Example.html
> >
> > You can get the Report class from the pool and call execute on it. This
> > will return the bytes as second element of the response.
>
> Could you give me example on this? Iam litte bit confuse.
>
> Currently, I am using Relatio. It should be a dirty hack. I hope can use the method directly from Trytond instead.
>
> Below is my code:
>
> from relatorio import Report
>
> class BarcodeLabel(Workflow, ModelSQL, ModelView):
>
> @classmethod
> def print_lpn(cls, records):
> report = Report(abspath(join(dirname(__file__), 'lpn.odt')),
> ODT_MIME)
> data = report(records=records).render().getvalue()
> file_name = join(dirname(__file__), 'output_basic.odt')
> open(file_name, 'wb').write(data)

It should be something like:

@classmethod
def print_lpn(cls, records):
pool = Pool()
Report = pool.get('module.my_report', type='report')
ext, data, print, name = Report.execute(map(int, records), {})

Markus Bala

unread,
Sep 21, 2018, 5:15:26 AM9/21/18
to tryton-dev
Thank you :D

Markus Bala

unread,
Sep 28, 2018, 4:12:35 AM9/28/18
to tryton-dev
Hi Cedric,

I had tried above code. Overall it is working.
But some need to do correction. I had explain the code below.
For others may need in future.

line 1: @classmethod
line 2: def print_lpn(cls, records):
line 3: pool = Pool()
line 4: Report = pool.get('report_name', type='report')
line 5: ext, data, print, name = Report.execute([5,4], {})

Correction:
line 4: it only need "report_name", it do not need the 'module'.
line 5: it use [5,4] "ids" as parameter
Reply all
Reply to author
Forward
0 new messages