Preview and Download Selected Documents as PDFs in Business Central Using AL
Preview and Download Selected Documents as PDFs in Business Central Using AL
Introduction
In Microsoft Dynamics 365 Business Central, users frequently need to generate and review Purchase Order (PO) documents. Traditionally, this process involved downloading PDF files locally and then opening them with an external PDF viewer. While functional, this workflow can be inefficient, especially when reviewing multiple purchase orders.
With recent enhancements in the AL language and web client capabilities, it is now possible to preview PDF documents directly within the browser, eliminating unnecessary steps and improving user experience. Additionally, Business Central continues to support direct file downloads for scenarios where saving a copy locally is required.
This article presents a customization to the Purchase Order List page, allowing users to select multiple purchase orders and either preview or download their PDF documents using AL code.
Functional Overview
The proposed solution introduces a new action on the Purchase Order List page titled "Preview Selected Purchase Orders". This action performs the following tasks:
Captures the user's selection of Purchase Orders.
Utilizes Report Selections to determine the correct report and layout for each vendor.
Generates PDF output using the selected report.
Either:
Previews the document in a browser tab (File.ViewFromStream), or
Prompts the user to download the file (File.DownloadFromStream).
Role of Report Selections
Report Selections play a vital role in ensuring flexibility and modularity. Instead of hardcoding specific report IDs, the system determines the report to be used for each Purchase Order based on vendor configuration.
Example AL Snippet:
This method respects configurations made in the Report Selection – Purchase page, allowing different vendors to use different report formats or layouts for the same document type.
AL Implementation
Below is the complete AL code for the pageextension object:
File Handling Options: Preview vs Download
Depending on business needs, developers can choose between two methods:
1. File.ViewFromStream
Opens the PDF in a new browser tab.
Useful for immediate review and internal validation.
Does not store the file locally.
2. File.DownloadFromStream
Prompts the user to save the file to their device.
Appropriate for archival, printing, or external sharing.
Output
This customization provides two ways to handle PDF outputs for Purchase Orders:
1. For a Single Selected Document
When a single purchase order is selected and the action is triggered:
A PDF is generated using the correct layout.
2. For Multiple Selected Documents (Merged into One PDF)
All selected Purchase Orders are processed sequentially.
Instead of generating separate files, the reports are merged into a single PDF document using AL's TempBlob and SaveAsPDF functionality.
Business Benefits
Improved Usability: Users can review documents without navigating away or opening additional tools.
Time-Saving: Reduces steps involved in document handling.
Flexible Output: Honors vendor-specific layouts via Report Selections.
Enhanced UX: Seamless integration within the Business Central web client.
Conclusion
By leveraging AL capabilities such as Report Selections, Temp Blob, and the File data type methods, developers can significantly enhance document handling processes in Microsoft Dynamics 365 Business Central. Offering both in-browser preview and direct download options provides users with flexibility and improves overall productivity.
This customization is a practical example of how small enhancements can deliver substantial value in day-to-day business operations.