Grouping Print Jobs
If a printer is used by more than a single user, you may run into situations where print jobs from different users are mixed in the output bin. Pages that belong to the same print job are printed together without being mixed, but the jobs themselves may come out in a mixed order.
This problem becomes even worse if you have multiple print services or instances of print services. In this case, you cannot be sure that multiple jobs from the same user will be printed in a predictable order. The jobs are printed in the order they are handed to the Windows spooler process by the different instances of the service.
In most use cases, this is not a problem, but you may have situations where it is important to ensure that the order of reports and pages is consistent. Let's say that you are in a warehouse where you ship orders. You are printing an invoice and a shipping list to put in a box with the items to be shipped. Here, the invoice and the shipping list must follow each other and not be mixed up with other orders and shipping lists.
Print jobs on hold
The solution to the print sequence problem described above is to create a direct printer definition and set the initial job status to On Hold.
When this printer is used, jobs are written to the print queue and have the status On Hold.
The user's session ID is also written to the print queue. Session IDs are later used to group jobs, helping us find the jobs that belong together.
Collect the print jobs
When the last print job in the group is in the print queue, it is time to collect all the jobs and mark them ready for printing.
Jobs are collected by calling the GroupSessionPrintJobs function on the queue table.
var
DirPrtQueue: Record "ForNAV DirPrt Queue";
begin
DirPrtQueue.GroupSessionPrintJobs(true);
end;
All jobs with the current session ID are collected, and the result is placed as a new entry on the print queue with the type set to Collection. Collected jobs are marked as finished, and the latest job is marked as ready.
The GroupSessionPrintJobs function takes a parameter that tells the print service how to handle the collection of jobs. The print service can print the jobs individually or merge them before printing them. Regular print jobs are stored as PDFs on the print queue, and combining the separate PDF documents into one PDF before printing will ensure that the printers handle all the documents as one print job. This prevents other service instances from inserting print jobs between the jobs in the collection.
Only jobs based on PDF documents are supported for merging before print. Zebra ZPL jobs do not support this.
When to collect
You decide when to call the function to collect the On Hold jobs. If your documents are printed by a user action on a page, it would make sense to subscribe to the OnAfterActionEvent and then call the GroupSessionPrintJobs function from there. If you don't have access to the page code, this could be done in a page extension.
Here is an example of the code. Remember to modify it to fit your scenario.
[EventSubscriber(ObjectType::Page, Page::"ForNAV Reports", 'OnAfterActionEvent', 'Run', true, true)]
local procedure MyOnAfterActionEvent()
var
DirPrtQueue: Record "ForNAV DirPrt Queue";
begin
DirPrtQueue.GroupSessionPrintJobs(true);
end;
Multiple printers
Print jobs are grouped per printer. This means that if you have jobs on hold for multiple printers in your session, the system will create a collection for each printer in the job queue.
Note
Grouping of printjobs are available since extension version 7.4.0.3 and binaries version 7.4.0.2576.