[ABAP] Spoolauftrag erzeugen und Text in den SAP-Spool ausgeben

DATA: lv_spoolid TYPE tsp01-rqident.
DATA: lv_handle TYPE sy-tabix.

* Spool: Öffnen eines Spoolauftrags zum Schreiben
CALL FUNCTION 'RSPO_SR_OPEN'
  EXPORTING
    dest             = 'LOCL'        " Ausgabeziel ist lokaler Drucker (Windows)
    layout           = 'X_65_80'
    titleline        = 'Spoolausgabe'
    doctype          = 'LIST'
  IMPORTING
    handle           = lv_handle
    spoolid          = lv_spoolid
  EXCEPTIONS
    device_missing   = 1
    name_twice       = 2
    no_such_device   = 3
    operation_failed = 4
    OTHERS           = 5.

IF sy-subrc = 0.

  WRITE: 'Open spool:', lv_spoolid.

  DATA: lv_text TYPE char255.
  lv_text = 'Testtext'.

* Spool: Schreiben einer Zeile in einen offenen Spoolauftrag
  CALL FUNCTION 'RSPO_SR_WRITE'
    EXPORTING
      handle           = lv_handle
      text             = lv_text
    EXCEPTIONS
      handle_not_valid = 1
      operation_failed = 2
      OTHERS           = 3.

  IF sy-subrc = 0.
    WRITE: / 'Text written.'.
  ELSE.
    WRITE: / 'Writing failed.'.
  ENDIF.

  IF sy-subrc = 0.
* Spool: Schliessen eines Spoolrequests
    CALL FUNCTION 'RSPO_SR_CLOSE'
      EXPORTING
        handle           = lv_handle
      EXCEPTIONS
        handle_not_valid = 1
        operation_failed = 2
        OTHERS           = 3.

    IF sy-subrc = 0.
      WRITE: / 'Close.'.
    ELSE.
      WRITE: / 'Close failed.'.
    ENDIF.
  ENDIF.
ELSE.
  WRITE: / 'Open failed.'.
ENDIF.

[ABAP] Spoolauftrag auf einem Drucker ausgeben

* Spoolauftrag
PARAMETERS: p_spool TYPE tsp01-rqident DEFAULT '12345' OBLIGATORY.
* Drucker
PARAMETERS: p_dest TYPE tsp03-padest OBLIGATORY.

START-OF-SELECTION.

* Prio
* 0 - default
* 1 = sehr hoch
* 2 = hoch
* 3 = hoch
* 4 = mittel
* 5 = mittel
* 6 = mittel
* 7 = niedrig
* 8 = niedrig
* 9 = sehr niedrig
  CALL FUNCTION 'RSPO_OUTPUT_SPOOL_REQUEST'
    EXPORTING
*     copies                   = 0
      device                   = p_dest
*     division                 = '*'
*     endpage                  = 0
*     prio                     = 0
*     receiver                 = '*'
*     reqest_title             = '*'
      spool_request_id         = p_spool
*     startpage                = 0
*     telelan                  = '*'
*     telenum                  = '*'
*     posname                  =
*     acttime                  =
*    TABLES
*     attributes               =
    EXCEPTIONS
      archive_dest_invalid     = 1
      archive_dest_not_found   = 2
      archive_dest_no_right    = 3
      cannot_archive           = 4
      change_archdest_no_right = 5
      change_copies_no_right   = 6
      change_dest_no_right     = 7
      change_devtype_no_right  = 8
      change_prio_no_right     = 9
      change_telenum_no_right  = 10
      change_title_no_right    = 11
      dest_invalid             = 12
      dest_not_found           = 13
      dest_no_right            = 14
      internal_problem         = 15
      invalid_fax_attribute    = 16
      invalid_parameters       = 17
      non_owner_no_right       = 18
      no_layout                = 19
      no_spool_request         = 20
      out_again_no_right       = 21
      spooler_problem          = 22
      in_process               = 23
      OTHERS                   = 24.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    WRITE: / 'Druck fertig.'.
  ENDIF.

[ABAP] Listenausgabe in Spool umleiten und als PDF ausgeben

DATA: it_print_params TYPE pri_params.

* Spool Druckparameter und Archivparameter lesen, bestimmen, ändern
CALL FUNCTION 'GET_PRINT_PARAMETERS'
  EXPORTING
    no_dialog              = abap_true
  IMPORTING
    out_parameters         = it_print_params
  EXCEPTIONS
    archive_info_not_found = 1
    invalid_print_params   = 2
    invalid_archive_params = 3
    OTHERS                 = 4.

IF sy-subrc = 0.
* Liste -> Spool (ID)
  NEW-PAGE PRINT ON PARAMETERS it_print_params NO DIALOG.
  WRITE: / 'Hello World'.
  NEW-PAGE PRINT OFF.

  COMMIT WORK.

  DATA: lv_bin_length TYPE i.
  DATA: lv_xstring_pdf TYPE xstring.

* Spool (ID) -> PDF
  CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
    EXPORTING
      src_spoolid              = CONV tsp01-rqident( sy-spono )
      no_dialog                = abap_true
      pdf_destination          = 'X' " xstring
      get_size_from_format     = abap_true
    IMPORTING
      pdf_bytecount            = lv_bin_length
      bin_file                 = lv_xstring_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
      OTHERS                   = 12.

  IF sy-subrc = 0.
    DATA: lv_action TYPE i.
    DATA: lv_filename TYPE string.
    DATA: lv_fullpath TYPE string.
    DATA: lv_path TYPE string.

    TRY.
* SaveDialog aufrufen
        cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                      default_extension   = 'pdf'
                                                      default_file_name   = 'ListOutput'
                                                      file_filter         = |PDF (*.pdf)\|*.pdf\|{ cl_gui_frontend_services=>filetype_all }|
                                                      prompt_on_overwrite = abap_true
                                                    CHANGING
                                                      filename          = lv_filename
                                                      path              = lv_path
                                                      fullpath          = lv_fullpath
                                                      user_action       = lv_action ).

        IF lv_action EQ cl_gui_frontend_services=>action_ok.
* xstring -> solix
          DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( lv_xstring_pdf ).
* iTab (bytes) -> lokale Datei
          cl_gui_frontend_services=>gui_download( EXPORTING
                                                    filename     = lv_fullpath
                                                    filetype     = 'BIN'
                                                    bin_filesize = lv_bin_length
                                                  CHANGING
                                                    data_tab     = it_raw_data ).

* Browserfenster erzeugen
          DATA(o_html) = NEW cl_gui_html_viewer( parent = cl_gui_container=>default_screen ).

* PDF im ABAP-Fenster anzeigen
          o_html->show_url( url      = CONV skwf_url( lv_fullpath )
                            in_place = abap_true ).

* leere SAP-Toolbar ausblenden
          cl_abap_list_layout=>suppress_toolbar( ).

* cl_gui_container=>default_screen erzwingen
          WRITE: / |Datei erfolgreich unter { lv_fullpath } gespeichert.|.
        ENDIF.

      CATCH cx_root INTO DATA(e_txt).
        MESSAGE e_txt->get_text( ) TYPE 'S'.
    ENDTRY.
  ENDIF.
ELSE.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Links

[ABAP] Spoolaufträge (OTF, OTFHPL2, SMART, LIST, TEXT) in PDF wandeln

Variante 1 (cl_rspo_spoolid_to_pdf)

* Nr. des Spoolauftrags
PARAMETERS: p_spool TYPE rspoid OBLIGATORY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_spool.

  SELECT rqident, rqtitle, rqdest, rqpaper, rqdoctype
    INTO TABLE @DATA(it_spool)
    FROM tsp01
    ORDER BY rqident.

  DATA: it_return TYPE TABLE OF ddshretval.

* eigene Suuchhilfe für Auswahl der Spooljobs anzeigen
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = 'RQIDENT'     " Spalte der internen Tabelle bei value_tab
      dynpprog        = sy-repid
      dynpnr          = sy-dynnr
      dynprofield     = 'P_SPOOL'     " Name des Dynpro-Feldes für die automatische Werterückgabe
      value_org       = 'S'           " Werteübergabe: C: zellenweise, S: strukturiert
      window_title    = 'Auswahl'
    TABLES
      value_tab       = it_spool      " Übergabe-Tabelle mit Werten für die Anzeige und Auswahl
      return_tab      = it_return     " Rückgabe-Tabelle mit den ausgewählten (geklickten) Elementen
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.

  IF sy-subrc = 0.
    IF lines( it_return ) > 0.
      MESSAGE it_return[ 1 ]-fieldval TYPE 'S'.
    ENDIF.
  ENDIF.

START-OF-SELECTION.
  TRY.
      DATA: lv_xstring_pdf TYPE xstring.
      DATA: lv_size TYPE int4.
      DATA: lv_jobname TYPE btcjob.
      DATA: lv_jobcount TYPE btcjobcnt.

* SPOOL-ID -> PDF
      cl_rspo_spoolid_to_pdf=>get_spool_pdf( EXPORTING
                                               iv_rqident       = p_spool
                                               iv_no_background = abap_true
                                             IMPORTING
                                               ev_pdf           = lv_xstring_pdf
                                               ev_size          = lv_size
                                               ev_jobname       = lv_jobname
                                               ev_jobcount      = lv_jobcount
                                           ).

      WRITE: / 'Size:', lv_size.
      WRITE: / 'Jobname:', lv_jobname.
      WRITE: / 'Jobcount', lv_jobcount.

      DATA: lv_action TYPE i.
      DATA: lv_filename TYPE string.
      DATA: lv_fullpath TYPE string.
      DATA: lv_path TYPE string.

* SaveDialog
      cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                    default_file_name = 'test.pdf'
                                                    default_extension = 'pdf'
                                                    file_filter       = '(*.pdf)|*.pdf|'
                                                  CHANGING
                                                    filename          = lv_filename
                                                    path              = lv_path
                                                    fullpath          = lv_fullpath
                                                    user_action       = lv_action ).

      IF lv_action EQ cl_gui_frontend_services=>action_ok.

* xstring -> solix
        DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( lv_xstring_pdf ).

* PDF lokal speichern
        cl_gui_frontend_services=>gui_download( EXPORTING
                                                  filename     = lv_fullpath
                                                  filetype     = 'BIN'
                                                  bin_filesize = lv_size
                                                CHANGING
                                                  data_tab     = it_raw_data ).

* Browserfenster erzeugen
        DATA(o_html) = NEW cl_gui_html_viewer( parent = cl_gui_container=>default_screen ).

* PDF im ABAP-Fenster anzeigen
        o_html->show_url( url      = CONV skwf_url( lv_fullpath )
                          in_place = abap_true ).

* leere SAP-Toolbar ausblenden
        cl_abap_list_layout=>suppress_toolbar( ).

* cl_gui_container=>default_screen erzwingen
        WRITE: / |Datei erfolgreich unter { lv_fullpath } gespeichert.|.
      ENDIF.
    CATCH cx_root INTO DATA(e_txt).
  ENDTRY.

Variante 2 (CONVERT_OTFSPOOLJOB_2_PDF, CONVERT_ABAPSPOOLJOB_2_PDF)

* Nr. des Spoolauftrags
PARAMETERS: p_spool TYPE rspoid OBLIGATORY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_spool.

  DATA: it_return TYPE TABLE OF ddshretval.

  SELECT rqident, rqtitle, rqdest, rqpaper, rqdoctype
    INTO TABLE @DATA(it_spool)
    FROM tsp01
    ORDER BY rqident.

* eigene Suuchhilfe für Auswahl der Spooljobs anzeigen
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = 'RQIDENT'     " Spalte der internen Tabelle bei value_tab
      dynpprog        = sy-repid
      dynpnr          = sy-dynnr
      dynprofield     = 'P_SPOOL'     " Name des Dynpro-Feldes für die automatische Werterückgabe
      value_org       = 'S'           " Werteübergabe: C: zellenweise, S: strukturiert
      window_title    = 'Auswahl'
    TABLES
      value_tab       = it_spool      " Übergabe-Tabelle mit Werten für die Anzeige und Auswahl
      return_tab      = it_return     " Rückgabe-Tabelle mit den ausgewählten (geklickten) Elementen
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.

  IF sy-subrc = 0.
    IF lines( it_return ) > 0.
      MESSAGE it_return[ 1 ]-fieldval TYPE 'S'.
    ENDIF.
  ENDIF.

START-OF-SELECTION.

  SELECT SINGLE rqclient, rqo1name
    INTO @DATA(lv_tsp01_sel)
    FROM tsp01
    WHERE rqident = @p_spool.

  IF sy-subrc = 0.
* Dummys für Aufruf von RSPO_RETURN_SPOOLJOB
    DATA: it_otf TYPE STANDARD TABLE OF soli.
    DATA: it_pdf TYPE STANDARD TABLE OF tline.

* Beispiel: Spooljob als iTabs holen
    CALL FUNCTION 'RSPO_RETURN_SPOOLJOB'
      EXPORTING
        rqident              = p_spool
        desired_type         = 'RAW' " RAW, ALI, OTF, PDF
      TABLES
        buffer               = it_otf
        buffer_pdf           = it_pdf
      EXCEPTIONS
        no_such_job          = 1
        job_contains_no_data = 2
        selection_empty      = 3
        no_permission        = 4
        can_not_access       = 5
        read_error           = 6
        type_no_match        = 7
        OTHERS               = 8.

    IF sy-subrc <> 0.
      WRITE: 'Spoolauftrag nicht vorhanden: ', sy-subrc.
    ELSE.
      SELECT SINGLE rqdoctype FROM tsp01 INTO @DATA(lv_objtype) WHERE rqident = @p_spool.

      IF sy-subrc <> 0.
        WRITE: 'Fehler beim Lesen des Doctypes.', sy-subrc.
      ELSE.
        DATA: lv_numbytes TYPE i.
        DATA: lv_pdfspoolid LIKE tsp01-rqident.
        DATA: lv_jobname TYPE tbtcjob-jobname.
        DATA: lv_jobcount TYPE tbtcjob-jobcount.
        DATA: lv_xstring_pdf TYPE xstring.

* Konvertierung anhand des Typs vornehmen
* OTF, SMART
        CASE lv_objtype.
          WHEN 'OTF' OR 'OTFHPL2' OR 'SMART'.
            CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
              EXPORTING
                src_spoolid              = p_spool
                no_dialog                = abap_true
                pdf_destination          = 'X' " xstring
                no_background            = abap_false
              IMPORTING
                pdf_bytecount            = lv_numbytes
                pdf_spoolid              = lv_pdfspoolid
                btc_jobname              = lv_jobname
                btc_jobcount             = lv_jobcount
                bin_file                 = lv_xstring_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.

            IF sy-subrc <> 0.
              WRITE: / 'Fehler bei der OTF->PDF Konvertierung:', sy-subrc.
            ELSE.

            ENDIF.
* ALI
          WHEN 'LIST' OR 'TEXT'.
            CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
              EXPORTING
                src_spoolid              = p_spool
                no_dialog                = abap_true
                pdf_destination          = 'X' " xstring
                no_background            = abap_false
              IMPORTING
                pdf_bytecount            = lv_numbytes
                pdf_spoolid              = lv_pdfspoolid
                btc_jobname              = lv_jobname
                btc_jobcount             = lv_jobcount
                bin_file                 = lv_xstring_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.

            IF sy-subrc <> 0.
              WRITE: / 'Fehler bei der TEXT->PDF Konvertierung: ', sy-subrc.
            ELSE.

            ENDIF.

          WHEN 'ADSP'.
            WRITE: / 'Adobe Forms derzeit nicht implementiert.'.
          WHEN OTHERS.
            WRITE: / 'Verarbeitungsschritt derzeit nicht implementiert:', lv_objtype.
        ENDCASE.

* wenn erfolgreich -> PDF speichern
        IF lv_numbytes > 0 AND xstrlen( lv_xstring_pdf ) > 0.
          DATA: lv_action TYPE i.
          DATA: lv_filename TYPE string.
          DATA: lv_fullpath TYPE string.
          DATA: lv_path TYPE string.

          TRY.
* SaveDialog
              cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                            default_file_name = |{ p_spool }|
                                                            default_extension = 'pdf'
                                                            file_filter       = '(*.pdf)|*.pdf|'
                                                          CHANGING
                                                            filename          = lv_filename
                                                            path              = lv_path
                                                            fullpath          = lv_fullpath
                                                            user_action       = lv_action ).

              IF lv_action EQ cl_gui_frontend_services=>action_ok.
* xstring -> solix
                DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( lv_xstring_pdf ).

* PDF lokal speichern
                cl_gui_frontend_services=>gui_download( EXPORTING
                                                          filename     = lv_fullpath
                                                          filetype     = 'BIN'
                                                          bin_filesize = lv_numbytes
                                                        CHANGING
                                                          data_tab     = it_raw_data ).

* Browserfenster erzeugen
                DATA(o_html) = NEW cl_gui_html_viewer( parent = cl_gui_container=>default_screen ).

* PDF im ABAP-Fenster anzeigen
                o_html->show_url( url      = CONV skwf_url( lv_fullpath )
                                  in_place = abap_true ).

* leere SAP-Toolbar ausblenden
                cl_abap_list_layout=>suppress_toolbar( ).

* cl_gui_container=>default_screen erzwingen
                WRITE: / |Datei erfolgreich unter { lv_fullpath } gespeichert.|.
              ENDIF.

            CATCH cx_root INTO DATA(e_text).          " Oberklasse für Exceptions abfangen und Kurztext übergeben
              WRITE: / e_text->get_text( ).
          ENDTRY.

        ENDIF.
      ENDIF.
    ENDIF.
  ELSE.
    WRITE: / |Spoolauftrag { p_spool } nicht vorhanden.|.
  ENDIF.