Search code examples
pdfprintingaxaptadynamics-ax-2012x++

Print SSRSReport to file (.PDF)


I need to find a way to "print" a SrsReport, in my case SalesInvoice, as .PDF (or any kind of file) to a specific location.

For this I modified the SRSPrintDestinationSettings to output the SalesInvoice-Report as a .PDF:

settings = controller.parmReportContract().parmPrintSettings();
settings.printMediumType(SRSPrintMediumType::File);
settings.fileFormat(SRSReportFileFormat::PDF);
settings.overwriteFile(true);
settings.fileName(@'\\AXDEV\Bottomline\Test\test.pdf');

Somehow this gets ignored and I recive a Email with the report as .PDF attached.

For example this will run on ax 2012 but won't print to PDF for me.

SRSPrintDestinationSettings     settings;
CustInvoiceJour         custInvoiceJour;
SrsReportRunController          controller = new SrsReportRunController();
PurchPurchaseOrderContract  rdpContract = new PurchPurchaseOrderContract();
SalesInvoiceContract    salesInvoiceContract = new SalesInvoiceContract();

select firstOnly1 * from custInvoiceJour where custInvoiceJour.SalesId != "";

// Define report and report design to use
controller.parmReportName(ssrsReportStr(SalesInvoice,Report));
// Use execution mode appropriate to your situation
controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);


rdpContract.parmRecordId(custInvoiceJour.RecId);
controller.parmReportContract().parmRdpContract(rdpContract);

// Explicitly provide all required parameters
salesInvoiceContract.parmRecordId(custInvoiceJour.RecId); // Record id must be passed otherwise the report will be empty
controller.parmReportContract().parmRdpContract(salesInvoiceContract);
salesInvoiceContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo()); // comment this code if tested in pre release

// Change print settings as needed
settings = controller.parmReportContract().parmPrintSettings();
settings.printMediumType(SRSPrintMediumType::File);
settings.fileFormat(SRSReportFileFormat::PDF);
settings.overwriteFile(true);
settings.fileName(@'\\AXDEV\Bottomline\Test\test.pdf');

//tokens = settings as SrsPrintDestinationTokens();
//controller.parmPrintDestinationTokens(null);

//Suppress report dialog
controller.parmShowDialog(false);
// Execute the report
controller.startOperation();

Questions: Is this the correct way to print a srsReport to .pdf? Am I passing/setting the printerSettings correctly? Where does it say "Send Email"?

EDIT: Code is working fine. We are using external code of a company which simply doesnt implement this. Use the cleaner code of Alex Kwitny


Solution

  • Here is working code for me. I just quickly coded this from scratch/memory based off of glancing at yours, so compare for differences.

    I have two things marked (1) and (2) for you to try with your code, or just copy/paste mine.

    static void JobSendToPDFInvoice(Args _args)
    {
        SrsReportRunController          controller = new SrsReportRunController();
        SRSPrintDestinationSettings     settings;
        CustInvoiceJour                 custInvoiceJour = CustInvoiceJour::findRecId(5637925275);
        SalesInvoiceContract            salesInvoiceContract = new SalesInvoiceContract();
        Args                            args = new Args();
    
        controller.parmReportName(ssrsReportStr(SalesInvoice, Report));
        controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);
        controller.parmShowDialog(false);
    
        salesInvoiceContract.parmRecordId(custInvoiceJour.RecId);
        salesInvoiceContract.parmDocumentTitle(CustInvoiceJour.InvoiceId);
        salesInvoiceContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo());
    
        // (1) Try by passing args
        args.record(custInvoiceJour);
        args.parmEnum(PrintCopyOriginal::Original);
        args.parmEnumType(enumNum(PrintCopyOriginal));
    
        controller.parmReportContract().parmRdpContract(salesInvoiceContract);    
        controller.parmArgs(args);
    
        // (2) Try explicitly preventing loading from last value
        // controller.parmLoadFromSysLastValue(false);
    
        // Change print settings as needed
        settings = controller.parmReportContract().parmPrintSettings();
        settings.printMediumType(SRSPrintMediumType::File);
        settings.fileFormat(SRSReportFileFormat::PDF);
        settings.overwriteFile(true);
        settings.fileName(@'C:\Temp\Invoice.pdf');
    
    
        controller.startOperation();
    }