{"title":"OnPreRendering (Report) trigger","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/triggers-auto/report/devenv-onprerendering-report-trigger/","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/triggers-auto/report/devenv-onprerendering-report-trigger, 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/triggers-auto/report/devenv-onprerendering-report-trigger","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":"Runs when a report dataset has been finalized and before the output artifact has been rendered.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":157,"chars":297},{"level":3,"title":"Parameters","slug":"parameters","at":253,"chars":201},{"level":2,"title":"Remarks","slug":"remarks","at":454,"chars":986},{"level":2,"title":"Example","slug":"example","at":1440,"chars":4385},{"level":2,"title":"Related information","slug":"related-information","at":5825,"chars":290}],"text":"> Version: _Available or changed with runtime version 15.0._\nRuns when a report dataset has been finalized and before the output artifact has been rendered.\n## Syntax\n```al\ntrigger OnPreRendering(var RenderingPayload: JsonObject)\nbegin\n    ...\nend;\n```\n### Parameters\nRenderingPayload\nType: ../../methods-auto/jsonobject/jsonobject-data-type\nThe JSON object containing post rendering setup which will be applied by the runtime during artifact rendering.\n## Remarks\nThe `OnPreRendering trigger` runs after the OnPreDataItem trigger for last dataitem and before OnPostReporttrigger.\nThe trigger collects information needed for performing the following operations :\n Attach document for PDF targets\n Append other documents to the current output artifact\n Protect the output artifact with user and admin passwords.\nYou add code to the trigger to specify the output modifications you want&mdash;attach, additional documents, or protect&mdash;according to the ../../devenv-post-process-report-pdf#report-rendering-payload-schema-definition.\nThe trigger collects report rendering data in a JSON payload. It doesn't invoke processing instructions directly. Instead, it builds the JSON object with a list of attached and appended files, and user or admin passwords. The platform uses this payload to apply the requested changes to the report PDF output.\nSee Attach files, append, and protect report PDFs in AL<../../devenv-post-process-report-pdf>.\n## Example\n> NOTE\n> The capability in described in this article works with both Business Central online and on-premises. However, some parts of this example apply to on-premises only. For more detailed examples, go to Business Central Tech Samples on GitHub<https://github.com/microsoft/BCTech/tree/master/samples/ReportTech/PdfAttachments>.\n> Sample code that shows how use the OnPreRendering report trigger to manage PDF attachments for electronic invoicing in Business Central is available in the BCTech<https://github.com/microsoft/BCTech/tree/master/samples/ReportTech/PdfAttachments> repository.\nThe following AL code example create a report extension object that modifies the Customer - List report PDF output to include an attached file and append it with the Customer - Top 10 List report. In the OnPreRendering trigger of the report extension, the code:\n Adds a version number to the rendering payload.\n Creates a sample XML file and attaches it as a file to the report's PDF output.\n  To view the attachment in the rendered PDF, select Send to > PDF on the report request page, download the PDF, and open it in a PDF viewer like Adobe Reader.\n Generates a PDF file of the Customer - Top 10 List report and appends it to the main report output as an additional document.\n  To observe the Customer - Top 10 List report in the rendered PDF, select any of these actions from the request page: Send to PDF, Preview, Print.\n Adds password protection information to the rendering payload.\n  To test the password on the rendered PDF, use the Send to PDF action from the request page and open downloaded file.\nThe code uses AL's JsonObject and JsonArray types to build the JSON payload for the report rendering engine. The output includes attachments, additional documents, and protection settings.\n```al\nreportextension 50136 MyCustRepExtension extends \"Customer - List\"\n{\n    dataset\n    {\n    }\n\n    requestpage\n    {\n    }\n\n    rendering\n    {\n        layout(LayoutName)\n        {\n            Type = Word;\n            LayoutFile = 'mycustlayout.docx';\n        }\n    }\n\n    trigger OnPreRendering(var RenderingPayload: JsonObject)\n    var\n        Name: Text;\n        FileName: Text;\n        ProtectionObj: JsonObject;\n        AttachmentsArray: JsonArray;\n        AttachmentObj: JsonObject;\n        AdditionalDocumentsArray: JsonArray;\n        AdditionalDocObj: JsonObject;\n        Top10PdfFile: Text;\n    begin\n        RenderingPayload.Add('version', '1.0.0.0');\n\n        // Create a simple attachment object\n        Name := 'sample-attachment.xml';\n        FileName := CreateXmlFile(Name, 'This is a sample xml file');\n        AttachmentObj.Add('name', Name);\n        AttachmentObj.Add('filename', FileName);\n        AttachmentObj.Add('description', 'This is a sample xml document');\n        AttachmentObj.Add('relationship', 'Data');\n        AttachmentObj.Add('mimetype', 'text/xml');\n        // Add the attachment object to rendering payload\n        AttachmentsArray.Add(AttachmentObj);\n        RenderingPayload.Add('attachments', AttachmentsArray);\n\n        // Create PDF of Customer - Top 10 List report for appending\n        Top10PdfFile := CreatePdfFile('CustomerTop10.pdf', '');\n        // Add PDF to rendering payload\n        AdditionalDocumentsArray.Add(Top10PdfFile);\n        RenderingPayload.Add('additionalDocuments', AdditionalDocumentsArray);\n\n        // Create the protection object with user password\n        ProtectionObj.Add('user', 'UserPasswordHere');\n        // Add protection object to rendering payload\n        RenderingPayload.Add('protection', ProtectionObj);\n    end;\n\n    local procedure CreatePdfFile(Filename: Text; Content: Text) FilePath: Text\n    begin\n        FilePath := System.TemporaryPath + Filename;\n        Report.SaveAsPdf(Report::\"Customer - Top 10 List\", FilePath);\n    end;\n\n    local procedure CreateXmlFile(Filename: Text; Content: Text) FilePath: Text\n    var\n        FileObject: File;\n        OutStream: OutStream;\n    begin\n        FilePath := System.TemporaryPath + Filename;\n        FileObject.TextMode := true;\n        FileObject.Create(FilePath);\n        FileObject.CreateOutStream(OutStream);\n        OutStream.WriteText('<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>');\n        OutStream.WriteText('<root><test>');\n        OutStream.WriteText(Content);\n        OutStream.WriteText('</test></root>');\n    end;\n}\n```\n## Related information\ndevenv-oninitreport-report-trigger\ndevenv-onprereport-report-trigger\ndevenv-onpostreport-report-trigger\nReport triggers and runtime operations<../../devenv-report-triggers>\nGet started with AL<../../devenv-get-started>\nDeveloping extensions<../../devenv-dev-overview>"}