KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Handling print jobs in headless mode
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: September 6, 2022

Typically, the standard behavior whenever a dialog is displayed in headless mode is to abort the process just to be safe. When dealing with any print jobs or with clients with different printers, it's recommended to have defensive code to assure the desired printer and printer destination is properly set prior to printing a form/selection.

For example, the code below will suppress the print dialog when running in headless mode, but allow the save dialog prompt to be displayed when running in non-headless.

C_TEXT($destPath_t)
C_TEXT($setPrinter_t)
C_OBJECT($appInfo_o)
ARRAY TEXT($printers_at; 0)

ALL RECORDS([Table_1])

PRINTERS LIST($printers_at)
$setPrinter_t:="Microsoft Print to PDF"

If (Find in array($printers_at; $setPrinter_t)>0)
   SET CURRENT PRINTER($setPrinter_t)
   $destPath_t:=System folder(Desktop)+"test.pdf"
   SET PRINT OPTION(Destination option; 2; $destPath_t)
  
   // Suppress dialog in headless mode, otherwise allow Save Dialog prompt in non-headless
   $appInfo_o:=Get application info
   If ($appInfo_o.headless)
     PRINT SELECTION([Table_1]; >)
   Else
     PRINT SELECTION([Table_1])
   End if
End if


Alternatively, ON ERR CALL could also be used to allow the code to continue upon encountering an error instead of having the process abort in headless mode. The decision will be up to the developer.