PDF download on a button click in ignition

Hi guys,

I'm trying to download the report in PDF format with a button click in perspective with a specified name of each pdf, but instead of creating the file within the folder it's creating folder within folder and the pdf file is getting generated with current timestamp.
below is the code:
import os
import re

                     # List of reports with paths and file names
                     repathlist = [
                         {"path": self.getSibling("RHFCpReportViewer").props.source, "name": self.getSibling("RHFCpReportViewer").props.downloadFilename},
                         {"path": self.getSibling("TemperingReportViewer").props.source, "name": self.getSibling("TemperingReportViewer").props.downloadFilename},
                         {"path": self.getSibling("ReportViewer").props.source, "name": self.getSibling("ReportViewer").props.downloadFilename},
                         {"path": self.getSibling("TemperingReportViewer_0").props.source, "name": self.getSibling("TemperingReportViewer_0").props.downloadFilename}
                     ]
                     
                     # Example: dynamically set order number
                     order_number = str(self.view.params.orderNumber)
                     
                     # Base directory to save reports
                     base_dir = os.path.join(r"C:\Signature\Reports", order_number)
                     result = system.report.executeReport(path="HT/RHF_temp_SCADA_Signed_Report", project="Chennai", format="pdf")
                     system.perspective.print("Test execute result: {result}")
                     system.perspective.print(result)
                     # Define the file name and path
                     file_path = "HTSCADAR1.pdf"  # Or dynamically set this if necessary
                     file_name = "HTSCADAR1.pdf"  # Or dynamically set this if necessary
                     
                     # Trigger the download in the Perspective session
                     system.perspective.download(file_name, result)
                     
                     # Ensure the base directory exists
                     if not os.path.exists(base_dir):
                         os.makedirs(base_dir)
                         system.perspective.print("Created base directory: {base_dir}")
                     else:
                         system.perspective.print("Base directory already exists: {base_dir}")
                     
                     # Iterate through the report list and generate each report
                     for report in repathlist:
                         try:
                             # Extract report details
                             report_file_name = report["name"]
                             
                             # Ensure the file name has the '.pdf' extension
                             if not report_file_name.endswith(".pdf"):
                                 report_file_name += ".pdf"
                             
                             # Sanitize the file name (remove special characters or spaces)
                             report_file_name = re.sub(r'[^a-zA-Z0-9_\-\.]', '_', report_file_name)
                     
                             # Construct the full file path
                             file_path = os.path.join(base_dir, report_file_name)
                     
                             # Log the report path and file path
                             system.perspective.print("Report path: {report['path']}")
                             system.perspective.print("File path: {file_path}")
                     
                             # Ensure the directory exists before trying to create the file
                             if not os.path.exists(base_dir):
                                 os.makedirs(base_dir)
                            
                             # Execute and save the report
                             report_bytes = system.report.executeAndDistribute(
                                 path=report["path"],
                                 project="Chennai",
                                 action="save",
                                 actionSettings={"path": file_path, "format": "pdf"}
                             )
                     
                             # Log the result of the report execution
                             if report_bytes is None:
                                 system.perspective.print("Failed to execute report for: {report_file_name}")
                             else:
                                 system.perspective.print("Report execution successful for: {report_file_name}")
                     
                             # Check if the report file was generated and exists
                             if os.path.isfile(file_path):
                                 system.perspective.print("Report saved: {file_path}")
                                 system.perspective.download(file_path, report_file_name)  # Trigger download
                             else:
                                 raise Exception("File not found after report generation: {file_path}")
                     
                         except Exception as e:
                             # Log any errors with proper formatting
                             system.perspective.print("Error generating report {report_file_name}: {str(e)}")

please help to understand, where are we making a mistake.

Best Regards,
Payel Bera

Please see Wiki - how to post code on this forum. Then edit your post and apply formatting to the code block. That will fix the indentation and apply syntax highlighting, making it easier for us to read. Thanks.

Tip: the "Keep it Tidy" section of the forum FAQ asks that we do not add signatures to our posts. Your username is applied automatically.

system.report.executeAndDistribute is documented to return nothing.

You should be using system.report.execute, which does return the bytes. Then pass that directly to system.perspective.download(). No need to save the file in the gateway anywhere. (If you need it there for other purposes, just write those bytes in your script.)

1 Like

Hi,

Thank you so much for your response, we tried the method system.report.execute and system.perspective.download(), problem is files need to be stored automatically with a specified name and format in a specific folder without any manual intervention, which is not the case when using execute or download method.

Best Regards,
Payel Bera

In the gateway or in the client?

If the gateway, why do you need system.perspective.download() at all?

If in the client, you simply cannot do this with Perspective. That is a huge security violation for browser-based technology.