Printing in ABAP

The expression printing in ABAP means that the output from a report is sent to the spool system. This does not necessarily mean that it is printed. You can also use the spool system to save a list for a while. You can also write a list to the spool system exclusively for subsequent optical archiving.

In ABAP, there are two fundamentally different ways of printing a list:

  1. Printing a list after it has been created
  2. Printing a list while it is being created

Printing a list after it has been created

This is the method you use when you execute a report and then print it by choosing System→List→Print.

This method has certain problems:

  1. Since the list is formatted for screen display, it is not usually suitable for printing:
    1. A normal online list only has one logical page. When you print it, the system has to divide the single logical page into several physical pages. Since the screen list only has one page header, the same header is used on each printed page. If the page header contains a page number, this will appear on every page of the printed report (usually page 1).
    2. Screen lists are usually wide (more than 130 columns). You can only print lists this wide on standard printers by splitting lines.
  2. The screen list does not contain special print control sequences, output with the PRINT-CONTROL statement. They can therefore not bhe used when you print the screen list.

This method of printing lists would be better called a list dump. Practically its only use is to generate an extract from the screen list for test purposes.

Printing a list while it is being created

The best way to print a list is to format it for printing right from the start. This works by printing each page and then deleting the page. Since the list never exists in its entirity, you can print large amounts, since the output is only limited by the capacity of the spool system. Because the printing works in this way, you cannot refer to a previous page while you are printing. You can print a list while it is being created as follows:

  1. Execute a report using the Execute and print function
  2. Switch on printing in a report using the ABAP statement NEW-PAGE PRINT ON.
  3. Call a report using the statement SUBMIT rep TO SAP-SPOOL
  4. Schedule a report as a background job using the statement SUBMIT rep USER user VIA JOB job NUMBER n TO SAP-SPOOL. When the report runs, the system writes the output to the spool system.
  5. Schedule a report as a background job using the function module JOB_SUBMIT. When the report runs, the sytsem writes the output to the spool system.

Print Parameters

When you start printing, you need to set the print parameters. You do this using the ABAP Dictionary structure PRI_PARAMS.

The most important print parameters are:

  1. Print details:
    1. Output device
    2. Number of copies
  2. Spool identification:
    1. Name of spool request
    2. Text for cover sheet
  3. Spool control:
    1. Delete after printing,,
    2. Archiving mode (1 = print, 2 = archive, 3 = print and archive).
    3. New spool request (do not append to an existing request)
  4. Format details:
    1. Number of columns on a printed page
    2. Lines per page
    3. Print format
  5. Cover sheet details
    1. Output cover sheet Yes/No
    2. User name for cover sheet
    3. Department name for cover sheet

    As well as the print parameters, there are archiving parameters. You only have to specify these if you have optical archiving switched on (archiving mode 2 or 3). The structure of the archiving parameters is described in the ABAP Dictionary structure ARC_PARAMS.

    You can only set the print and archiving parameters using the function module GET_PRINT_PARAMETERS. If you set the parameters directly, the system triggers a runtime error when you next use the parameterse. The function module GET_PRINT_PARAMETERS allows you to do the following:

    1. Determine the default print parameters
    2. Determine the current print parameters during printing
    3. Change individual print parameters
    4. Query the print parameters interactively

    Control printing

    While printing is active, you can use the PRINT-CONTROL statement to write the list information to the spool system. This is mostly control statements for the output devices (printer) or administration information for optical archiving.

    Setting the top and left-hand margins

    The SET MARGIN x y statement allows you to set the upper and left-hand margins on a print page. The statement sets the system fields SY-MAROW and SY-MACOL, which set the number of lines at the top margin and the number of columns at the left-hand margin.

    Examples:

    * Uusual call of NEW-PAGE PRINT ON
    DATA: PARAMS LIKE PRI_PARAMS,
          VALID  TYPE C.

    CALL FUNCTION 'GET_PRINT_PARAMETERS'
      IMPORTING OUT_PARAMETERS = PARAMS
                VALID          = VALID.
    IF VALID <> SPACE.
      NEW-PAGE PRINT ON PARAMETERS PARAMS NO-DIALOG.

      WRITE / ...

      NEW-PAGE PRINT OFF.
    ENDIF.

    * NEW-PAGE PRINT ON with optical archiving
    DATA: PRI_PARAMS LIKE PRI_PARAMS,
          ARC_PARAMS LIKE ARC_PARAMS,
          VALID      TYPE C.

    CALL FUNCTION 'GET_PRINT_PARAMETERS'
      IMPORTING OUT_PARAMETERS         = PRI_PARAMS
                OUT_ARCHIVE_PARAMETERS = ARC_PARAMS
                VALID                  = VALID.
    IF VALID <> SPACE.
      NEW-PAGE PRINT ON PARAMETERS         PRI_PARAMS
                        ARCHIVE PARAMETERS ARC_PARAMS NO-DIALOG.
      PRINT-CONTROL INDEX-LINE '   '.   "Write administration information

      WRITE / ....

      NEW-PAGE PRINT OFF.
    ENDIF.

    * SUBMIT ... TO SAP-SPOOL
    DATA: PARAMS LIKE PRI_PARAMS,
          VALID  TYPE C.

    CALL FUNCTION 'GET_PRINT_PARAMETERS'
      IMPORTING OUT_PARAMETERS = PARAMS
                VALID          = VALID.
    IF VALID <> SPACE.
      SUBMIT MYREPORT TO SAP-SPOOL WITHOUT SPOOL DYNPRO
                      SPOOL PARAMETERS PARAMS.
    ENDIF.

    * Einplanen eines Hintergrund-Jobs
    DATA: PARAMS LIKE PRI_PARAMS,
          VALID  TYPE C.

    CALL FUNCTION 'GET_PRINT_PARAMETERS'
      EXPORTING MODE                  = 'BATCH'
                REPORT                = 'MYREPORT'
      IMPORTING OUT_PARAMETERS        = PARAMS
                VALID                 = VALID.
    IF VALID <> SPACE.
      CALL FUNCTION 'JOB_OPEN' ....  EXPORTING JOBCOUNT ...
      SUBMIT MYREPORT VIA JOB 'MY_JOB' NUMBER JOBCOUNT
             TO SAP-SPOOL WITHOUT SPOOL DYNPRO
                SPOOL PARAMETERS PARAMS.
      CALL FUNCTION 'JOB_CLOSE' ...
    ENDIF.