How to document the ALV output into a PDF file ( using spool )

A+ A-

This document shows you how to download the ALV output into a PDF file from program level, in an easier way.

REPORT  ztest_create2.
TYPE-POOLS:slis.
TYPES: BEGIN OF ty_t001,
         bukrs TYPE t001-bukrs,
         butxt TYPE t001-butxt,
        ort01 TYPE t001-ort01,
         land1 TYPE t001-land1,
         waers TYPE t001-waers,
        END  OF ty_t001.
DATA: it_t001      TYPE TABLE OF ty_t001,
       it_pdf       LIKE tline OCCURS 0,
       it_fieldcat  TYPE slis_t_fieldcat_alv.
DATA: g_spool      TYPE tsp01-rqident,
      g_program    TYPE sy-repid VALUE sy-repid.
DATA: w_print       TYPE slis_print_alv,
      w_print_ctrl  TYPE alv_s_pctl,
      w_pri_params  TYPE pri_params.
** Selection screen
PARAMETERS: p_file TYPE string.
** Initialization
INITIALIZATION.
  p_file = 'C:\Users\mayur.priyan\Desktop\CHECK.pdf'.
** Start of Selection
START-OF-SELECTION.
  SELECT bukrs
         butxt
         ort01
         land1
         waers
    FROM t001
    INTO TABLE it_t001 UP TO 100 ROWS.
  w_print-print = 'X'.
* Function Call to get print parameters
  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
      mode                   = 'CURRENT'
      no_dialog              = 'X'
      user                   = sy-uname
    IMPORTING
      out_parameters         = w_pri_params
    EXCEPTIONS
      archive_info_not_found = 1
      invalid_print_params   = 2
      invalid_archive_params = 3
      OTHERS                 = 4.
* To capture the Spool Request
  NEW-PAGE PRINT ON PARAMETERS w_pri_params NO DIALOG.
  PERFORM fill_catalog_alv1 USING:
     'BUKRS'     'T001'    'BUKRS',
     'BUTXT'     'T001'    'BUTXT',
     'ORT01'     'T001'    'ORT01',
     'LAND1'     'T001'    'LAND1',
     'WAERS'     'T001'    'WAERS'.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = g_program
      it_fieldcat        = it_fieldcat
    TABLES
      t_outtab           = it_t001.
  IF sy-subrc EQ 0.
    NEW-PAGE PRINT OFF.
    g_spool = sy-spono.
    CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
      EXPORTING
        src_spoolid = g_spool
        dst_device  = 'LOCL'
      TABLES
        pdf         = it_pdf.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ELSE.
      CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
          filename = p_file
          filetype = 'BIN'
        TABLES
          data_tab = it_pdf.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2.
      ELSE.
        WRITE: / 'Downloaded document successfully'.
      ENDIF.
    ENDIF.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  fill_catalog_alv1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_FIELD_NAME   text
*      -->P_REFTAB_NAME  text
*      -->P_REF_FIELD    text
*----------------------------------------------------------------------*
FORM fill_catalog_alv1 USING p_field_name  TYPE slis_fieldname
                             p_reftab_name TYPE slis_tabname
                             p_ref_field   TYPE slis_fieldname.
  DATA: ls_field_alv TYPE slis_fieldcat_alv.
* Filling the field catalog values.
  ls_field_alv-fieldname     = p_field_name.
  ls_field_alv-ref_tabname   = p_reftab_name.
  ls_field_alv-ref_fieldname = p_ref_field.
  APPEND ls_field_alv TO it_fieldcat.
ENDFORM.                    " FILL_CATALOG_ALV1