Printing a Document (Windows)

On Windows the server command (specified in the <SERVER_COMMAND> element) is not executed in a command shell unless explicitly invoked via the Windows command interpreter (cmd.exe) and this is necessary in order to use such facilities as pipes and redirection. The configuration described here is representative for Windows platforms.

Depending on the file type, your printing requirements, and the target printer there are a number of possible options and configurations for printing on Windows. For instance, your particular version of Adobe Reader may allow for direct printing or your printer may support direct PDF printing.

A convenient way to implement print functionality is to write a batch file for the Windows command interpreter to invoke and perform any necessary operations and to get the server to execute this batch file. A sample batch file is shown in Printing a Document (Windows) below. Let us assume that the batch file is saved as c:\xmlsrv\xmlserverprint.bat1. The server command can pass parameters to the batch file through the command line and the batch file accesses these as %1 for the first parameter, %2 for the second, etc. These parameters are provided to the batch file via the server command tokens specified in the batch file invocation in the server configuration file and replaced when it is invoked. (See Server Command Configuration and Printing a Document (Windows) for more information on command tokens.)

While Windows applications sometimes allow the use of either forward-slash (/) or back-slash (\) characters interchangeably as a path separator, the Windows command interpreter only allows the \ character. Care must be taken to ensure that all paths that may be visible to the command interpreter use back-slash characters (\) as separators. As path information will not be available in the context of your batch file, commands must have fully specified paths. The interpreters built-in commands do not require a path.

The following example illustrates the use of the sample SimplePrintService class, which is implemented using the Java Print Service API. You could utilize this API for your own custom solution; for instance, to utilize specific printer features in your environment. To print a PDF file using this sample class would require the printer to have direct PDF print support.

Figure 1. Batch File for Printing a Document (Windows)
@ECHO OFF

echo ---------------------------------------------------- ^
  >> XMLServer.log
REM log output
echo File:         %1                     ^
  >> XMLServer.log
echo Print Server: %2                     ^
  >> XMLServer.log

REM Call the system print command
echo Starting Print                       ^
  >> XMLServer.log
echo %JAVA_HOME%\bin\java                 ^
  -cp xmlserver.jar;xmlservercommon.jar   ^
  curam.util.xmlserver.SimplePrintService ^
  %2 "%1" >> XMLServer.log 2>&1
%JAVA_HOME%\bin\java                      ^
  -cp xmlserver.jar;xmlservercommon.jar   ^
  curam.util.xmlserver.SimplePrintService ^
  %2 "%1" >> XMLServer.log 2>&1
echo Printing Completed                   ^
  >> XMLServer.log
echo ---------------------------------------------------- ^
  >> XMLServer.log

Instead of the sample Java program above any appropriate processing could be specified or additional processing prior to printing or cleanup after printing could also be implemented as needed. If you use any command that may send output to the console, make sure that you add null redirection. This output needs to be redirected to the null device or it will cause the command to block and the batch file will hang. Therefore, redirection must be added to the command pointing to the null device; e.g.: > nul:, which avoids the problem of blocking the XML Server. Setting the <USE_STDOUT_SINK> and <USE_STDERR_SINK> elements in the configuration will not work on Windows.

A sample configuration file used to launch this batch file is shown in Printing a Document (Windows) below. Note how the printer name and the details of the temporary file are passed to the batch file using the command tokens.

Figure 2. Configuration for Printing a Document (Windows)
<XML_SERVER_CONFIG>
  <SERVER_PORT>6789</SERVER_PORT>
  <SERVER_COMMAND>
    c:\Windows\System32\CMD.EXE 
    /C c:\xmlsrv\xmlserverprint.bat %d\%f %p
  </SERVER_COMMAND>
  <USE_TMP_FILE>true</USE_TMP_FILE>
  <TMP_FILE_ROOT>temp</TMP_FILE_ROOT>
  <TMP_DIRECTORY>c:\xmlsrv\tmp</TMP_DIRECTORY>
  <DEFAULT_PRINTER>\\MyPC\ps1</DEFAULT_PRINTER>
  ...
</XML_SERVER_CONFIG>

The command interpreter (cmd.exe) uses the/C option to specify a batch file to execute. The batch file is passed two parameters. The first parameter is the name of the temporary PDF file created by concatenating the expanded %d token for the temporary directory name, a back-slash separator, and the expanded %f token for the name of the temporary PDF file. The second parameter is the expanded %p token for the name of the printer. The configuration file also includes a default printer name. But this may be overridden by the client. See Server Command Configuration for a more detailed description of these tokens.

1 Note that you should choose a target destination for setting up your XML Server and its customizations to avoid being overwritten by subsequent service pack updates.