Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article describes the syntax of the OnAfterDocumentDownload event and the attributes of the report payload.
Usage
Use the OnAfterDocumentDownload event to specify what happens when the user has generated a report artifact by stream or file, from code or a request page action. The OnAfterDocumentDownload event is used to enable document patching scenarios in the application.
Publisher
Codeunit 44 ReportManagement.
Raised
When the report runtime has generated an output artifact, which is going to be downloaded to a UI client. This can occur when the user invokes one of the SendTo actions in the report request page, or when the document is being printed using the client print capability.
Syntax
[IntegrationEvent(false, false)]
local procedure OnAfterDocumentDownload(ObjectId: Integer; ObjectPayload: JsonObject; DocumentStream: InStream; var Success: Boolean)
Parameters
ObjectID
Type: Integer
The ID of the report object to be run.
ObjectPayload
Type: JsonObject
Instance of the report payload. For more information, see Report payload structure.
DocumentStream
Type: InStream
A stream object that contains the generated artifact. The actual data type can be found from the mime type in the report payload document.
Success
Type: Boolean
Specifies whether the extension handled the generated artifact successfully. The document will not be downloaded to the client if the application code handles the artifact and sets the Success parameter to true.
Report payload
The 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.
{
"filterviews":
[
{"name":"Header","tableid":112,"view":"VERSION(1) SORTING(Field3) WHERE(Field3=1(103027))"},
{"name":"Line","tableid":113,"view":"VERSION(1) SORTING(Field3,Field4) WHERE(Field4=1(0..10000))"},
{"name":"ShipmentLine","tableid":7190,"view":"VERSION(1) SORTING(Field1,Field2,Field3) WHERE(Field2=1(10000))"}
],
"version":1,
"objectname":"Standard Sales - Invoice",
"objectid":1306,
"documenttype":"application/pdf",
"invokedby":"00000000-0000-0000-0000-000000000001",
"invokeddatetime":"2020-01-17T15:33:52.48+01:00",
"companyname":"CRONUS International Ltd.",
"printername":"My Printer",
"duplex":false,
"color":false,
"defaultcopies":1,
"papertray":
{
"papersourcekind":257,
"paperkind":0,
"landscape":false,
"units":0,
"height":1268,
"width":929
},
"intent":"Print",
"layoutmodel": "Rdlc",
"layoutname": "RDLCLayout",
"layoutmimetype": "Application/ReportLayout/Rdlc",
"layoutapplicationid": "00001111-aaaa-2222-bbbb-3333cccc4444",
"reportrunid": "6a3742c1-65ef-44a6-af58-3ef4a902932b"
}
Attributes
objectname
Specifies the name of the object.
objectid
Specifies the ID of the object.
documenttype
Specifies 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.
invokedby
Specifies the ID of the user who invoked the print action.
invokeddatetime
Specifies 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.
companyname
Specifies the Business Central company.
version
Specifies the version of the report payload. Currently, the only supported version for the payload is 1.
duplex
Specifies whether to use duplex (2-sided) printing. true specifies duplex printing; otherwise false.
Note
duplex is currently not used and ignored at runtime.
color
Specifies whether color printing is set. true specifies color printing; otherwise false.
Note
color is currently not used and ignored at runtime.
defaultcopies
Specifies the number of copies to print by default. The default is 1.
Note
defaultcopies is currently not used and ignored at runtime.
filterviews
Specifies views that are used on the document.
name
Specifies the name of data item that the filter view applies to.
tableid
Specifies the ID of the table for the view.
view
Specifies the name of the view.
papertray
Specifies the paper tray settings from the printer payload.
papersourcekind
Specifies 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).
paperkind
Specifies the paper size. The value can be either the text representation (such as A4) or the corresponding number (such as 9).
landscape
Specifies whether landscape orientation is used. true specifies landscape orientation; otherwise false.
units
Specifies the units of measurement for values in height and width attributes. Valid values include:
HIfor hundredths of an inch (default value).TIfor thousandths of an inch.INfor inches.CMfor centimeters.MMfor millimeters.PCfor picas.PTfor points.
height
Specifies the height of the paper. The values include positive integers up to 2,147,483,647.
width
Specifies the width of the paper. The values include positive integers up to 2,147,483,647.
intent
Specifies the intent of the current report invocation, which can be one of the following values.
- None
- Preview
- Save
- Schedule
- Download
- Parameters
layoutmodel
Specifies the layout model selected for the current report invocation, which can be one of the following values.
- Rdlc
- Word
- Excel
- Custom
layoutname
Specifies the layout name for the currently selected layout.
layoutmimetype
Specifies the layout mimetype for the currently selected layout. The built-in layout types are:
- Application/ReportLayout/Rdlc
- Application/ReportLayout/Word
- Application/ReportLayout/Excel
layoutapplicationid
Specifies the ID of the application that provides the selected layout.
reportrunid
Specifies 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.
Sample code
Save the JSON payload to text file in the temporary folder and save the report artifact to a file. Skip the client download by setting Success to true.
[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnAfterDocumentDownload', '', true, true)]
local procedure OnAfterDocumentDownload(ObjectId: Integer; ObjectPayload: JsonObject; DocumentStream: InStream; var Success: Boolean)
var
JsonText: Text;
JsonFile: File;
ObjectName: JsonToken;
DocumentTypeParts: List of [Text];
DocumentType: JsonToken;
ReportIntentJson: JsonToken;
ReportIntentTxt: Text;
FileHandler: File;
FileContent: OutStream;
FilePath: Text;
FileName: Text;
Extension: Text;
TempFolderPath: Text;
SharedDocumentStream: InStream;
begin
// Honor the standard handle pattern
if Success = true then
exit;
// Empty stream, no actions possible on the stream so return immediately
if DocumentStream.Length < 1 then
exit;
// Use a shared stream and reset the read pointer to beginning of stream
SharedDocumentStream := DocumentStream;
if SharedDocumentStream.Position > 1 then
SharedDocumentStream.ResetPosition();
TempFolderPath := System.TemporaryPath;
ObjectPayload.WriteTo(JsonText);
JsonFile.TextMode := true;
JsonFile.Create(TempFolderPath + 'OnAfterDocumentDownload.json', TextEncoding::UTF8);
JsonFile.Write(JsonText);
JsonFile.Close();
ObjectPayload.Get('intent', ReportIntentJson);
ReportIntentTxt := ReportIntentJson.AsValue().AsText();
case ReportIntentTxt of
'Download':
HandleDownloadScenario();
'Preview':
HandlePreviewScenario();
'Print':
HandlePrintScenario();
end;
ObjectPayload.Get('objectname', ObjectName);
FileName := ObjectName.AsValue().AsText();
ObjectPayload.Get('documenttype', DocumentType);
DocumentTypeParts := DocumentType.AsValue().AsText().Split('/');
Extension := DocumentTypeParts.Get(DocumentTypeParts.Count);
FilePath := TempFolderPath + 'DownLoad - ' + FileName + '.' + Extension;
FileHandler.Create(FilePath);
FileHandler.CreateOutStream(FileContent);
CopyStream(FileContent, SharedDocumentStream);
FileHandler.Close();
Success := true; // Return true so platform don't attempt to download to client
end;
Related information
Events in AL
Publishing Events
Raising Events
Subscribing to Events