Printer Settings and Run From Code

From Axaptapedia

In several situations you may find yourself needing to run a report in the background, without any User Interaction. Of course, you will still need to be able to control where the output goes.

User Interaction

To run our example, we will use the standard report "Cust". We need to change the "Interactive" property on the report and on the query for this example to work. See article about User Interaction.

Initializing The Report

First, we need to instantiate our report, and prepare it to run. There are several ways to do this, but i like this best since it works with the Args class and allows you to pass along some values. You can also supply Args to the Run() method of ReportRun. First we declare our reportrun to use, and the args class.

       ReportRun           custReport;
       Args                args = new Args();

To instantiate it, we put the report's name in the Args.Name() field. Remember it is best practice to use the precompiler reportStr() function to avoid run-time errors.

       args.name(ReportStr(Cust));
       custReport = new ReportRun(args);
       custReport.init();

Now CustReport is ready to run. However, we want to manipulate the printersettings. Declare the PrintJobSettings at the top right after our reportrun and args declarations:

       PrintJobSettings    printJobSettings;

I am fetching the PrintJobSettings from the reportrun class to get a default. After that, we need to set what we want to do with the report output. Here we specify to print as a PDF file, to our c: drive.

       printJobSettings = custReport.printJobSettings();
       printJobSettings.setTarget(PrintMedium::File);
       printJobSettings.preferredTarget(PrintMedium::File);
       printJobSettings.format(PrintFormat::PDF);
       printJobSettings.preferredFileFormat(PrintFormat::PDF);
       printJobSettings.fileName("c:\\test-invoice.pdf");

Of course you will need to randomize the filename or find some clean way to come with a name. You could use some of the WinApi:: functions to check if files already exist etc... Last thing to do is pass the printjobsettings back to the report, and run it...

       custReport.unpackPrintJobSettings(printJobSettings.packPrintJobSettings());
       custReport.run();

Summary

As always, there are many variations to achieve the same goal. But this should get you on your way.