Sending Report over a mail

Hi I would like to send a report that looks like below,


Parameters used: StartDate, EndDate,iProcess
I want the screen to append for iProcess from 1 to 5 and send this report over a mail.
I am triggering this function via memory tag with script below,

if currentValue.value and not initialChange:
	    import system
	
	    # Get logger
	    logger = system.util.getLogger("Report")
	
	    # Define report path and date parameters
	    reportPath = "Graphical Loss Report"
	    StartDate = system.tag.readBlocking("[.]../../Global/ShiftStart")[0].value
	    EndDate = system.tag.readBlocking("[.]../../Global/ShiftEnd")[0].value
	
	    # Reset trigger tag
	    system.tag.writeBlocking("[.]ShiftNotificationTrigger", [0])
	
	    # Initialize flag variable to track exceptions
	    hasExceptions = False
	
	    # Initialize report bytes
	    reportBytes = bytearray()
	
	    # Loop through iProcess values and add each process page to report
	    for iProcess in range(1, 5):
	        logger.info("Adding process page for iProcess %d" % iProcess)
	
	        # Set report parameters and get process page bytes
	        params = {"StartDate": StartDate, "EndDate": EndDate, "iProcess": iProcess}
	        try:
	            processBytes = system.report.executeReport(
	                path=reportPath,
	                project="XYZ",
	                parameters=params,
	                fileType="pdf"
	            )
	        except Exception as e:
	            # Catch report generation exceptions and set flag variable
	            logger.error("Error generating report for iProcess %d: %s" % (iProcess, str(e)))
	            hasExceptions = True
	            continue
	
	        # Append process page bytes to report bytes
	        reportBytes.extend(processBytes)
	
	    if hasExceptions:
	        # Send email indicating that report generation failed for one or more iProcess values
	        system.net.sendEmail(
	               smtpProfile="XYZ",
	            fromAddr="ignition-noreply@xxx.tech",
	            to=[ "xysz.com" ],
	            subject="Combined Report for all iProcess - Error",
	            body="Failed to generate report for one or more iProcess values.",
	        )
	    else:
	        # Send email with report bytes
	        system.net.sendEmail(
	            smtpProfile="XYZ",
	            fromAddr="ignition-noreply@xxx.tech",
	            to=[ "xysz.com" ],
	            subject="Combined Report for all iProcess",
	            body="Please find attached the combined report for all iProcess.",
	            attachments=[("CombinedReport.pdf", reportBytes)]
	        )
	
	        logger.info("Combined report sent")

I am getting a mail without attachment.
IDK what is missed/wrong here.
Kindly assist in resolving this

You're using system.report.executeReport, try using system.report.executeAndDistribute as this has the ability to send it via email.

2 Likes

I tried the same with below script,

		if currentValue.value and not initialChange:
		import system
		
		# Get logger
		logger = system.util.getLogger("Report")
		
		# Define report path and date parameters
		reportPath = "Graphical Loss Report"
		StartDate = system.tag.readBlocking("[.]../../Global/ShiftStart")[0].value
		EndDate = system.tag.readBlocking("[.]../../Global/ShiftEnd")[0].value
		# Reset trigger tag
		system.tag.writeBlocking("[.]ShiftNotificationTrigger", [0])
		# Loop through iProcess values and generate report for each one
		for i in range(1, 5):
			iProcess = i
			logger.info("Generating report for iProcess %d" % iProcess)
			
			# Set report parameters and execute distribution
			params = {"StartDate": StartDate, "EndDate": EndDate, "iProcess": iProcess}
			logger.info(str(params))
			system.report.executeAndDistribute(path=reportPath,project="XYZ_MES",action="email",parameters= params,
			actionSettings={"to": ["dharani.t@batpl.com"],"smtpServerName": "XYZ_SMTP","subject": "Report for iProcess %d" % iProcess,"from": "ignition-noreply@XYZ.tech","attachmentName": "Loss"})
			logger.info("Report for iProcess %d sent" % iProcess)
	        
	        

But am getting separate mail for each process but i need all the process as page by page.

That's not doing what you want to be doing.
If you want to email five separate PDF attachments on one email, you can make a list, execute each report, and store the generated data into that list to pass in to sendEmail in the attachmentData parameter.

You cannot directly combine the binary data for different PDFs and expect a valid PDF as the output, though. There are headers and other information that will at best leave you with a totally invalid PDF-like binary blob. Ignition doesn't have any internal facility to combine PDFs, so I would highly recommend just sending five attachments. If that's really not an option, you will have to either integrate a third party Java library that can stitch together PDFs, or use a third party tool to merge the PDFs.

But i require a single report pdf in which each process is appended

Then you will need to use an external tool to combine them. One of the solutions Paul linked should do.

Or is there a way to use script data source so that report charts will be dynamically arranged and hence now we will be having a single report?

No.

The closest you could come would be to use a table group.