# Print from linux server

**URL:** <https://discuss.tryton.org/t/print-from-linux-server/5597>\
**Category:** Developer\
**Created:** [October 12, 2022, 12:59pm UTC](https://discuss.tryton.org/t/print-from-linux-server/5597 "2022-10-12T12:59:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcm](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/jcm/32/344_2.png) [@jcm](https://discuss.tryton.org/u/jcm)\
**Post date:** [October 12, 2022, 12:59pm UTC](https://discuss.tryton.org/t/print-from-linux-server/5597/1 "2022-10-12T12:59:18Z")

</div>

Hi,

I’d like to be able to print automatically some documents when Shipment state changes.  
The server is on linux. I’d like to call a command line to print the generated reports from the server. Could you please point me to an example or to the way to achieve this?

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [October 12, 2022, 4:35pm UTC](https://discuss.tryton.org/t/print-from-linux-server/5597/2 "2022-10-12T16:35:53Z")

</div>

I would suggest to use the email notification using the email of the printer (now most of the printer has such service).

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [October 12, 2022, 5:44pm UTC](https://discuss.tryton.org/t/print-from-linux-server/5597/3 "2022-10-12T17:44:23Z")

</div>

There are many examples on internet about commandline printing in Linux. The best way I think is to create a temporary PDF and use that to send to the printer using the commandline. When you use a trigger for the state change, it stays outside your workflow so when printing fails no problem.

You can use Python to execute the commandline command, so when a state changes, the trigger is called. That trigger will create the report and stores it somewhere in your filesystem. Then the trigger executes a commandline command to send the report to the printer.

So first thing I would do is search the internet. Then take a PDF file and try to send it to the printer. Next thing is integrating it with the trigger system in Tryton.

---

<div class="post-metadata">

**Author:** ![jcm](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/jcm/32/344_2.png) [@jcm](https://discuss.tryton.org/u/jcm)\
**Post date:** [October 12, 2022, 9:49pm UTC](https://discuss.tryton.org/t/print-from-linux-server/5597/4 "2022-10-12T21:49:58Z")

</div>

> [@edbo](#):
>
> You can use Python to execute the commandline command, so when a state changes, the trigger is called. That trigger will create the report and stores it somewhere in your filesystem. Then the trigger executes a commandline command to send the report to the printer.

Yes, that’s for this part I was looking an example.  
I think I’ll use something like

> <https://github.com/NaN-tic/trytond-printer/blob/main/printer.py>

to send the report to local cups server, and something like ir/email\_.py to generate the Report.

---

<div class="post-metadata">

**Author:** ![jcm](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/jcm/32/344_2.png) [@jcm](https://discuss.tryton.org/u/jcm)\
**Post date:** [October 12, 2022, 9:52pm UTC](https://discuss.tryton.org/t/print-from-linux-server/5597/5 "2022-10-12T21:52:49Z")

</div>

> [@ced](#):
>
> I would suggest to use the email notification using the email of the printer (now most of the printer has such service).

Sry, it won’t fit for me, because the printer is not on the same network, I can reach it only with cups/ipp, and I don’t see in its admin how to receive email. I must say I trust more ipp thant email to respond quickly.

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [October 12, 2022, 10:29pm UTC](https://discuss.tryton.org/t/print-from-linux-server/5597/6 "2022-10-12T22:29:50Z")

</div>

I was more thinking about the `convert` function in [https://hg.tryton.org/trytond/file/tip/trytond/report/report.py#l335](https://hg.tryton.org/trytond/file/tip/trytond/report/report.py#l335)

What I’m thinking of:

1. You have to create the template / report in the ‘normal’ way and make sure to have either Postscript or PDF as output extension.
2. Create a function (Python code) which can be executed by the trigger.
3. The function gets the template and knows the record(s) so it can generate the report.
4. The rendered output is stored on disk.
5. A Python subprocess executes the print command.

To generate the report you can use something like

```python
        pool = Pool()
        Report = pool.get('ir.action.report')
        reports = Report.search([
                ('report_name', '=', 'account.invoice'),
                ])

        with Transaction().set_context(language=language):
            ext, content, _, title = Report.execute(
                [record.id], {
                    'action_id': report[0].id,
                    })

```

You get the contents back and using `BytesIO` you can store the report on disk. Then do something like in the `convert` function of [https://hg.tryton.org/trytond/file/tip/trytond/report/report.py#l335](https://hg.tryton.org/trytond/file/tip/trytond/report/report.py#l335) specific the `cmd` variable. Just play with it.

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [October 13, 2022, 9:45am UTC](https://discuss.tryton.org/t/print-from-linux-server/5597/7 "2022-10-13T09:45:48Z")

</div>

Here is an example how to add your function to the trigger. [https://hg.tryton.org/modules/notification\_email/file/tip/ir.py](https://hg.tryton.org/modules/notification_email/file/tip/ir.py)  
And your function can look like [https://hg.tryton.org/modules/notification\_email/file/tip/notification.py#l213](https://hg.tryton.org/modules/notification_email/file/tip/notification.py#l213)

Maybe you can extend the `ir.trigger` with a `Many2One` which holds the report template.
