Table of Contents

Set Copies From AL-Code

When a print job is created, it uses the settings on the direct printer. If you want to change these settings for a specific print job, you can follow the example shown here.

This example sets the number of copies on a print job from AL-code. Usually, the number of copies is specified on the direct printer or defined in the report itself. When you set the number of copies on the report, the output is replicated to match the number of copies you requested. This is a simple way to do it, but if you have many copies, the output PDF will grow and make handling impractical. It would be better if the PDF in the print job contained one copy of the document, and then the printer was told to replicate that. This is where the number of copies set on the direct printer comes into play.

Copies setting

The number of copies on the direct printer definition controls how many times the print service prints the job. Some printer drivers accept a setting for copies. We can send the job to the printer once and tell it to print several copies. Transmitting the job to the printer only once improves the copy operation's performance. For printers that do not support the copies setting, the print service will repeat printing the job the specified number of times. In rare cases, you may have a printer with a poorly written driver that claims to support the copies setting but ignores it anyway. Here, you can force the copy mode to repeat the print job by setting the direct printer's copy mode property to Repeat.

Sample code

The code in this example will create a print job on the print queue. It will use a PDF from a BASE64 string, but this could also be the result of running a report with the SaveAs function.

Printer settings are read from the direct printer definition. The settings are a JSON object, where the Copies value is changed to the number of copies we would like to print.

var
    DirPrtQueue: Record "ForNAV DirPrt Queue";
    LocalPrinter: Record "ForNAV Local Printer";
    Base64Convert: Codeunit "Base64 Convert";
    TempBlob: Codeunit "Temp Blob";
    pdfInStream: InStream;
    pdfOutStream: OutStream;
    directPrinterName: Text;
    printerSettings: Text;
    printerSettingsObj: JsonObject;
begin
    // Get printer settings
    directPrinterName := 'Office';
    LocalPrinter.Get(directPrinterName);
    printerSettingsObj := LocalPrinter.PrinterSetting();
    printerSettingsObj.Replace('Copies', 2);
    printerSettingsObj.WriteTo(printerSettings);

    // Get PDF stored base64 string
    TempBlob.CreateOutStream(pdfOutStream);
    Base64Convert.FromBase64(GetSamplePdfBase64(), pdfOutStream);

    // Create an InStream with the PDF
    TempBlob.CreateInStream(pdfInStream);

    // Create a print job on the print queue
    DirPrtQueue.Create(0, LocalPrinter."Cloud Printer Name", LocalPrinter."Local Printer Name", pdfInStream, printerSettings, 'Test with copies', DirPrtQueue.ContentType::PDF);
end;