Change Local Printer Settings From AL-Code
When a report is printed with direct print, Business Central needs to translate the printer selected by the user (or on the direct printer definition) into a local printer name and a set of printer settings. This translation is done through an integration event, so you can override or extend it from your own extension.
The event
The event is defined on codeunit "ForNAV Report Management":
[IntegrationEvent(false, false)]
procedure GetLocalPrinter2(CloudPrinterName: Text[250]; var LocalPrinterName: Text; var PrinterSettings: Text; var IsPrintService: Boolean)
begin
end;
It is raised right before a report is printed, no matter if it is printed from the report request page or from AL-code with Report.Print.
CloudPrinterNameis the name of the printer selected in Business Central. This is usually the direct printer's name.LocalPrinterNameshould be set to the name of the Windows printer, or a UNC path for a network printer.PrinterSettingsis a JSON object (as text) describing paper size, tray, copies, scaling, and so on. See Direct printer for what each setting means.IsPrintServiceshould be set totrueif the job must be queued for service-print. If leftfalse(andLocalPrinterNameis set), the job is downloaded to the client instead (client-print).
The built-in subscriber for this event resolves these values from the direct printer definition (table "ForNAV Local Printer"). Your own subscriber runs alongside it, so you can either provide values for printers that are not defined as direct printers, or override values that were already set.
Note
There is also an older event, GetLocalPrinter, without the IsPrintService parameter. Use GetLocalPrinter2 for new code.
Example: changing the printer tray
In this example, we look up the direct printer's normal settings and then override the paper tray for a specific direct printer, so that print jobs sent to it always come from the lower tray, no matter what tray is configured on the direct printer itself.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"ForNAV Report Management", 'GetLocalPrinter2', '', false, false)]
local procedure OnGetLocalPrinter2(CloudPrinterName: Text[250]; var LocalPrinterName: Text; var PrinterSettings: Text; var IsPrintService: Boolean)
var
LocalPrinter: Record "ForNAV Local Printer";
printerSettingsObj: JsonObject;
begin
if CloudPrinterName <> 'Office' then
exit;
if not LocalPrinter.Get(CloudPrinterName) then
exit;
LocalPrinterName := LocalPrinter."Local Printer Name";
IsPrintService := LocalPrinter.IsPrintService;
printerSettingsObj := LocalPrinter.PrinterSetting();
printerSettingsObj.Replace('PaperSourceKind', 'Lower');
printerSettingsObj.WriteTo(PrinterSettings);
end;
PaperSourceKind accepts the standard Windows tray names, such as Upper, Lower, Middle, Manual, Envelope, AutomaticFeed, TractorFeed, SmallFormat, LargeFormat, LargeCapacity, and Cassette.
Using a printer-specific tray
Some printers do not support the standard tray names above. In that case, set PaperSourceKind to Custom and add a CustomPaperSourceName with the printer-specific tray name (this is the same name the print service reports back to Business Central when it scans a printer's trays):
printerSettingsObj.Replace('PaperSourceKind', 'Custom');
printerSettingsObj.Replace('CustomPaperSourceName', 'Tray 2');
Note
Requires Customizable Report Pack 7.4.0.0 or newer.
Example code
You can find example code in the following repository: https://github.com/fornav/ForNav.Demo.PrinterSettings