Skip to Content

Convert Classical/ALV/Interactive Reports to direct PDF File

Hello ,

While Developing SAP reports in ABAP we faces number of similar requirements like Save as Excel, Mail , Save as PDF Output Etc.

For saving as PDF we can do few manually steps as

1. Generate spool request of report,

2. Execute spool in Report RSTXPDFT5, which may not suitable for users.

We can build Logic also in Report to avoid Manual steps for users, but that will be limited to that report only.

I have written small code, Generic Executable Report as ZRPT2PDF.

Using it one can generate PDF for any Report , Custom or Standard .

It works with single T-code parameter in selection screen ,

and support ALV/Classical/Interactive Formats.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

ZRPT2PDF is single Report , No need to Change any of your existing Source codes.

I found it very handy for PDF Generation.

++++++++++++++++++++++++++

I have upload Source code for ZRPT2PDF Version 1,You can use it.

Source Code:-

**”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*

* Report          : ZST_REPORT_TO_PDF

* Request No      : Local

* Developed by    : Stallone Christian

* Requested on    : 04.05.2012

* Description     : Generate PDF from Report

**”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*”*

*Extra Info:

************************************************************************

* Generate PDF File for any Report using Tcode

* After Executing Report, Go back and Save PDF File

************************************************************************

REPORT  ZST_REPORT_TO_PDF.

DATA :

      WA_TSTC              TYPE TSTC,

      WA_TSTCP             TYPE TSTCP,

      SM30_PARA            TYPE TSTCPPARAM,

      IT_PARAM             TYPE STANDARD TABLE OF STRING,

      WA_PARAM             TYPE STRING,

      GV_REPORT            TYPE SYREPID,

      GV_PARAMS            TYPE PRI_PARAMS,

      GV_VALID             TYPE STRING,

      GV_RQIDENT           TYPE TSP01RQIDENT,

      GT_RSPARAMS          TYPE STANDARD TABLE OF RSPARAMS,

      WA_TSP01             TYPE TSP01,

      GV_SPOOL             TYPE TSP01RQIDENT,

      GV_RQ2NAME           TYPE TSP01RQ2NAME,

      PDF_BYTECOUNT         TYPE  I,

      PDF_SPOOLID           LIKE  TSP01RQIDENT,

      LIST_PAGECOUNT       TYPE I,

      BTC_JOBNAME           TYPE  TBTCJOBJOBNAME,

      BTC_JOBCOUNT         TYPE  TBTCJOBJOBCOUNT,

      BIN_FILE             TYPE XSTRING,

      GT_PDF               TYPE STANDARD TABLE OF TLINE,

      GV_BIN_FILESIZE      TYPE I, ” Binary File Size

      GV_FILE_NAME         TYPE STRING,

      GV_FILE_PATH         TYPE STRING,

      GV_FULL_PATH         TYPE STRING.

” Selection Screen

SELECTIONSCREEN: BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT000.

SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN COMMENT (23) TEXT001 FOR FIELD P_TCODE.

PARAMETERS P_TCODE TYPE TSTCTCODE OBLIGATORY.

SELECTION-SCREEN END OF LINE.

“Text

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN COMMENT (70) TEXT002.

SELECTION-SCREEN END OF LINE.

SELECTIONSCREEN: END OF BLOCK B1.

LOADOFPROGRAM.

  TEXT000 ‘Selection Criteria’.

  TEXT001 ‘@8T@ Transaction Code’.

  TEXT002 ‘*Enter Tcode->Select Print Option->Execute report->Save PDF File’.

STARTOFSELECTION.

  “Fetch Tcode and Report info

  PERFORM FETCH_TCODE_REPORT.

  “Call Transaction

  PERFORM CALL_TRANSACTION.

  “Get Spool and Generate PDF

  PERFORM GET_SPOOL_TO_PDF.

*&———————————————————————*

*&      Form  FETCH_TCODE_REPORT

*&———————————————————————*

*       text

*———————————————————————-*

*  –>  p1        text

*  <–  p2        text

*———————————————————————-*

FORM FETCH_TCODE_REPORT .

  DATA :

         FLG_TCODE TYPE CHAR1,

         CHR1      TYPE CHAR40,

         CHR2      TYPE CHAR40.

  SELECT SINGLE * FROM TSTC

    INTO WA_TSTC WHERE TCODE = P_TCODE

                  AND PGMNA NE SPACE

                  AND DYPNO  = 1000. “only Standard Report

  IF SYSUBRC 0.

    FLG_TCODE = ‘X’.

    GV_REPORT = WA_TSTCPGMNA.

  ELSE.

    SELECT SINGLE * FROM

       TSTCP INTO WA_TSTCP

       WHERE TCODE = P_TCODE

         AND PARAM NE SPACE.

    SPLIT WA_TSTCPPARAM AT ‘;’ INTO TABLE IT_PARAM.

    LOOP AT IT_PARAM INTO WA_PARAM.

      IF WA_PARAM CS ‘-REPORT=’.

        FLG_TCODE = ‘X’.

        SPLIT WA_PARAM AT ‘-REPORT=’ INTO CHR1 CHR2.

        GV_REPORT = CHR2.

        EXIT.

      ENDIF.

    ENDLOOP.

  ENDIF.

  IF FLG_TCODE IS INITIAL.

    MESSAGE ‘In Valid Tcode’ TYPE ‘I’ DISPLAY LIKE ‘E’.

    LEAVE TO TRANSACTION SYTCODE.

  ENDIF.

  CALL FUNCTION ‘AUTH_CHECK_TCODE’

    EXPORTING

      TCODE                          = P_TCODE

    EXCEPTIONS

      PARAMETER_ERROR                = 1

      TRANSACTION_NOT_FOUND          = 2

      TRANSACTION_LOCKED             = 3

      TRANSACTION_IS_MENU            = 4

      MENU_VIA_PARAMETER_TRANSACTION = 5

      NOT_AUTHORIZED                 = 6

      OTHERS                         = 7.

  IF SYSUBRC <> 0.

    MESSAGE ‘No Authorization for Tcode’ TYPE ‘I’ DISPLAY LIKE ‘E’.

    LEAVE TO TRANSACTION SYTCODE.

  ENDIF.

ENDFORM.                    ” FETCH_TCODE_REPORT

*&———————————————————————*

*&      Form  CALL_TRANSACTION

*&———————————————————————*

*       text

*———————————————————————-*

*  –>  p1        text

*  <–  p2        text

*———————————————————————-*

FORM CALL_TRANSACTION .

  CALL FUNCTION ‘GET_PRINT_PARAMETERS’

    IMPORTING

      OUT_PARAMETERS = GV_PARAMS

      VALID          = GV_VALID.

  IF SYSUBRC <> 0.

  ENDIF.

  “Spool Mode, Default will be Spool Only

  CLEAR GV_PARAMSPRIMM.

  “Spool Request Name, Pass Report name , Only 1st 12 Chars

  GV_RQ2NAME      = GV_REPORT.

  GV_PARAMSPLIST = GV_RQ2NAME.

  SUBMIT (GV_REPORT) VIA SELECTION-SCREEN

                    TO SAPSPOOL

                    SPOOL PARAMETERS GV_PARAMS

                    WITHOUT SPOOL DYNPRO

                    AND RETURN.

  IF SYSUBRC NE 0.

    MESSAGE ‘Unable to Execute Report’ TYPE ‘I’ DISPLAY LIKE ‘E’.

    LEAVE TO TRANSACTION SYTCODE.

  ENDIF.

  WAIT UP TO 2 SECONDS.

ENDFORM.                    ” CALL_TRANSACTION

*&———————————————————————*

*&      Form  GET_SPOOL_TO_PDF

*&———————————————————————*

*       text

*———————————————————————-*

*  –>  p1        text

*  <–  p2        text

*———————————————————————-*

FORM GET_SPOOL_TO_PDF .

  DATA :

      LV_RQCRETIME  TYPE TSP01RQCRETIME,

      LV_RETURNCODE TYPE I.

  CONCATENATE SYDATUM ‘%’ INTO LV_RQCRETIME.

  SELECT MAX( RQIDENT ) INTO GV_SPOOL FROM TSP01  WHERE RQCLIENT = SYMANDT

                                                  AND   RQ2NAME  = GV_RQ2NAME

                                                  AND   RQOWNER  = SYUNAME

                                                  AND   RQCRETIME LIKE LV_RQCRETIME.

  CHECK SYSUBRC = 0.

  SELECT SINGLE * FROM TSP01

    INTO WA_TSP01 WHERE RQIDENT = GV_SPOOL.

  IF WA_TSP01 IS INITIAL.

    MESSAGE ‘No Valid Spool Request Found’ TYPE ‘I’ DISPLAY LIKE ‘E’.

    LEAVE TO TRANSACTION SYTCODE.

  ENDIF.

  CALL FUNCTION ‘CONVERT_ABAPSPOOLJOB_2_PDF’

    EXPORTING

      SRC_SPOOLID              = GV_SPOOL

      NO_DIALOG                = ABAP_FALSE

    IMPORTING

      PDF_BYTECOUNT            = PDF_BYTECOUNT

      PDF_SPOOLID              = PDF_SPOOLID

      BTC_JOBNAME              = BTC_JOBNAME

      BTC_JOBCOUNT             = BTC_JOBCOUNT

    TABLES

      PDF                      = GT_PDF

    EXCEPTIONS

      ERR_NO_ABAP_SPOOLJOB     = 1

      ERR_NO_SPOOLJOB          = 2

      ERR_NO_PERMISSION        = 3

      ERR_CONV_NOT_POSSIBLE    = 4

      ERR_BAD_DESTDEVICE       = 5

      USER_CANCELLED           = 6

      ERR_SPOOLERROR           = 7

      ERR_TEMSEERROR           = 8

      ERR_BTCJOB_OPEN_FAILED   = 9

      ERR_BTCJOB_SUBMIT_FAILED = 10

      ERR_BTCJOB_CLOSE_FAILED  = 11.

  CHECK GT_PDF IS NOT INITIAL.

  GV_FILE_NAME = P_TCODE.

* To display File SAVE dialog window

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG

    EXPORTING

      WINDOW_TITLE         = ‘Save Output as PDF’ “LV_TITLE

      DEFAULT_EXTENSION    = ‘PDF’

      FILE_FILTER          = ‘(*.pdf)|*.pdf|’

      DEFAULT_FILE_NAME    = GV_FILE_NAME

    CHANGING

      FILENAME             = GV_FILE_NAME

      PATH                 = GV_FILE_PATH

      FULLPATH             = GV_FULL_PATH

    EXCEPTIONS

      CNTL_ERROR           = 1

      ERROR_NO_GUI         = 2

      NOT_SUPPORTED_BY_GUI = 3

      OTHERS               = 4.

  IF SYSUBRC <> 0.

    MESSAGE ID SYMSGID TYPE SYMSGTY NUMBER SYMSGNO

    WITH SYMSGV1 SYMSGV2 SYMSGV3 SYMSGV4.

  ENDIF.

* Use the FM GUI_DOWNLOAD to download the generated PDF file onto the

* presentation server

  CHECK GV_FULL_PATH IS NOT INITIAL.

  CALL FUNCTION ‘GUI_DOWNLOAD’

    EXPORTING

      BIN_FILESIZE            = GV_BIN_FILESIZE

      FILENAME                = GV_FULL_PATH

      FILETYPE                = ‘BIN’

    TABLES

      DATA_TAB                = GT_PDF

    EXCEPTIONS

      FILE_WRITE_ERROR        = 1

      NO_BATCH                = 2

      GUI_REFUSE_FILETRANSFER = 3

      INVALID_TYPE            = 4

      NO_AUTHORITY            = 5

      UNKNOWN_ERROR           = 6

      HEADER_NOT_ALLOWED      = 7

      SEPARATOR_NOT_ALLOWED   = 8

      FILESIZE_NOT_ALLOWED    = 9

      HEADER_TOO_LONG         = 10

      DP_ERROR_CREATE         = 11

      DP_ERROR_SEND           = 12

      DP_ERROR_WRITE          = 13

      UNKNOWN_DP_ERROR        = 14

      ACCESS_DENIED           = 15

      DP_OUT_OF_MEMORY        = 16

      DISK_FULL               = 17

      DP_TIMEOUT              = 18

      FILE_NOT_FOUND          = 19

      DATAPROVIDER_EXCEPTION  = 20

      CONTROL_FLUSH_ERROR     = 21

      OTHERS                  = 22.

  IF SYSUBRC = 0.

    MESSAGE ‘File Generated Successfully’ TYPE ‘S’.

  ELSE.

    MESSAGE ID SYMSGID TYPE SYMSGTY NUMBER SYMSGNO

    WITH SYMSGV1 SYMSGV2 SYMSGV3 SYMSGV4.

  ENDIF.

ENDFORM.                    ” GET_SPOOL_TO_PDF 

Be the first to leave a comment
You must be Logged on to comment or reply to a post.