Table of Contents

Example - Notepad

The Notepad example is another example where we will use a handler that runs in the user's context instead of that of the print and file service.

It will send a text and open it in the user's Notepad.

This example uses one-way communication. It sends a package to a locally installed handler but does not expect anything in return.

Direct printer for handler

The direct printer for this sample is called NotepadSample. It will refer to the script in the NotepadHandler folder on the user's computer.

Please note that it should not queue the jobs. Instead, the job is downloaded and run on the client where the browser is running.

Direct printers

Handler folder

On the user's computer, we place our handler.ps1 script in the handler folder defined in the direct printer for the handler.

C:\Program Files\Reports ForNAV\Handlers\NotepadHandler

Handler script file

Here is the Powershell script for the handler.ps1 file.

# Notepad handler
$inputfile = $args[0]
$package = Get-Content -Raw $inputfile

# Save message to a temp file
$tmpFile = New-TemporaryFile
Set-Content -Path $tmpFile -Value $package

# Start Notepad
Start-Process 'C:\windows\system32\notepad.exe' -ArgumentList $tmpFile 

It will read the package from the file name in the first command line argument.

The content is written to a new temporary file and opened in Notepad.

In theory, you could skip the part where it is saved to a temporary file and open the package directly.

Calling the handler from Business Central

In the AL-code, we create a package for the handler. The package contains a message with the current time. We use a BigText for the message and create an InStream with the text for the call to Create on the ForNAV DirPrt Queue table.

The code ends after the package is created. In this case, we do not wait for a result.

trigger OnAction()
var
    DirPrtQueue: Record "ForNAV DirPrt Queue";
    tempBlob: Codeunit "Temp Blob";
    fileContentBigText: BigText;
    packageInStream: InStream;
    packageOutStream: OutStream;
begin
    // Create the package for the local handler
    fileContentBigText.AddText('Hello World! The time is ' + Format(Time()));

    // Create a print job
    tempBlob.CreateOutStream(packageOutStream, TextEncoding::UTF8);
    fileContentBigText.Write(packageOutStream);
    tempBlob.CreateInStream(packageInStream);
    DirPrtQueue.Create('Handler sample with Notepad',
        'NotepadSample', packageInStream,
        DirPrtQueue.ContentType::Package);
end;