How to call Report.print() on the server

35 views
Skip to first unread message

Juan Jose Pablos

unread,
May 6, 2025, 6:08:23 AMMay 6
to Jam.py Users Mailing List
Hi,
I have a simple delivery note system (no tax)
I can print reports just fine.
I would like to forward the pdf using email within the system.
so I create a "Sent button" on the form, pass Record.ID
def sent_note(item,id_value):

But I am unable to find how to:
- get report item to call
- generate(report)

The further i have got is here:

Dean D. Babic

unread,
May 6, 2025, 10:29:25 AMMay 6
to Jam.py Users Mailing List

Juan Jose Pablos

unread,
May 6, 2025, 12:45:05 PMMay 6
to jam...@googlegroups.com
El 6/5/25 a las 16:29, Dean D. Babic escribió:
This?

I saw it, but the propose solution is on the client:

function print(item) {
    task.customers_report.customers.value = item.selections;
    task.customers_report.print();
}

When using it... it opens a new windows with the Pdf Report. But on the mailing list suggest that nothing is opened.

I tough that if you have already the item, you could execute on the server and get control over filename to reuse in another way

Dean D. Babic

unread,
May 6, 2025, 9:31:28 PMMay 6
to Jam.py Users Mailing List
Hi,
I think some more information is needed.

Jam deletes reports after specified time on Parameters.
So this:
"I would like to forward the pdf using email within the system.
so I create a "Sent button" on the form, pass Record.ID"
- would not work after report is deleted, and new one would be needed.

If your only problem is a new Window, than use this (as in a group thread):
def on_after_generate(report):
    report.report_url = None

I just tested it with V7 Demo Customers report after added above to Server Module:

-rw-rw-r-- 1 dbabic dbabic 259235 May  7 09:10 customers_report_2025-05-07_09-10-27.725324.pdf
-rw-rw-r-- 1 dbabic dbabic 259235 May  7 09:10 customers_report_2025-05-07_09-10-30.378040.pdf

I can click a number of times, no new Window will appear (3 seconds difference).

Hence, use Danijel's code on_after_generate to email it.

Then there is a on_convert_report event:

So your GitHub issue that was raised I merged it, but in reality, it is not needed.
Because simply adding above code (which exist on Demo Groups/Reports)
will override the framework "def  convert" and you could do here whatever is needed....

Hope this helps. 

Juan Jose Pablos

unread,
May 7, 2025, 6:17:41 AMMay 7
to jam...@googlegroups.com
El 7/5/25 a las 3:31, Dean D. Babic escribió:
If your only problem is a new Window, than use this (as in a group thread):
def on_after_generate(report):
    report.report_url = None
That fix the problem of the new windows!. Thanks!


Hence, use Danijel's code on_after_generate to email it.

How can I get the report.report_filename variable from other function() within the server?

I have try:

def sent_note(item,id_value):

    report = item.task.reports.report_template.copy()
    print(report.report_filename)

But I get this error on the console:

ERROR - 'Report' object has no attribute 'report_filename'


I have even check that item.task.reports.report_template is a report

 print(item.task.reports.report_template.item_type)


Then there is a on_convert_report event:

So your GitHub issue that was raised I merged it, but in reality, it is not needed.

I am happy if you revert that pull. If I can overwrite like that. This is mainly when using docker


Dean D. Babic

unread,
May 9, 2025, 3:10:49 AMMay 9
to Jam.py Users Mailing List
Have a look:

dbabic@dbabic-VirtualBox:~$ mail
No mail for dbabic

Now I fire the Demo/Customers list with below code:
dbabic@dbabic-VirtualBox:~$ mail
"/var/mail/dbabic": 1 message 1 new
>N   1 dbabic@mint        Fri May  9 14:54 412/31495 Novi dnevni izvještaj

And it contains the report:
Date: Fri,  9 May 2025 14:54:33 +0800 (AWST)

--===============5304737977545444297==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Test
   
    test
   
--===============5304737977545444297==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

    <html>
        <head></head>
        <body>
            <p>Test,<br><br>
               Test
                <hr>
                <i style="font-size: 11px">Test</i>
            </p>
        </body>
    </html>
--===============5304737977545444297==
Content-Type: application/octet-stream; Name="customers_report_2025-05-09_14-54-26.642168.pdf"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="customers_report_2025-05-09_14-54-26.642168.pdf"

JVBERi0xLjUKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURl
.....

Here is the clean code for Customers Report:

import smtplib  # for sending e-mail
from email.mime.multipart import MIMEMultipart # for composing MIME e-mail
from email.mime.text import MIMEText # for composing MIME text
from email.mime.application import MIMEApplication #novi dio modula!!!
from os.path import basename


def on_after_generate(report):
    report.report_url = None
    filename = report.report_filename

    sender_pass = ""    # password for SMTP server
    sender_smtp = "localhost"          # SMTP server
    sender_port = 25                       # SMTP port (here is SSL/TLS)
    sender_user = "dbabic"     # STMP sender account
   
    email_one= "dbabic" #<--- this is just example with local user dbabic, use you own from DB, etc

    # Create message container - the correct MIME type is multipart/alternative.
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Novi dnevni izvještaj"
    msg['From'] = sender_user
    msg['To'] = email_one
    # msg['Cc'] = user_email
   
    email_recipients_list=  email_one.split(",")
   
    # e-mail text (plain)
    mail_text = """Test
   
    test
    """
   
    # e-mail text (html)
    mail_html = """\
    <html>
        <head></head>
        <body>
            <p>Test,<br><br>
               Test
                <hr>
                <i style="font-size: 11px">Test</i>
            </p>
        </body>
    </html>"""
   
    with open(filename, 'rb') as f:
        part = MIMEApplication(f.read(), Name=basename(filename))
   
    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(mail_text, 'plain')
    part2 = MIMEText(mail_html, 'html')
    part['Content-Disposition'] = 'attachment; filename="{}"'.format(basename(filename))
   
    # Attach parts into message container.
    msg.attach(part1)
    msg.attach(part2)
    msg.attach(part)
   
    # Send the message via local SMTP server.
    s = smtplib.SMTP(sender_smtp, sender_port) # SMTP server params

    # sendmail function takes 3 arguments: sender's address, recipient's address
    # and message to send - here it is sent as one string.
    s.sendmail(sender_user,  email_recipients_list  , msg.as_string())
    s.quit()

So after I save email attachment, it is here:
-rw-------  1 dbabic dbabic    316799 May  9 15:02  mbox
drwx------  2 dbabic dbabic      4096 May  9 15:04  Mail
-rw-------  1 dbabic dbabic    232162 May  9 15:07  customers_report_2025-05-09_15-05-21.345450.pdf

You need to know how to set the email server where u running Jam. This is SIMPLE postfix on Ubuntu. Use
orig. code for advanced postfix authentication, ssl, etc....

Enjoy
D.

Juan Jose Pablos

unread,
May 9, 2025, 6:51:05 AMMay 9
to jam...@googlegroups.com
El 9/5/25 a las 9:10, Dean D. Babic escribió:

> Have a look:

ok Try to do it from a new button. because sometimes you want to print
the report. others you want to send by mail or whatsup or Telegram...


>
> def on_after_generate(report):
>     report.report_url = None
>     filename = report.report_filename

When you create another button. You need access to *report.report_filename*

I had some success using this hack:

1) call 2 functions:

 email_btn.click(function() {
        task.notes_template.print();
        item.server('send_note', task.journals.pending.id.value,
function(result, error)

2) after generate the report copy on /tmp as Note$Report.id).pdf

def on_after_generate(report):
    import os
    report.report_url = None
    os.system('cp ' + report.report_filename + ' /tmp/Note' +
str(report.id.value) + '.pdf')

3)when call the forward funtion using task.journals.pending.id.value

send_note_bymail(customer.phone.value,' + '/tmp/Note' +
str(task.journals.pending.id.value) + '.pdf')



Reply all
Reply to author
Forward
0 new messages