Z-Programm Konvertiere OTF/Spooljob nach PDF und Speicher auf Applikationsserver und/oder Frontend

Aus SAP-Wiki
Wechseln zu:Navigation, Suche

Dieses Programm konvertiert ein OTF-Spoolauftrag in ein PDF und speichert die Datei auf dem Applikationserver mittels des Funktionsbausteins 'GUI_DOWNLOAD'.

Dieses Programm verbessert das Standardprogramm RSTXPDFT4, was nicht im Hintergrund/als Job ausgeführt werden kann.

Es werden die Schritte durchlaufen

  1. Es werden die Spool-Attribute für einen Selektionsparameter Spoolauftrag ausgelesen (Funktionsbaustein 'RSPO_GET_ATTRIBUTES_SPOOLJOB').
  2. Der OTF-Inhalt vom Spoolauftrag wird gelesen und mit dem Funktionsbaustein 'CONVERT_OTFSPOOLJOB_2_PDF' (Spooltyp OTF oder SMART) bzw. 'CONVERT_ABAPSPOOLJOB_2_PDF' (Spooltyp LIST) in eine Variable vom Typ XSTRING gefüllt, was den PDF repräsentiert.
  3. Speichern der Datei vom Typ XSTRING auf dem Applikationserver mit den Befehlen OPEN DATASET, TRANSFER und CLOSE DATASET.
  4. Speichern der Datei auf dem Frontend (nur im Vordergrund möglich) mit Funktionsbaustein 'GUI_DOWNLOAD' (Type SOLIX_TAB).

Programmcoding

REPORT zsspool2file LINE-SIZE 132.

PARAMETERS:
path    TYPE rlgrap-filename DEFAULT '/interfaces/outbound/sonderlauf/PDF1.PDF' OBLIGATORY,
rqident TYPE tsp01-rqident OBLIGATORY,
app     TYPE c AS CHECKBOX.


DATA pdf_size      TYPE so_obj_len.
DATA pdf_xstring   TYPE xstring.
DATA gv_xstring    TYPE xstring.

DATA :   v_text    TYPE localfile.

START-OF-SELECTION.

  PERFORM create_pdf.

  PERFORM upload_app_server.

  PERFORM download_app_server.

  PERFORM download.
*&---------------------------------------------------------------------*
*&      Form  create_pdf
*&---------------------------------------------------------------------*
* Create PDF Content
* 1) get attributes of spool request
* 2) convert spool request to PDF dependent on document type
*----------------------------------------------------------------------*
FORM create_pdf.

  DATA rq       TYPE tsp01.
  DATA bin_size TYPE i.
  DATA dummy    TYPE TABLE OF rspoattr.

*   ------------ get attributes of spool request ---------------------
  CALL FUNCTION 'RSPO_GET_ATTRIBUTES_SPOOLJOB'
    EXPORTING
      rqident     = rqident
    IMPORTING
      rq          = rq
    TABLES
      attributes  = dummy
    EXCEPTIONS
      no_such_job = 1
      OTHERS      = 2.
  IF sy-subrc <> 0.
    MESSAGE e126(po) WITH rqident.
  ENDIF.

*   --- convert spool request into PDF, dependent on document type ---
  IF rq-rqdoctype = 'OTF' OR rq-rqdoctype = 'SMART'.
    CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
      EXPORTING
        src_spoolid              = rqident
        no_dialog                = 'X'
        pdf_destination          = 'X'
        no_background            = 'X'
      IMPORTING
        pdf_bytecount            = bin_size
        bin_file                 = pdf_xstring
      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
        OTHERS                   = 12.
    IF sy-subrc <> 0.
      MESSAGE e712(po) WITH sy-subrc 'CONVERT_OTFSPOOLJOB_2_PDF'.
    ENDIF.
  ELSEIF rq-rqdoctype = 'LIST'.
    CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
      EXPORTING
        src_spoolid              = rqident
        no_dialog                = 'X'
        pdf_destination          = 'X'
        no_background            = 'X'
      IMPORTING
        pdf_bytecount            = bin_size
        bin_file                 = pdf_xstring
      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
        OTHERS                   = 12.
    IF sy-subrc <> 0.
      MESSAGE e712(po) WITH sy-subrc 'CONVERT_ABAPSPOOLJOB_2_PDF'.
    ENDIF.
  ELSE.
    MESSAGE e789(po) WITH rq-rqdoctype.
  ENDIF.
  pdf_size = bin_size.
ENDFORM.                    "create_pdf
*&---------------------------------------------------------------------*
*&      Form  UPLOAD_APP_SERVER
*&---------------------------------------------------------------------*
*       Save PDF (Type XFILE) on application server
*----------------------------------------------------------------------*
FORM upload_app_server.

*  CONCATENATE sy-uname+0(8) sy-datum sy-uzeit INTO
*              v_text.

  v_text = path.
  OPEN DATASET v_text FOR OUTPUT IN BINARY MODE.

  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  TRANSFER pdf_xstring TO v_text.

  CLOSE DATASET v_text.

ENDFORM.                    " UPLOAD_APP_SERVER
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_APP_SERVER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM download_app_server.
 
  OPEN DATASET v_text FOR INPUT IN BINARY MODE.
 
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
 
  READ DATASET v_text INTO gv_xstring.
 
  CLOSE DATASET v_text.
 
ENDFORM.                    " DOWNLOAD_APP_SERVER
*&---------------------------------------------------------------------*
*&      Form  download
*&---------------------------------------------------------------------*
FORM download.
 
  DATA : lv_filename    TYPE string,
         lt_pdf_content TYPE solix_tab.

  lv_filename = path.

* get the PDF binary table
  lt_pdf_content = cl_document_bcs=>xstring_to_solix( gv_xstring ).

  CALL FUNCTION 'GUI_DOWNLOAD'
   EXPORTING
*   BIN_FILESIZE                   =
    filename                       = lv_filename
    filetype                       = 'BIN'
*  IMPORTING
*   FILELENGTH                     =
    TABLES
      data_tab                     = lt_pdf_content
  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 sy-subrc <> 0.
*   Implement suitable error handling here
  ENDIF.

ENDFORM.                    "download