Monday, 6 October 2025

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

 Code to print sales invoice to PDF file and uplo

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        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);
        }

No comments:

Post a Comment

Creating new D365FO Business Event

  The name should include the noun or phrase that corresponds with theevent, followed by the BusinessEvent suffix. New business event for Cu...