{"title":"OnAfterIntermediateDocumentReady Event","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-onafterintermediatedocumentready-event/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-onafterintermediatedocumentready-event, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-onafterintermediatedocumentready-event","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Describe the OnAfterIntermediateDocumentReady Event in Business Central.","date":"2023-03-10","meta":{"topic":"article"},"outline":[{"level":2,"title":"Usage","slug":"usage","at":122,"chars":1043},{"level":2,"title":"Publisher","slug":"publisher","at":1165,"chars":43},{"level":2,"title":"Raised","slug":"raised","at":1208,"chars":379},{"level":2,"title":"Syntax","slug":"syntax","at":1587,"chars":312},{"level":2,"title":"Parameters","slug":"parameters","at":1899,"chars":790},{"level":3,"title":"ObjectID","slug":"objectid","at":1913,"chars":97},{"level":3,"title":"ObjectPayload","slug":"objectpayload","at":2010,"chars":147},{"level":3,"title":"DocumentStream","slug":"documentstream","at":2157,"chars":205},{"level":3,"title":"TargetStream","slug":"targetstream","at":2362,"chars":193},{"level":3,"title":"Success","slug":"success","at":2555,"chars":134},{"level":2,"title":"Report payload","slug":"report-payload","at":2689,"chars":4858},{"level":3,"title":"Attributes","slug":"attributes","at":4092,"chars":3455},{"level":4,"title":"objectname","slug":"objectname","at":4107,"chars":50},{"level":4,"title":"objectid","slug":"objectid-1","at":4157,"chars":46},{"level":4,"title":"documenttype","slug":"documenttype","at":4203,"chars":217},{"level":4,"title":"invokedby","slug":"invokedby","at":4420,"chars":74},{"level":4,"title":"invokeddatetime","slug":"invokeddatetime","at":4494,"chars":184},{"level":4,"title":"companyname","slug":"companyname","at":4678,"chars":57},{"level":4,"title":"version","slug":"version","at":4735,"chars":120},{"level":4,"title":"duplex","slug":"duplex","at":4855,"chars":175},{"level":4,"title":"color","slug":"color","at":5030,"chars":161},{"level":4,"title":"defaultcopies","slug":"defaultcopies","at":5191,"chars":160},{"level":4,"title":"filterviews","slug":"filterviews","at":5351,"chars":64},{"level":4,"title":"name","slug":"name","at":5415,"chars":75},{"level":4,"title":"tableid","slug":"tableid","at":5490,"chars":57},{"level":4,"title":"view","slug":"view","at":5547,"chars":42},{"level":4,"title":"papertray","slug":"papertray","at":5589,"chars":123},{"level":4,"title":"papersourcekind","slug":"papersourcekind","at":5712,"chars":190},{"level":4,"title":"paperkind","slug":"paperkind","at":5902,"chars":145},{"level":4,"title":"landscape","slug":"landscape","at":6047,"chars":119},{"level":4,"title":"units","slug":"units","at":6166,"chars":279},{"level":4,"title":"height","slug":"height","at":6445,"chars":105},{"level":4,"title":"width","slug":"width","at":6550,"chars":103},{"level":4,"title":"intent","slug":"intent","at":6653,"chars":169},{"level":4,"title":"layoutmodel","slug":"layoutmodel","at":6822,"chars":157},{"level":4,"title":"layoutname","slug":"layoutname","at":6979,"chars":77},{"level":4,"title":"layoutmimetype","slug":"layoutmimetype","at":7056,"chars":210},{"level":4,"title":"layoutapplicationid","slug":"layoutapplicationid","at":7266,"chars":96},{"level":4,"title":"reportrunid","slug":"reportrunid","at":7362,"chars":185},{"level":2,"title":"Sample code","slug":"sample-code","at":7547,"chars":1796},{"level":2,"title":"Related information","slug":"related-information","at":9343,"chars":118}],"text":"This article describes the syntax of the OnAfterIntermediateDocumentReady event and the attributes of the report payload.\n## Usage\nUse the OnAfterIntermediateDocumentReady event to specify what happens when the user has generated an intermediate report artifact by stream or file, from code or a request page action. This will typically be Xml or Word files where they are genereated in the specific process flows. The OnAfterIntermediateDocumentReady event is used to enable document patching scenarios in the application, or to copy the artifact to a different location during testing.\nThe event input is the report ID, a JSON collection with report runtime information and the generated document in an InStream. Use the documenttype JSON property to identify the data type stored in the DocumentStream parameter and act accordingly. The final result must be written to the TargetStream parameter and the parameter Success must be set to true if the modified stream is to be used in the platform. The content in the TargetStream will be discarded if the Success parameter is false upon return from the procedure.\n> NOTE\n> Changing the content type isn't allowed.\n## Publisher\nCodeunit 44 ReportManagement.\n## Raised\nWhen the report runtime has generated an intermediate output artifact that'll be removed when the final artifact have be created. This can occur when the application runs a SaveAs method, the user invokes one of the SendTo actions in the report request page, or when the document is being printed using an extension printer or by using the web client print capability.\n## Syntax\n```al\n[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnAfterIntermediateDocumentReady', '', true, true)]\nlocal procedure OnAfterIntermediateDocumentReady(ObjectId: Integer; ObjectPayload: JsonObject; DocumentStream: InStream; var TargetStream: OutStream; var Success: Boolean)\n```\n## Parameters\n### ObjectID\nType: methods-auto/integer/integer-data-type\nThe ID of the report object to be run.\n### ObjectPayload\nType: methods-auto/jsonobject/jsonobject-data-type\nInstance of the report payload. See Report payload structure<#reportpayload>.\n### DocumentStream\nType: methods-auto/instream/instream-data-type\nA stream object that contains the generated artifact. The actual data type can be found from the MIME type in the report payload document.\n### TargetStream\nType: methods-auto/outstream/outstream-data-type\nTarget stream that must contain the modified artifact if the application code handles the object and returns `success = true`.\n### Success\nType: methods-auto/boolean/boolean-data-type\nSpecifies whether the extension handled the generated artifact successfully.\n## Report payload\nThe report payload contains metadata for the report object and a list of attributes that identifies that current invocation. The payload consists of several attributes and has the following structure.\n```json\n{\n    \"filterviews\":\n    [\n        {\"name\":\"Header\",\"tableid\":112,\"view\":\"VERSION(1) SORTING(Field3) WHERE(Field3=1(103027))\"},\n        {\"name\":\"Line\",\"tableid\":113,\"view\":\"VERSION(1) SORTING(Field3,Field4) WHERE(Field4=1(0..10000))\"},\n        {\"name\":\"ShipmentLine\",\"tableid\":7190,\"view\":\"VERSION(1) SORTING(Field1,Field2,Field3) WHERE(Field2=1(10000))\"}\n    ],\n    \"version\":1,\n    \"objectname\":\"Standard Sales - Invoice\",\n    \"objectid\":1306,\n    \"documenttype\":\"application/pdf\",\n    \"invokedby\":\"00000000-0000-0000-0000-000000000001\",\n    \"invokeddatetime\":\"2020-01-17T15:33:52.48+01:00\",\n    \"companyname\":\"CRONUS International Ltd.\",\n    \"printername\":\"My Printer\",\n    \"duplex\":false,\n    \"color\":false,\n    \"defaultcopies\":1,\n    \"papertray\":\n    {\n        \"papersourcekind\":257,\n        \"paperkind\":0,\n        \"landscape\":false,\n        \"units\":0,\n        \"height\":1268,\n        \"width\":929\n    },\n    \"intent\":\"Print\",\n    \"layoutmodel\": \"Rdlc\",\n    \"layoutname\": \"RDLCLayout\",\n    \"layoutmimetype\": \"Application/ReportLayout/Rdlc\",\n    \"layoutapplicationid\": \"00001111-aaaa-2222-bbbb-3333cccc4444\",\n    \"reportrunid\": \"6a3742c1-65ef-44a6-af58-3ef4a902932b\"\n}\n\n```\n### Attributes\n#### objectname\nSpecifies the name of the object.\n#### objectid\nSpecifies the ID of the object.\n#### documenttype\nSpecifies the MIME type of the document. Currently only `application/pdf` is supported for printing, other intents can have types specific to the report layout model and selected output target type.\n#### invokedby\nSpecifies the ID of the user who invoked the print action.\n#### invokeddatetime\nSpecifies the date and time that the print action was invoked, for example, `2019-10-22T22:25:54.338+02:00`. The value is the date and time on the client machine.\n#### companyname\nSpecifies the Business Central company.\n#### version\nSpecifies the version of the report payload. Currently, the only supported version for the payload is `1`.\n#### duplex\nSpecifies whether to use duplex (2-sided) printing. true specifies duplex printing; otherwise false.\n> NOTE\n> duplex is currently not used and ignored at runtime.\n#### color\nSpecifies whether color printing is set. true specifies color printing; otherwise false.\n> NOTE\n> color is currently not used and ignored at runtime.\n#### defaultcopies\nSpecifies the number of copies to print by default. The default is `1`.\n> NOTE\n> defaultcopies is currently not used and ignored at runtime.\n#### filterviews\nSpecifies views that are used on the document.\n#### name\nSpecifies the name of data item that the filter view applies to.\n#### tableid\nSpecifies the ID of the table for the view.\n#### view\nSpecifies the name of the view.\n#### papertray\nSpecifies the paper tray settings from the printer payload<devenv-onaftersetupprinters-event#printpayload>.\n#### papersourcekind\nSpecifies a standard paper source for the paper tray. The value can be either the text representation (such as AutomaticFeed) or the corresponding number (such as `7`).\n#### paperkind\nSpecifies the paper size. The value can be either the text representation (such as A4) or the corresponding number (such as `9`).\n#### landscape\nSpecifies whether landscape orientation is used. true specifies landscape orientation; otherwise false.\n#### units\nSpecifies the units of measurement for values in height and width attributes. Valid values include:\n HI for hundredths of an inch (default value).\n TI for thousandths of an inch.\n IN for inches.\n CM for centimeters.\n MM for millimeters.\n PC for picas.\n PT for points.\n#### height\nSpecifies the height of the paper. The values include positive integers up to 2,147,483,647.\n#### width\nSpecifies the width of the paper. The values include positive integers up to 2,147,483,647.\n#### intent\nSpecifies the intent of the current report invocation, which can be one of the following values.\n None\n Print\n Preview\n Save\n Schedule\n Download\n Parameters\n#### layoutmodel\nSpecifies the layout model selected for the current report invocation, which can be one of the following values.\n Rdlc\n Word\n Excel\n Custom\n#### layoutname\nSpecifies the layout name for the currently selected layout.\n#### layoutmimetype\nSpecifies the layout mimetype for the currently selected layout. The built-in layout types are:\n Application/ReportLayout/Rdlc\n Application/ReportLayout/Word\n Application/ReportLayout/Excel\n#### layoutapplicationid\nSpecifies the ID of the application that provides the selected layout.\n#### reportrunid\nSpecifies a unique ID of type Guid that identifies the current report invocation. This value can be used to correlate calls to report event that support json payloads.\n## Sample code\nSubscribe to the standard event in codeunit ReportManagement. This sample saves the JSON payload to a text file and copies the document stream content to the target stream.\n```al\n[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnAfterIntermediateDocumentReady', '', true, true)]\nlocal procedure OnAfterIntermediateDocumentReady(ObjectId: Integer; ObjectPayload: JsonObject; DocumentStream: InStream; var TargetStream: OutStream; var Success: Boolean)\nvar\n    JsonText: Text;\n    JsonFile: File;\n    mimeType: Text;\n    ReportIntentJson: JsonToken;\n    ReportIntentTxt: Text;\n    SharedDocumentStream: InStream;\nbegin\n    // Honor the standard handle pattern\n    if Success = true then\n        exit;\n\n    // Empty stream, no actions possible on the stream so return immediately\n    if DocumentStream.Length < 1 then\n        exit;\n\n    // Use a shared stream and reset the read pointer to beginning of stream.\n    SharedDocumentStream := DocumentStream;\n    if SharedDocumentStream.Position > 1 then\n        SharedDocumentStream.ResetPosition();\n\n    // Save the report payload in a text file.\n    ObjectPayload.WriteTo(JsonText);\n    JsonFile.TextMode := true;\n    JsonFile.Create('c:\\temp\\OnAfterDocumentReady.json', TextEncoding::UTF8);\n    JsonFile.Write(JsonText);\n    JsonFile.Close();\n\n    ObjectPayload.Get('intent', ReportIntentJson);\n    ReportIntentTxt := ReportIntentJson.AsValue().AsText();\n\n    case ReportIntentTxt of\n        'Save':\n            HandleSaveScenario();\n        'Download':\n            HandleDownloadScenario();\n        'Print':\n            HandlePrintScenario();\n    end;\n\n    // do something on the document stream and write it back to the serverr.\n    CopyStream(TargetStream, SharedDocumentStream);\n    Success := true;\nend;\n\n```\n## Related information\ndevenv-events-in-al\ndevenv-publishing-events\ndevenv-raising-events\ndevenv-subscribing-to-events"}