Steps to be followed to convert ALV to PDF
- Develop an ALV report.
- Create custom GUI status.
- Submit the ALV report in background, store output in spool and generate PDF.
Step1: Develop an ALV report.
REPORT ZSAPN_ALV2PDF_MARA. TYPE-POOLS SLIS. TYPES : BEGIN OF TY_MARA, "User defined internal table type MATNR TYPE MARA-MATNR, MTART TYPE MARA-MTART, MBRSH TYPE MARA-MBRSH, MEINS TYPE MARA-MEINS, END OF TY_MARA. DATA : IT_MARA TYPE TABLE OF TY_MARA ."Internal table DATA : WA_MARA TYPE TY_MARA . "work area DATA : IT_FCAT TYPE SLIS_T_FIELDCAT_ALV . "field catalog table DATA : WA_FCAT LIKE LINE OF IT_FCAT . "field catalog work area PARAMETERS : P_MTART TYPE MARA-MTART. "material type input START-OF-SELECTION. **get table data SELECT MATNR MTART MBRSH MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS WHERE MTART = P_MTART. *** generate field catalogue WA_FCAT-COL_POS = '1' . "column position WA_FCAT-FIELDNAME = 'MATNR' . "column name WA_FCAT-TABNAME = 'IT_MARA' . "table WA_FCAT-SELTEXT_M = 'Material' . "Column lable WA_FCAT-KEY = 'X' . "is a key field APPEND WA_FCAT TO IT_FCAT . "append to fcat CLEAR WA_FCAT . WA_FCAT-COL_POS = '2' . WA_FCAT-FIELDNAME = 'MBRSH' . WA_FCAT-TABNAME = 'IT_MARA' . WA_FCAT-SELTEXT_M = 'Industry Sec' . APPEND WA_FCAT TO IT_FCAT . CLEAR WA_FCAT . WA_FCAT-COL_POS = '3' . WA_FCAT-FIELDNAME = 'MTART' . WA_FCAT-TABNAME = 'IT_MARA' . WA_FCAT-SELTEXT_M = 'Material Type' . APPEND WA_FCAT TO IT_FCAT . CLEAR WA_FCAT . WA_FCAT-COL_POS = '4' . WA_FCAT-FIELDNAME = 'MEINS' . WA_FCAT-TABNAME = 'IT_MARA' . WA_FCAT-SELTEXT_M = 'Base.Unit' . WA_FCAT-REF_TABNAME = 'MARA' . APPEND WA_FCAT TO IT_FCAT . CLEAR WA_FCAT . **display ALV CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING I_CALLBACK_PROGRAM = SY-REPID I_CALLBACK_PF_STATUS_SET = 'FORM_MENU' "GU status form IT_FIELDCAT = IT_FCAT "PASS FIELD CATALOG TO ALV TABLES T_OUTTAB = IT_MARA. FORM FORM_MENU USING RT_EXTAB TYPE SLIS_T_EXTAB. *SET PF STATUS to ALV SET PF-STATUS 'MENU'. "GUI status...double click to create ENDFORM. " SET_PF_STATUS
Step2: Create custom GUI status
You can create GUI status in two ways.
- Double click on 'MENU' in from FORM_MENU
- Go to SE41 and create
In our program, we will create using the first one. Double click on MENU and click on YES.
Provide short text and enter.
Expand application toolbar, provide name as EPDF, enter a popup will open just press enter.
Provide function text, icon, info text and enter.
Select a function key and enter.
Expand function keys and add standard toolbar buttons like below.
Save, activate and go back (F3) to program.
Execute the program, you will find the toolbar as below.
Step3: Submit the program, store output in spool and generate PDF.
Implement the logic to submit the program and download data into PDF. The below code has lots of steps, please try to understand based on comments provided.
**for to handle user command FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM RS_SELFIELD TYPE SLIS_SELFIELD. CASE R_UCOMM. WHEN 'EPDF'. "Function code for export which we created in MENU **submit the same program in background and store TYPES: BEGIN OF TY_TSP01, RQIDENT TYPE TSP01-RQIDENT, " spool number RQ2NAME TYPE TSP01-RQ2NAME, " Spool request: Suffix 2 RQCRETIME TYPE TSP01-RQCRETIME, " User name END OF TY_TSP01. DATA: LV_JOBCNT TYPE TBTCJOB-JOBCOUNT, " job number LV_JOBNAME TYPE TBTCJOB-JOBNAME. " job name DATA: LV_LEN TYPE I, LS_PARAM TYPE RSPARAMS, " selection work area LS_TSP01 TYPE TY_TSP01, " Spool Requests work area LT_TSP01 TYPE STANDARD TABLE OF TY_TSP01, " Spool Requests internal table LT_PARAM TYPE RSPARAMS_TT. " Selection table * ********* Derive job counter LV_JOBNAME = 'ZALV2PDF'. "Background job name CALL FUNCTION 'JOB_OPEN' "open a job EXPORTING JOBNAME = LV_JOBNAME IMPORTING JOBCOUNT = LV_JOBCNT EXCEPTIONS CANT_CREATE_JOB = 1 INVALID_JOB_DATA = 2 JOBNAME_MISSING = 3 OTHERS = 4. IF SY-SUBRC EQ 0. DATA : LV_RQDEST TYPE TSP01-RQDEST VALUE 'LP01', LV_LINSZ TYPE SYLINSZ VALUE '9999999'. SUBMIT (SY-REPID) "submit the same program WITH P_MTART = P_MTART TO SAP-SPOOL DESTINATION LV_RQDEST LINE-SIZE LV_LINSZ IMMEDIATELY 'X' KEEP IN SPOOL 'X' USER SY-UNAME VIA JOB LV_JOBNAME NUMBER LV_JOBCNT WITHOUT SPOOL DYNPRO WITH SELECTION-TABLE LT_PARAM AND RETURN. CALL FUNCTION 'JOB_CLOSE' "job close EXPORTING JOBCOUNT = LV_JOBCNT JOBNAME = LV_JOBNAME STRTIMMED = 'X' EXCEPTIONS CANT_START_IMMEDIATE = 1 INVALID_STARTDATE = 2 JOBNAME_MISSING = 3 JOB_CLOSE_FAILED = 4 JOB_NOSTEPS = 5 JOB_NOTEX = 6 LOCK_FAILED = 7 INVALID_TARGET = 8 OTHERS = 9. IF SY-SUBRC <> 0. RAISE JOB_CANNOT_BE_CLOSED. " Raise exception ENDIF. ELSE. RAISE JOB_CANNOT_BE_SUBMITTED. ENDIF. *********** confirm job status if finished DATA: LV_JOB TYPE TBTCV-FIN. " Job status DO 120 TIMES. CALL FUNCTION 'BDL_READ_JOB_STATUS' "get job status EXPORTING JOBNAME = LV_JOBNAME JOBNUMBER = LV_JOBCNT IMPORTING JOBSTATUS = LV_JOB EXCEPTIONS JOB_NOT_FOUND = 1 OTHERS = 2. IF LV_JOB NE 'F'. "job finished WAIT UP TO 1 SECONDS. CONTINUE. ELSE. EXIT. ENDIF. ENDDO. **calculate report name in Spool table DATA: LV_RQ2NAME TYPE TSP01-RQ2NAME. " Spool request DATA: LV_TMP(14) TYPE C, " temp LV_STRING TYPE STRING, " store program name LV_TEMP TYPE TSP01-RQ2NAME, " Spool request LV_SPOOL TYPE TSP01-RQIDENT. " spool number IF LV_JOB EQ 'F'. "job finished LV_LEN = STRLEN( SY-REPID ) . IF LV_LEN >= 9 . CONCATENATE SY-REPID+0(9) SY-UNAME+0(3) INTO LV_RQ2NAME . ELSE. LV_LEN = 9 - LV_LEN. DO LV_LEN TIMES . CONCATENATE LV_TEMP '_' INTO LV_TEMP . ENDDO. CONCATENATE SY-REPID LV_TEMP SY-UNAME INTO LV_RQ2NAME . ENDIF. *Get spool request from SPOOL table TSP01 SELECT RQIDENT RQ2NAME RQCRETIME FROM TSP01 INTO TABLE LT_TSP01 WHERE RQ2NAME = LV_RQ2NAME AND RQOWNER = SY-UNAME . *sort table to find latest spool no SORT LT_TSP01 BY RQCRETIME DESCENDING . *read table to find latest spool no READ TABLE LT_TSP01 INTO LS_TSP01 INDEX 1. *get user desktop CALL METHOD CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY CHANGING DESKTOP_DIRECTORY = LV_STRING EXCEPTIONS CNTL_ERROR = 1 ERROR_NO_GUI = 2 NOT_SUPPORTED_BY_GUI = 3 OTHERS = 4. IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. CALL METHOD CL_GUI_CFW=>UPDATE_VIEW. CONCATENATE LV_STRING TEXT-001 INTO LV_STRING. *Prepare selection table for PDF download LS_PARAM-SELNAME = 'SPOOLNO'. LS_PARAM-SIGN = 'I'. LS_PARAM-OPTION = 'EQ'. LS_PARAM-LOW = LS_TSP01-RQIDENT. LS_PARAM-HIGH = ''. APPEND LS_PARAM TO LT_PARAM. LS_PARAM-SELNAME = 'P_FILE'. LS_PARAM-SIGN = 'I'. LS_PARAM-OPTION = 'EQ'. LS_PARAM-LOW = LV_STRING. LS_PARAM-HIGH = ''. APPEND LS_PARAM TO LT_PARAM. * Submit to PDF converted and download SUBMIT RSTXPDFT4 WITH SELECTION-TABLE LT_PARAM AND RETURN. IF SY-SUBRC <> 0. MESSAGE 'PDF convert not possible' TYPE 'I'. ENDIF. ELSE. MESSAGE 'PDF Can not be downloaded, you have selected huge data. Reduce data and try again' TYPE 'I'. ENDIF. ENDCASE. ENDFORM. "user command
Full and Final code will be
REPORT ZSAPN_ALV2PDF_MARA. TYPE-POOLS SLIS . TYPES : BEGIN OF TY_MARA, "User defined internal table type MATNR TYPE MARA-MATNR, MTART TYPE MARA-MTART, MBRSH TYPE MARA-MBRSH, MEINS TYPE MARA-MEINS, END OF TY_MARA. DATA : IT_MARA TYPE TABLE OF TY_MARA ."internal table DATA : WA_MARA TYPE TY_MARA . "work area DATA : IT_FCAT TYPE SLIS_T_FIELDCAT_ALV . "field catalog table DATA : WA_FCAT LIKE LINE OF IT_FCAT . "field catalog work area TYPES: BEGIN OF TY_TSP01, RQIDENT TYPE TSP01-RQIDENT, " spool number RQ2NAME TYPE TSP01-RQ2NAME, " Spool request: Suffix 2 RQCRETIME TYPE TSP01-RQCRETIME, " User name END OF TY_TSP01. DATA: LV_JOBCNT TYPE TBTCJOB-JOBCOUNT, " job number LV_JOBNAME TYPE TBTCJOB-JOBNAME. " job name DATA: LV_LEN TYPE I, LS_PARAM TYPE RSPARAMS, " selection work area LS_TSP01 TYPE TY_TSP01, " Spool Requests work area LT_TSP01 TYPE STANDARD TABLE OF TY_TSP01, " Spool Requests internal table LT_PARAM TYPE RSPARAMS_TT. " Selection table DATA: LV_RQ2NAME TYPE TSP01-RQ2NAME. " Spool request DATA: LV_TMP(14) TYPE C, " temp LV_STRING TYPE STRING, " store program name LV_TEMP TYPE TSP01-RQ2NAME, " Spool request LV_SPOOL TYPE TSP01-RQIDENT. " spool number DATA: LV_JOB TYPE TBTCV-FIN. " Job status DATA : LV_RQDEST TYPE TSP01-RQDEST VALUE 'LP01', LV_LINSZ TYPE SYLINSZ VALUE '9999999'. *selection Screen PARAMETERS : P_MTART TYPE MARA-MTART. "material type input START-OF-SELECTION. **get table data SELECT MATNR MTART MBRSH MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS WHERE MTART = P_MTART. *** generate field catalogue WA_FCAT-COL_POS = '1' . "column position WA_FCAT-FIELDNAME = 'MATNR' . "column name WA_FCAT-TABNAME = 'IT_MARA' . "table WA_FCAT-SELTEXT_M = 'Material' . "Column lable WA_FCAT-KEY = 'X' . "is a key field APPEND WA_FCAT TO IT_FCAT . "append to fcat CLEAR WA_FCAT . WA_FCAT-COL_POS = '2' . WA_FCAT-FIELDNAME = 'MBRSH' . WA_FCAT-TABNAME = 'IT_MARA' . WA_FCAT-SELTEXT_M = 'Industry Sec' . APPEND WA_FCAT TO IT_FCAT . CLEAR WA_FCAT . WA_FCAT-COL_POS = '3' . WA_FCAT-FIELDNAME = 'MTART' . WA_FCAT-TABNAME = 'IT_MARA' . WA_FCAT-SELTEXT_M = 'Material Type' . APPEND WA_FCAT TO IT_FCAT . CLEAR WA_FCAT . WA_FCAT-COL_POS = '4' . WA_FCAT-FIELDNAME = 'MEINS' . WA_FCAT-TABNAME = 'IT_MARA' . WA_FCAT-SELTEXT_M = 'Base.Unit' . WA_FCAT-REF_TABNAME = 'MARA' . APPEND WA_FCAT TO IT_FCAT . CLEAR WA_FCAT . **display ALV DATA: ALV_PRINT TYPE SLIS_PRINT_ALV. ALV_PRINT-NO_PRINT_LISTINFOS = 'X'. "avoid process list on SPOOL CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING I_CALLBACK_PROGRAM = SY-REPID I_CALLBACK_PF_STATUS_SET = 'FORM_MENU' "GU status form I_CALLBACK_USER_COMMAND = 'USER_COMMAND' IT_FIELDCAT = IT_FCAT "PASS FIELD CATALOG TO ALV IS_PRINT = ALV_PRINT "to set spool options TABLES T_OUTTAB = IT_MARA. FORM FORM_MENU USING RT_EXTAB TYPE SLIS_T_EXTAB. *SET PF STATUS to ALV SET PF-STATUS 'MENU'. "GUI status...double click to create ENDFORM. " SET_PF_STATUS **for to handle user command FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM RS_SELFIELD TYPE SLIS_SELFIELD. CASE R_UCOMM. WHEN 'EPDF'. "Function code for export which we created in MENU **submit the same program in background and store * ********* Derive job counter LV_JOBNAME = 'ZALV2PDF'. "Background job name CALL FUNCTION 'JOB_OPEN' "open a job EXPORTING JOBNAME = LV_JOBNAME IMPORTING JOBCOUNT = LV_JOBCNT EXCEPTIONS CANT_CREATE_JOB = 1 INVALID_JOB_DATA = 2 JOBNAME_MISSING = 3 OTHERS = 4. IF SY-SUBRC EQ 0. SUBMIT (SY-REPID) "submit the same program WITH P_MTART = P_MTART TO SAP-SPOOL DESTINATION LV_RQDEST LINE-SIZE LV_LINSZ IMMEDIATELY 'X' KEEP IN SPOOL 'X' USER SY-UNAME VIA JOB LV_JOBNAME NUMBER LV_JOBCNT WITHOUT SPOOL DYNPRO WITH SELECTION-TABLE LT_PARAM AND RETURN. CALL FUNCTION 'JOB_CLOSE' "job close EXPORTING JOBCOUNT = LV_JOBCNT JOBNAME = LV_JOBNAME STRTIMMED = 'X' EXCEPTIONS CANT_START_IMMEDIATE = 1 INVALID_STARTDATE = 2 JOBNAME_MISSING = 3 JOB_CLOSE_FAILED = 4 JOB_NOSTEPS = 5 JOB_NOTEX = 6 LOCK_FAILED = 7 INVALID_TARGET = 8 OTHERS = 9. IF SY-SUBRC <> 0. RAISE JOB_CANNOT_BE_CLOSED. " Raise exception ENDIF. ELSE. RAISE JOB_CANNOT_BE_SUBMITTED. ENDIF. *********** confirm job status if finished DO 120 TIMES. CALL FUNCTION 'BDL_READ_JOB_STATUS' "get job status EXPORTING JOBNAME = LV_JOBNAME JOBNUMBER = LV_JOBCNT IMPORTING JOBSTATUS = LV_JOB EXCEPTIONS JOB_NOT_FOUND = 1 OTHERS = 2. IF LV_JOB NE 'F'. "job finished WAIT UP TO 1 SECONDS. CONTINUE. ELSE. EXIT. ENDIF. ENDDO. **calculate report name in Spool table IF LV_JOB EQ 'F'. "job finished LV_LEN = STRLEN( SY-REPID ) . IF LV_LEN >= 9 . CONCATENATE SY-REPID+0(9) SY-UNAME+0(3) INTO LV_RQ2NAME . ELSE. LV_LEN = 9 - LV_LEN. DO LV_LEN TIMES . CONCATENATE LV_TEMP '_' INTO LV_TEMP . ENDDO. CONCATENATE SY-REPID LV_TEMP SY-UNAME INTO LV_RQ2NAME . ENDIF. *Get spool request from SPOOL table TSP01 REFRESH LT_TSP01. SELECT RQIDENT RQ2NAME RQCRETIME FROM TSP01 INTO TABLE LT_TSP01 WHERE RQ2NAME = LV_RQ2NAME AND RQOWNER = SY-UNAME . *sort table to find latest spool no SORT LT_TSP01 BY RQCRETIME DESCENDING . *read table to find latest spool no CLEAR LS_TSP01. READ TABLE LT_TSP01 INTO LS_TSP01 INDEX 1. *get user desktop CALL METHOD CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY CHANGING DESKTOP_DIRECTORY = LV_STRING EXCEPTIONS CNTL_ERROR = 1 ERROR_NO_GUI = 2 NOT_SUPPORTED_BY_GUI = 3 OTHERS = 4. IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. CALL METHOD CL_GUI_CFW=>UPDATE_VIEW. CONCATENATE LV_STRING SY-UZEIT INTO LV_STRING. REFRESH LT_PARAM. *Prepare selection table for PDF download LS_PARAM-SELNAME = 'SPOOLNO'. LS_PARAM-SIGN = 'I'. LS_PARAM-OPTION = 'EQ'. LS_PARAM-LOW = LS_TSP01-RQIDENT. LS_PARAM-HIGH = ''. APPEND LS_PARAM TO LT_PARAM. LS_PARAM-SELNAME = 'P_FILE'. LS_PARAM-SIGN = 'I'. LS_PARAM-OPTION = 'EQ'. LS_PARAM-LOW = LV_STRING. LS_PARAM-HIGH = ''. APPEND LS_PARAM TO LT_PARAM. * Submit to PDF converted and download SUBMIT RSTXPDFT4 WITH SELECTION-TABLE LT_PARAM AND RETURN. IF SY-SUBRC <> 0. MESSAGE 'PDF convert not possible' TYPE 'I'. ENDIF. ELSE. MESSAGE 'PDF Can not be downloaded, you have selected huge data. Reduce data and try again' TYPE 'I'. ENDIF. ENDCASE. ENDFORM. "user command
A PDF will be downloaded in the below format.