What Function Modules do I need to call to print a file from an APAB program?

1.6k Views Asked by At

I'm currently learning to program in ABAP, and have been given an exercise where I'm stuck. I more or less have it working but I'm pretty sure there's another and better way doing this.

The exercise requirements are:

  • From the selection screen allow a user to upload a file.
  • From the selection screen allow a user to select a printer.
  • When the report is ran the contents of the uploaded file should be sent to the specified printer to be printed.

What I've got so far, which more or less works is this:

*&---------------------------------------------------------------------*
*& Report  Z_SELECT_PRINT_JR                                           *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  z_select_print_jr NO STANDARD PAGE HEADING                       .

DATA: filedata TYPE STANDARD TABLE OF char255,
      fileline TYPE char255.

DATA: valid,
      params TYPE pri_params.


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.

PARAMETERS: p_file TYPE char255.
PARAMETERS: p_prnt TYPE char30.

SELECTION-SCREEN END OF BLOCK b1.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM display_file_selection.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_prnt.
  PERFORM display_printer_selection.


START-OF-SELECTION.
  PERFORM load_file_data.
  PERFORM send_to_printer.
*&--------------------------------------------------------------------*
*&      Form  display_file_selection
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM display_file_selection.
  DATA: lv_return TYPE i,
        lt_filetable TYPE STANDARD TABLE OF file_table,
        ls_filetable TYPE file_table.


  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title      = 'SELECT FILE TO PRINT:'
      default_extension = 'txt'
      file_filter       = 'Text Files (.txt)|*.txt|XML Files (.xml)|*.xml'
      initial_directory = 'C:\demo'
      multiselection    = space
    CHANGING
      file_table        = lt_filetable
      rc                = lv_return
    EXCEPTIONS
      OTHERS            = 1.


  IF lv_return NE 0.
    READ TABLE lt_filetable INTO ls_filetable INDEX 1.
    p_file = ls_filetable-filename.
  ENDIF.
ENDFORM.                    "display_file_selection

*&--------------------------------------------------------------------*
*&      Form  display_printer_selection
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM display_printer_selection.

  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    IMPORTING
      out_parameters         = params
      valid                  = valid
    EXCEPTIONS
      archive_info_not_found = 1
      invalid_print_params   = 2
      invalid_archive_params = 3
      OTHERS                 = 4.
  IF sy-subrc EQ 0.
    p_prnt = params-pdest.
  ENDIF.

ENDFORM.                    "display_printer_selection

*&--------------------------------------------------------------------*
*&      Form  load_file_data
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM load_file_data.

  DATA: filename TYPE string,
        fileleng TYPE i.

  MOVE p_file TO filename.

  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename   = filename
    IMPORTING
      filelength = fileleng
    CHANGING
      data_tab   = filedata.


ENDFORM.                    "load_file_data

*&--------------------------------------------------------------------*
*&      Form  send_to_printer
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM send_to_printer.

  DATA: lv_line TYPE char255,
        ndx TYPE i.

  ndx = 1.

  NEW-PAGE PRINT ON
      PARAMETERS params
      NO DIALOG.

  DO.
    READ TABLE filedata INTO lv_line INDEX ndx.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
    WRITE: / lv_line.
    ndx = ndx + 1.
  ENDDO.

  NEW-PAGE PRINT OFF.

ENDFORM.                    "send_to_printer

When I say this more or less works, basically I'm getting an output to the printer, but if there are empty lines separating text then these are not carried over during the print process. I'm pretty sure that there are some sort of Function Modules that are available, but finding documentation about anything SAP and ABAP related has been very difficult. Does anyone have a better way of performing this action?

1

There are 1 best solutions below

0
On BEST ANSWER

After speaking with a colleague he suggested that I use the following Function Modules:

  • RSPO_SR_OPEN
  • RSPO_SR_TABLE_WRITE
  • RSPO_SR_CLOSE

In this case I'm opening a spool request with the Spool Server of SAP and then writing the table that is filled by the GUI_UPLOAD function. Finally at the end I close the spool request.

This allowed the file to be printed as is expected, but without all the control characters and it remembered to put in the blank lines as they are in the actual text file.