Saving a Report on Button Press

I'm trying to create a button in Vision that functions like a 'save as' for PDF reports.
My report is correctly configured and I can get it to generate and save on button press to a static location, but I want it to prompt the user for a file name, and location to save the report. I have this functionality for CSV files from a table and am trying to replicate this as a PDF. I'm sure it is something simple that i am missing.

im hoping to use system.file.saveFile or even JFileChooser but i get stuck at the same point with both options. I can successfully prompt the user for a location but it saves the PDF within another folder rather than the straight PDF.

# Get the current date
today = system.date.now()

# Define the fileDate based on the current date
fileDate = system.date.format(today, "yyyy-MM-dd")
reportName = "Alarm_History_" + fileDate + ".pdf"


# Display a file dialog to save the PDF report
filePath = system.file.saveFile(reportName, ".pdf", "PDF Files")

if filePath:
    reportParameters = {"StartDate": system.date.addHours(system.date.now(), -24), "EndDate": system.date.now()}
    settings = {"path": filePath, "format": "pdf"}

    # Execute and distribute the report to the specified path
    result = system.report.executeAndDistribute(path="Alarm_History", project="DEV_RPF2_MKVI", parameters=reportParameters, action="save", actionSettings=settings)

    # Check if the report execution was successful
    if result:
        print("PDF report saved to: " + filePath)
    else:
        print("Report generation and distribution failed.")
else:
    print("User canceled the file selection. Report generation and distribution canceled.")

any assistance would be greatly appreciated.

on a side note is it possible to restrict the system.save.file window so files can't be deleted?

Don't use executeAndDistribute(). Use system.report.execute() to return the PDF bytes, and save the file yourself (because it is Vision).

3 Likes