Print sales invoice to PDF file/stream and upload it to azure

Code to print sales invoice to PDF file and uplo
 
        System.Exception                ex;
        Filename                        fileName;
        SRSPrintDestinationSettings        settings;
        System.Byte[]                    reportBytes = new System.Byte[0]();
        SRSProxy                        srsProxy;
        SRSReportRunService                srsReportRunService = new SrsReportRunService();
        Map                                reportParametersMap;
        SRSReportExecutionInfo            executionInfo = new SRSReportExecutionInfo();
        Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
        
        System.DateTime    dateTime    = System.DateTime::get_UtcNow();
        str                utcTimeAsStr= dateTime.ToString('yyyyMdHHmmss');

        fileName = 'SalesInvoiceAutoPrint_' + custInvoiceJour.InvoiceId + '_' + utcTimeAsStr +'.pdf';
        
        try
        {
            PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat();

            SalesInvoiceContract salesInvoiceContract = new SalesInvoiceContract();
            salesInvoiceContract.parmRecordId(custInvoiceJour.RecId);

            SrsReportRunController srsReportRunController = new SrsReportRunController();
            srsReportRunController.parmReportName(printMgmtReportFormatName);
            srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
            srsReportRunController.parmShowDialog(false);
            srsReportRunController.parmLoadFromSysLastValue(false);
            srsReportRunController.parmReportContract().parmRdpContract(salesInvoiceContract);
    
            SRSPrintDestinationSettings printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();
            printerSettings.printMediumType(SRSPrintMediumType::File);
            printerSettings.fileFormat(SRSReportFileFormat::PDF);
            printerSettings.parmFileName(fileName);
            printerSettings.fileName(fileName);
            printerSettings.overwriteFile(true);

            // Below is a part of code responsible for rendering the report
            srsReportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
            srsReportRunController.parmReportContract().parmReportExecutionInfo(executionInfo);

            srsReportRunService.getReportDataContract(srsReportRunController.parmreportcontract().parmReportName());
            srsReportRunService.preRunReport(srsReportRunController.parmreportcontract());
            reportParametersMap = srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());
            parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

            srsProxy = SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());

            reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmReportContract().parmReportPath(),
                parameterValueArray,
                printerSettings.fileFormat(),
                printerSettings.deviceinfo());
            //srsproxy.renderReportToFile();
            //srsproxy.renderReportToPrinter();

            if (reportBytes)
            {
                // upload PDF file
                System.IO.MemoryStream    stream = new System.IO.MemoryStream(reportBytes);
                URL                        downloadLink;
                VKUploadToCloud            uploadToCloud = VKUploadToCloud::construct(
                        'DefaultEndpointsProtocol=https;AccountName={AccountName};AccountKey={AccountKey};EndpointSuffix=core.windows.net',
                        'invoices'
                    );
                
                downloadLink = uploadToCloud.upload(fileName, stream);
            }
        }
        catch(Exception::CLRError)
        {
            ex = ClrInterop::getLastException();
            if (ex != null)
            {
                ex = ex.get_InnerException();
                if (ex != null)
                {
                    error(ex.ToString());
                }
            }
        }
        catch(ex)
        {
            error(ex.Message);
        }

Support The Author

 If you found value in what I share, I've set up a Buy Me a Coffee page as a way to show your support.

Buy Me a Coffee

Post a Comment


All Comments


No comments. Be the first one to comment on this post.

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics 365 FO.

Authors are allowed to set their own "buy me a coffee" link.
Join us.

Blog Tags