Download ABAP spools and SAPscript as PDF files

Learn how to download ABAP spools and SAPscript as PDF files. This user-submitted tip provides sample code you can use to download a spool as a PDF.

This Content Component encountered an error

Looking to download ABAP spools and SAPscript as PDF files? You can, using SAP version 4.5.
 

  Code: ----------------------------------------------------------------------------------------------------------------------------------------------
Textpool :
----------------------------------------------------------------------------------------------------------------------------------------------
I001     CONVERT_OTFSPOOLJOB_2_PDF function successful                                                                                                        
I002     Not an OTF or ABAP spool request                                                                                                                     
I003     Request does not exist                                                                                                                               I004     No authorization to read spool request                                                                                                               
I005     Error in CONVERT_OTFSPOOLJOB_2_PDF function                                                                                                          
I006     CONVERT_ABAPSPOOLJOB_2_PDF function successful                                                                                                       
I007     Error in CONVERT_ABAPSPOOLJOB_2_PDF function                                                                                                         
I009     CaractÈres dans                                                                                                                                      
R        export SPOOLS to local files after PDF convertion                                                                                                    
S_CREDATE        Date                                                                                                                                         
S_RQ2NAME        Spool Name                                                                                                                                   
S_RQDEST         Destination (printer)                                                                        
S_RQIDENT        Spool Number                                                                                 
S_RQOWNER        Author                                                                                       
----------------------------------------------------------------------------------------------------------------------------------------------
Abap :
----------------------------------------------------------------------------------------------------------------------------------------------
*----------------------------------------------------------------------
* The program select spools
*          and export then under PDF format.
REPORT YYJPL14                        "NO STANDARD PAGE HEADING
                LINE-SIZE 170
                LINE-COUNT 58
                MESSAGE-ID Y_LOCAL_FR22.
*=-------------------------------------------------------------------=*
TABLES: TSP01, RSPOTYPE.
*----------------------------------------------------------------------*
DATA: DEFDIR(256) VALUE 'C:SAP'
     ,_TSP01 LIKE TSP01 OCCURS 0 WITH HEADER LINE,
      AUTHORITY LIKE  RSTSTYPE-AUTHCHECKD,
      CLIENT LIKE  RSTSTYPE-CLIENT,
      NAME LIKE  RSTSTYPE-NAME,
      PART LIKE  TST01-DPART,
      CHARCO LIKE  RSTSTYPE-CHARCO,
      CREATER LIKE  RSTSTYPE-CREATER,
      CREDATE LIKE  RSTSTYPE-CREDATE,
      DELDATE LIKE  RSTSTYPE-DELDATE,
      MAX_CREDATE LIKE  RSTSTYPE-CREDATE,
      MAX_DELDATE LIKE  RSTSTYPE-DELDATE,
      NON_UNIQ LIKE  RSTSTYPE-SEL_OK,
      NOOF_PARTS LIKE  TST01-DNOPARTS,
      RECTYP LIKE  RSTSTYPE-RECTYP,
      SIZE LIKE  TST01-DSIZE,
      STOTYP LIKE  TST01-DSTOTYP,
      TYPE LIKE  RSTSTYPE-TYPE,
      OBJTYPE LIKE  RSTSTYPE-TYPE,
      NUMBYTES TYPE I,
      PDFSPOOLID LIKE TSP01-RQIDENT,
      jobname like tbtcjob-jobname,
      JOBCOUNT LIKE TBTCJOB-JOBCOUNT,
      PDF LIKE TLINE OCCURS 0 WITH HEADER LINE,
      P_FILE LIKE RLGRAP-FILENAME VALUE 'C:tempfile.pdf'.
RANGES: S_RQFIN FOR TSP01-RQFINAL.
RANGES: _CRETIME FOR TSP01-RQCRETIME.
*----------------------------------------------------------------------*
SELECT-OPTIONS:
       _RQIDENT FOR TSP01-RQIDENT,
       _RQOWNER FOR TSP01-RQOWNER,
       _CREDATE FOR RSPOTYPE-CREDATE,
                                       "_cretime FOR tsp01-rqcretime,
       _RQ2NAME FOR TSP01-RQ2NAME,
       _RQDEST  FOR TSP01-RQDEST .
PARAMETERS:
       DIR LIKE RLGRAP-FILENAME DEFAULT DEFDIR.
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR DIR.
*----------------------------------------------------------------------*
  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            DEF_FILENAME     = '*.*'
            DEF_PATH         = DEFDIR
            MASK             = ',All files,*.*.'
            MODE             = 'O'
            TITLE            = 'Get filename for directory'
       IMPORTING
            FILENAME         = DIR
       EXCEPTIONS
            INV_WINSYS       = 01
            NO_BATCH         = 02
            SELECTION_CANCEL = 03
            SELECTION_ERROR  = 04.
  IF SY-SUBRC <> 0.
    SY-SUBRC = SY-SUBRC + 0.
  ENDIF.
  DEFDIR = DIR.
*=-------------------------------------------------------------------=*
START-OF-SELECTION.
*=-------------------------------------------------------------------=*
*here inspiration comes from RSPOSP01 (transaction SP01)
  REFRESH S_RQFIN.            " select all C & . required for index!!!
  S_RQFIN-LOW = 'C'.
  S_RQFIN-SIGN = 'I'.
  S_RQFIN-OPTION = 'EQ'.
  APPEND S_RQFIN.
  S_RQFIN-LOW = '.'.
  APPEND S_RQFIN.

  CLEAR _CRETIME.
  REFRESH _CRETIME.
  LOOP AT _CREDATE.
    _CRETIME-SIGN = _CREDATE-SIGN.
    _CRETIME-OPTION = _CREDATE-OPTION.
    IF _CREDATE-LOW IS INITIAL.
      CLEAR _CRETIME-LOW.
    ELSE.
      _CRETIME-LOW(8) = _CREDATE-LOW.
      _CRETIME-LOW+8(8) = '00000000'.
    ENDIF.
    IF _CREDATE-HIGH IS INITIAL.
      CLEAR _CRETIME-HIGH.
    ELSE.
      _CRETIME-HIGH(8) = _CREDATE-HIGH.
      _CRETIME-HIGH+8(8) = '23595900'.
    ENDIF.
    APPEND _CRETIME.
  ENDLOOP.
*find the spool list
  SELECT * FROM TSP01
         INTO CORRESPONDING FIELDS OF TABLE _TSP01
         WHERE RQFINAL IN S_RQFIN AND
               RQIDENT IN _RQIDENT AND
               RQOWNER IN _RQOWNER AND
               RQCRETIME IN _CRETIME AND
               RQ2NAME IN _RQ2NAME AND
               RQDEST  IN _RQDEST .
  LOOP AT _TSP01.
    MOVE-CORRESPONDING _TSP01 TO TSP01.
*authority to work with spool
    CALL FUNCTION 'RSPO_CHECK_JOB_PERMISSION'
         EXPORTING
              ACCESS        = 'BASE'
              SPOOLREQ      = TSP01
         EXCEPTIONS
              NO_PERMISSION = 1
              OTHERS        = 2.
*check authority
    CHECK SY-SUBRC = 0.
*here inspiration comes from RSTXPDFT4
*retrieve spool attributes
    client = tsp01-rqclient.
    name   = tsp01-rqo1name.
    CALL FUNCTION 'RSTS_GET_ATTRIBUTES'
         EXPORTING
              AUTHORITY     = 'SP01'
              CLIENT        = CLIENT
              NAME          = NAME
              PART          = 1
         IMPORTING
              CHARCO        = CHARCO
              CREATER       = CREATER
              CREDATE       = CREDATE
              DELDATE       = DELDATE
              MAX_CREDATE   = MAX_CREDATE
              MAX_DELDATE   = MAX_DELDATE
              NON_UNIQ      = NON_UNIQ
              NOOF_PARTS    = NOOF_PARTS
              RECTYP        = RECTYP
              SIZE          = SIZE
              STOTYP        = STOTYP
              TYPE          = TYPE
              OBJTYPE       = OBJTYPE
         EXCEPTIONS
              FB_ERROR      = 1
              FB_RSTS_OTHER = 2
              NO_OBJECT     = 3
              NO_PERMISSION = 4
              OTHERS        = 5.
    IF SY-SUBRC <> 0.
      MESSAGE E020 WITH SY-SUBRC CLIENT NAME.
      CONTINUE.
    ENDIF.
*spool À traiter
    WRITE:/
              TSP01-RQIDENT  ,
              TSP01-RQ0NAME  ,
              TSP01-RQ1NAME  ,
              TSP01-RQ2NAME  ,
              TSP01-RQOWNER  ,
          (19) TSP01-RQCRETIME USING EDIT MASK '____/__/__ __:__:__',
              TSP01-RQDEST   ,
              TSP01-RQPAPER.
    "              client        ,
    "              name          ,
    "              part          ,
    "              charco        ,
    "              creater       ,
    "              credate       ,
    "              deldate       ,
    "              max_credate   ,
    "              max_deldate   ,
    "              non_uniq      ,
    "              noof_parts    ,
    "              rectyp        ,
    "              size          ,
    "              stotyp        ,
    "              type          ,
    "              objtype       .
    if objtype(3) = 'OTF'.
      CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
          EXPORTING
            SRC_SPOOLID                    = TSP01-RQIDENT
            NO_DIALOG                      = ' '
*       DST_DEVICE                     =
*       PDF_DESTINATION                =
          IMPORTING
            PDF_BYTECOUNT                  = numbytes
            PDF_SPOOLID                    = pdfspoolid
*       OTF_PAGECOUNT                  =
            BTC_JOBNAME                    = jobname
            BTC_JOBCOUNT                   = jobcount
          TABLES
            PDF                            = pdf
          EXCEPTIONS
            ERR_NO_OTF_SPOOLJOB            = 1
            ERR_NO_SPOOLJOB                = 2
            ERR_NO_PERMISSION              = 3
            ERR_CONV_NOT_POSSIBLE          = 4
            ERR_BAD_DSTDEVICE              = 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.
      case sy-subrc.
        when 0.
         WRITE: / 'Funktion CONVERT_OTFSPOOLJOB_2_PDF erfolgreich'(001)
                      COLOR COL_POSITIVE.
        when 1.
          WRITE: / 'Kein OTF- und kein ABAP-Spoolauftrag'(002)
                COLOR COL_negative.
          exit.
        when 2.
          WRITE: / 'Spoolauftrag existiert nicht'(003)
                COLOR COL_negative.
          exit.
        when 3.
          WRITE: / 'Keine Berechtigung zum Lesen Spoolauftrag'(004)
                COLOR COL_negative.
          exit.
        when others.
          WRITE: / 'Fehler bei Funktion CONVERT_OTFSPOOLJOB_2_PDF'(005)
                    COLOR COL_negative.
          exit.
      endcase.
    else.
      CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
          EXPORTING
            SRC_SPOOLID                    = TSP01-RQIDENT
            NO_DIALOG                      = ' '
*       DST_DEVICE                     =
*       PDF_DESTINATION                =
          IMPORTING
            PDF_BYTECOUNT                  = numbytes
            PDF_SPOOLID                    = pdfspoolid
*       LIST_PAGECOUNT                 =
            BTC_JOBNAME                    = jobname
            BTC_JOBCOUNT                   = jobcount
          TABLES
            PDF                            = 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.
      case sy-subrc.
        when 0.
        WRITE: / 'Funktion CONVERT_ABAPSPOOLJOB_2_PDF erfolgreich'(006)
                            COLOR COL_POSITIVE.
        when 1.
          WRITE: / 'Kein OTF- und kein ABAP-Spoolauftrag'(002)
                COLOR COL_negative.
          exit.
        when 2.
          WRITE: / 'Spoolauftrag existiert nicht'(003)
                COLOR COL_negative.
          exit.
        when 3.
          WRITE: / 'Keine Berechtigung zum Lesen Spoolauftrag'(004)
                COLOR COL_negative.
          exit.
        when others.
         WRITE: / 'Fehler bei Funktion CONVERT_ABAPSPOOLJOB_2_PDF'(007)
                          COLOR COL_negative.
          exit.
      endcase.
    endif.
* download PDF file
    TRANSLATE TSP01-RQ2NAME USING '/  : * < > | ? '' " '.
    CONDENSE TSP01-RQ2NAME NO-GAPS.
    CONCATENATE DIR '' TSP01-RQ2NAME '-' TSP01-RQCRETIME '.pdf'
    INTO P_FILE.

    CALL FUNCTION 'WS_DOWNLOAD'"or GUI_DOWNLOAD
         EXPORTING
              BIN_FILESIZE = NUMBYTES
              FILENAME     = P_FILE
              FILETYPE     = 'BIN'
         IMPORTING
*             act_filename = p_file
              FILELENGTH   = NUMBYTES
         TABLES
              DATA_TAB     = PDF.
    WRITE: / NUMBYTES, 'Bytes heruntergeladen in Datei'(009),
             (100) P_FILE.
    ULINE.
  ENDLOOP.
----------------------------------------------------------------------------------------------------------------------------------------------
This was first published in September 2005

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close