In this article, we will show you how to generate an Electronic reporting format in its original file format as a byte array. If this original file format is Excel or Word, you will be able to print the ER Format to memory in PDF format as well.
One would think that printing ER Formats to memory is easy to achieve. Wrong! It's actually quite complex to understand and implement, so we created a helper class to make it easier for you.
Helper class DocERFormatGenerator
This helper class is called DocERFormatGenerator and uses:
- ERFileDestinationMemory - the Memory print destination, which is not visible on the ER destination settings form, but you can use it for custom scenarios, when you need to print a report to memory.
- Microsoft Office Conversion Service - to convert Word and Excel documents to PDF, if you require so.
The DocERFormatGenerator class extends SysOperationServiceBase and it requires the ER report controller and data contract, while it bypasses the ER report service class. Instead, DocERFormatGenerator generates the report using the ERFormatMappingRun class with the built-in ERFileDestinationMemory destination:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private void runER() { this.setFormatMappingGUID(); ERFormatMappingId erFormatMappingId = ERFormatMappingTable::getRecIDByGuid(formatMappingGUID); if (!erFormatMappingId) { throw error(strFmt("@DocentricAX4:ERFormatGeneratorERFormatMappingNotFound", guid2Str(formatMappingGUID))); } // Call ER to generate the report. ERIFormatMappingRun formatMappingRun = ERObjectsFactory::createFormatMappingRunByFormatMappingId(erFormatMappingId); this.setParameters(formatMappingRun); // Set memory stream file destionation ERFileDestinationMemory fileDestinationMemory = new ERFileDestinationMemory(); formatMappingRun.withFileDestination(fileDestinationMemory); str filePath = formatMappingRun.run(); if (filePath) { this.parmGeneratedDocument(DocDocument::createMemoryStream(fileDestinationMemory.GetStream(), filePath)); } } |
Learn more about ERReportLauncher reports >>
How to use the DocERFormatGenerator class
AssetRollForward is an ER report, based on the SysOperation framework. Let's take a walkthrough on how to print this report to memory using DocERFormatGenerator. This helper class is an abstract class, so basically you need to extend it and implement three abstract methods, as we did in the DocTutorialERFormatGenerator class for the AssetRollForward report.
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 |
public class DocTutorialERFormatGenerator extends DocERFormatGenerator { private const str ERModelDataSourceName = 'model'; /// <summary> /// Sets the ER format mapping GUID. /// </summary> public void setFormatMappingGUID() { AssetRollForwardContract dataContract = this.getContract(); this.parmFormatMappingGUID(ERFormatMappingTable::getGuidByRecId(dataContract.parmFormatMapping())); } /// <summary> /// Initializes an instantiated controller or data contact class from args. /// </summary> /// <param name = "_args">An instance of Args object</param> public void initFromArgs(Args _args) { AssetRollForwardContract assetRollForwardParams = _args.object() as AssetRollForwardContract; AssetRollForwardContract dataContract = this.getContract(); dataContract.parmFromDate(assetRollForwardParams.parmFromDate()); dataContract.parmToDate(assetRollForwardParams.parmToDate()); dataContract.parmFormatMapping(assetRollForwardParams.parmFormatMapping()); } /// <summary> /// The service method for Fixed asset roll forward report. /// </summary> /// <param name = "_contract">The Fixed asset roll forward contract<</param> /// <returns>An instance of DocTutorialERFormatGenerator class</returns> public DocTutorialERFormatGenerator generateER(AssetRollForwardContract _contract) { // Call ER to generate the report this.generateERReport(_contract); return this; } /// <summary> /// Sets the parameters for ER format. /// </summary> /// <param name = "_formatMappingRun">An instance of ERFormatMappingRun class</param> public void setParameters(ERIFormatMappingRun _formatMappingRun) { AssetRollForwardContract dataContract = this.getContract(); Query query = new Query(SysOperationHelper::base64Decode(dataContract.parmQuery())); AssetRollForwardDP dataProvider = AssetRollForwardDP::construct(); AssetRollForwardTmp assetRollForwardTmp = dataProvider.getAssetRollForwardTmp(dataContract, query); Name erModeleName = this.getERModelName(_formatMappingRun); ERIModelDefinitionParamsAction params = new ERModelDefinitionParamsUIActionComposite() .add(new ERModelDefinitionDatabaseContext().addTemporaryTable(assetRollForwardTmp)) .add(new ERModelDefinitionObjectParameterAction(ERModelDataSourceName, 'MyParameters', dataContract, true)); _formatMappingRun.withParameter(params); _formatMappingRun.withFileDestination(dataContract.getFileDestination()); } } |
We also included the main() method, to demonstrate printing to Memory in action.
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 |
public static void main() { // Set parameters for Fixed asset roll forward report AssetRollForwardContract dataContract = new AssetRollForwardContract(); dataContract.parmFromDate(mkDate(1, 1, 2000)); dataContract.parmToDate(mkDate(31, 12, 2021)); dataContract.parmFormatMapping(ERFormatMappingTable::findByGUID(str2Guid("{1BD90DB1-B189-4E65-9557-D833FA526DD2}")).RecId); Args args = new Args(); args.object(dataContract); // Create the ER format generator object with provided parameters DocTutorialERFormatGenerator erFormatGenerator = new DocTutorialERFormatGenerator( classStr(AssetRollForwardController), classStr(DocTutorialERFormatGenerator), methodStr(DocTutorialERFormatGenerator, generateER)); if (erFormatGenerator) { // Initialize the ER format generator object erFormatGenerator.initFromArgs(args); // Run the ER format generator erFormatGenerator.run(); // Get generated document as memory stream using (System.IO.MemoryStream documentContent = erFormatGenerator.getDocumentContentMemoryStream()) { // Check if the document was generated if (documentContent != null) { // Download the generated document documentContent.Position = 0; DocFileMngHelper::sendFileToUser(documentContent, erFormatGenerator.parmGeneratedDocument().parmFileName(), '', '', '', '', true, true); } } // Get generated document as PDF memory stream using (System.IO.MemoryStream pdfDocumentContent = erFormatGenerator.getPdfDocumentContentMemoryStream(DocReportPrintOrientation::Landscape)) { // Check if the source document was successfuly converted to PDF memory stream if (pdfDocumentContent != null) { // Download the generated PDF document pdfDocumentContent.Position = 0; DocFileMngHelper::sendFileToUser(pdfDocumentContent, System.IO.Path::ChangeExtension(erFormatGenerator.parmGeneratedDocument().parmFileName(), DocConstantFile::FileExtPdf), '', '', '', '', true, true); } } } } |