In the last post I showed how to print the sales invoice as a pdf. In this post we will do the same but generate a byte array of the pdf report.
I tried to make the code as readable as possible and hopefully can use it on other reports..
public static str printSalesInvoiceBase64Str(SalesInvoiceId _salesInvoiceId)
{
str ret;
CustInvoiceJour custInvoiceJour;
select firstonly custInvoiceJour
where custInvoiceJour.InvoiceId == _salesInvoiceId;
if (custInvoiceJour)
{
str ext = SRSPrintDestinationSettings::findFileNameType(SRSReportFileFormat::PDF, SRSImageFileFormat::BMP);
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.parmReportContract().parmRdpContract(salesInvoiceContract);
srsReportRunController.parmReportContract().parmReportExecutionInfo(new SRSReportExecutionInfo());
srsReportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
SRSPrintDestinationSettings printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();
printerSettings.printMediumType(SRSPrintMediumType::File);
printerSettings.fileFormat(SRSReportFileFormat::PDF);
printerSettings.parmFileName(custInvoiceJour.InvoiceId + ext);
printerSettings.overwriteFile(true);
SRSReportRunService srsReportRunService = new SrsReportRunService();
srsReportRunService.getReportDataContract(srsReportRunController.parmReportContract().parmReportName());
srsReportRunService.preRunReport(srsReportRunController.parmReportContract());
Map reportParametersMap = srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());
Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
SRSProxy srsProxy = SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());
System.Byte[] reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmreportcontract().parmreportpath(),
parameterValueArray,
printerSettings.fileFormat(),
printerSettings.deviceinfo());
if (reportBytes)
{
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes))
{
ret = System.Convert::ToBase64String(memoryStream.ToArray());
}
}
}
return ret;
}
Printing the byte array string as an info log looks like this.
To test the encoding and decoding, I have used online tools like this:
https://www.freeformatter.com/base64-encoder.html
Other sources for your reference:
https://meritsolutions.com/render-report-memory-stream-d365-aka-ax7/
https://d365technext.blogspot.com/2018/07/email-ssrs-report-as-attachment-d365fo.html
I will continue this series of posts to some exciting capabilities. Until next time, enjoy.