[ABAP] PDFs mittels Klasse CL_RSPO_PDF_MERGE zusammenfügen (mergen)

* siehe auch RSPO_TEST_MERGE_PDF_FILES (Beispielreport zum Aufruf des PDF-Merge ABAP-Interfaces)
* benötigt KEIN ADS (Adobe Document Service)
TRY.
    DATA: lv_rc TYPE i.
    DATA: it_files TYPE filetable.
    DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
    cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                  file_filter    = |pdf (*.pdf)\|*.pdf\|{ cl_gui_frontend_services=>filetype_all }|
                                                  multiselection = abap_true
                                                CHANGING
                                                  file_table  = it_files
                                                  rc          = lv_rc
                                                  user_action = lv_action ).

    IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn mind. zwei Dateien ausgewählt worden sind
      IF lines( it_files ) > 1.
* PDF-Merge Objekt
        DATA(o_pdf_merger) = NEW cl_rspo_pdf_merge( ).

* Dateiliste durchgehen
        LOOP AT it_files ASSIGNING FIELD-SYMBOL(<f>).

          DATA(lv_filesize) = CONV w3param-cont_len( '0' ).
          DATA(it_bin_data) = VALUE w3mimetabtype( ).

* Datei auf Appl. Server hochladen (binary)
          cl_gui_frontend_services=>gui_upload( EXPORTING
                                                  filename   = |{ it_files[ sy-tabix ]-filename }|
                                                  filetype   = 'BIN'
                                                IMPORTING
                                                  filelength = lv_filesize
                                                CHANGING
                                                  data_tab   = it_bin_data ).

* solix -> xstring
          DATA(lv_bin_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

* Binärdaten der PDF-Datei als xstring zum Merger-Objekt hinzufügen
          o_pdf_merger->add_document( lv_bin_data ).

          WRITE: / |Hinzugefügt: { it_files[ sy-tabix ]-filename } ({ lv_filesize } Bytes)|.
        ENDLOOP.

* PDF-Dokumente mergen
        o_pdf_merger->merge_documents( IMPORTING merged_document = DATA(lv_merged_pdf)
                                                 rc              = lv_rc ).

        IF lv_rc = 0.
          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_extension   = 'pdf'
                                                        default_file_name   = 'merged.pdf'
                                                        file_filter         = |pdf (*.pdf)\|*.pdf\|{ cl_gui_frontend_services=>filetype_all }|
                                                        prompt_on_overwrite = abap_true
                                                      CHANGING
                                                        filename          = lv_filename     " Dateiname
                                                        path              = lv_path         " Pfad
                                                        fullpath          = lv_fullpath     " Pfad + Dateiname
                                                        user_action       = lv_action ).    " Benutzeraktion

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

            DATA(lv_size_merged) = xstrlen( lv_merged_pdf ).

* Datei lokal speichern
            cl_gui_frontend_services=>gui_download( EXPORTING
                                                      filename                = lv_fullpath
                                                      filetype                = 'BIN'
                                                      bin_filesize            = lv_size_merged
                                                    CHANGING
                                                      data_tab                = it_bin_data_merged ).

            WRITE: / |Gespeichert: { lv_fullpath } ({ lv_size_merged } Bytes)|.

          ENDIF.
        ENDIF.
      ENDIF.
    ENDIF.
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Links

[ABAP] PDF-Dokument per ADS (Adobe Document Service) in XML konvertieren

* https://www.berater-wiki.de/XML_aus_PDF_extrahieren_mit_Testprogramm_FP_PDF_TEST_03
* http://sapfirst.blogspot.com/2014/05/inbound-abap-proxy-with-pdf-attachment.html
* Paket:     SAFP
* Programme: FP_PDF_TEST_01 (PDF erzeugen)
*            FP_PDF_TEST_03 (Datenextraktion)
* XFD (XML Form Data): https://www.berater-wiki.de/XFD-Datei_(Values_of_PDF_form_fields_in_XML)
* XFT (XML Form Template)
* XDC (XML Printer Definitions)

TRY.
    DATA: lv_rc TYPE i.
    DATA: it_files TYPE filetable.
    DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
    cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                  file_filter    = |pdf (*.pdf)\|*.pdf\|{ cl_gui_frontend_services=>filetype_all }|
                                                CHANGING
                                                  file_table  = it_files
                                                  rc          = lv_rc
                                                  user_action = lv_action ).

    IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn mind. zwei Dateien ausgewählt worden sind
      IF lines( it_files ) > 0.
        DATA(lv_filesize) = CONV w3param-cont_len( '0' ).
        DATA(it_bin_data) = VALUE w3mimetabtype( ).

* Datei auf Appl. Server hochladen (binary)
        cl_gui_frontend_services=>gui_upload( EXPORTING
                                                filename   = |{ it_files[ 1 ]-filename }|
                                                filetype   = 'BIN'
                                              IMPORTING
                                                filelength = lv_filesize
                                              CHANGING
                                                data_tab   = it_bin_data ).

* solix -> xstring
        DATA(lv_bin_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

* Form Processor erzeugen
        DATA(o_form_processor) = cl_fp=>get_reference( ).
* PDF-Objekt erzeugen
        DATA(o_pdf) = o_form_processor->create_pdf_object( ).
* Binärdaten an PDF-Objekt übergeben
        o_pdf->set_document( pdfdata = lv_bin_data ).
* PDF-Daten extrahieren
        o_pdf->set_extractdata( ).
* ADS-Server rufen
        o_pdf->execute( ).
* extrahierte Daten holen (xstring)
        o_pdf->get_data( IMPORTING formdata = DATA(lv_pdf_formdata) ).

* xstring (binary) -> XML-string (UTF-8)
        DATA(lv_pdf_xml_str) = ||.
        DATA(o_conv) = cl_abap_conv_in_ce=>create( input    = lv_pdf_formdata
                                                   encoding = 'UTF-8' ).
        o_conv->read( IMPORTING data = lv_pdf_xml_str ).

* XML-Zugriff
        DATA(o_ixml) = cl_ixml=>create( ).

        DATA(o_sf) = o_ixml->create_stream_factory( ).
        DATA(o_doc) = o_ixml->create_document( ).
        DATA(o_stream) = o_sf->create_istream_string( lv_pdf_xml_str ).

* XML-Parser
        DATA(o_parser) = o_ixml->create_parser( stream_factory = o_sf
                                                istream        = o_stream
                                                document       = o_doc ).

* XML parsen
        IF o_parser->parse( ) = 0.

          DATA(o_root) = o_doc->get_root_element( ).

          ...

        ELSE.
* Fehlerauswertung
          DO o_parser->num_errors( ) TIMES.
            DATA(o_err) = o_parser->get_error( index = sy-index - 1 ).
            IF o_err IS BOUND.
              WRITE: / o_err->get_column( ), o_err->get_line( ), o_err->get_reason( ).
            ENDIF.
          ENDDO.
        ENDIF.

      ENDIF.
    ENDIF.
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] CONVERT_OTF: OTF-Format als PDF speichern und anzeigen (Beispiel Smart Forms-Formular)

* https://www.tutorialspoint.com/sap_smart_forms/
* https://www.berater-wiki.de/Smart_Forms_Generierter_Funktionsbaustein
* https://mind-forms.de/sap-formulartechnologien/sapscript/sapscript-formulare-in-pdf-umwandeln/

* Tabelle STXFADM

PARAMETERS: p_form TYPE stxfadm-formname DEFAULT 'SF_EXAMPLE_01'.
PARAMETERS: p_open TYPE abap_bool DEFAULT abap_true.

START-OF-SELECTION.
  DATA(lv_control_parameters) = VALUE ssfctrlop( no_dialog = abap_true
                                                 preview   = space
                                                 getotf    = abap_true
                                                 langu     = sy-langu ).

  DATA: lv_devtype TYPE rspoptype.

* Smart Forms: Gerätetyp nach Sprache ermitteln
  CALL FUNCTION 'SSF_GET_DEVICE_TYPE'
    EXPORTING
      i_language             = lv_control_parameters-langu
    IMPORTING
      e_devtype              = lv_devtype
    EXCEPTIONS
      no_language            = 1
      language_not_installed = 2
      no_devtype_found       = 3
      system_error           = 4
      OTHERS                 = 5.

  IF sy-subrc = 0.
    DATA(lv_output_options) = VALUE ssfcompop( tddest    = 'LOCL'
                                               tdprinter = lv_devtype ).

* FuBa Name ermitteln
    DATA: lv_fm_name TYPE rs38l_fnam.

* Smart Forms: Name des Funktionsbausteins bestimmen
    CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
      EXPORTING
        formname           = p_form
      IMPORTING
        fm_name            = lv_fm_name
      EXCEPTIONS
        no_form            = 1
        no_function_module = 2
        OTHERS             = 3.

    IF sy-subrc = 0.

      DATA: lv_document_output_info TYPE ssfcrespd.
      DATA: lv_job_output_info TYPE ssfcrescl.
      DATA: lv_job_output_options TYPE ssfcresop.

* Zusatzparameter für Fuba für Formular 'SF_EXAMPLE_01'
      DATA: lv_customer TYPE scustom.
      DATA: it_bookings TYPE ty_bookings.
      DATA: it_connections TYPE ty_connections.

* FuBa der Form aufrufen und OTF-Daten erzeugen
      CALL FUNCTION lv_fm_name
        EXPORTING
          control_parameters   = lv_control_parameters
          output_options       = lv_output_options
          user_settings        = space
          customer             = lv_customer
          bookings             = it_bookings
          connections          = it_connections
        IMPORTING
          document_output_info = lv_document_output_info
          job_output_info      = lv_job_output_info
          job_output_options   = lv_job_output_options
        EXCEPTIONS
          formatting_error     = 1
          internal_error       = 2
          send_error           = 3
          user_canceled        = 4
          OTHERS               = 5.

      IF sy-subrc = 0.

        DATA(lv_bin_filesize) = 0.

        DATA: lv_pdf_xstring TYPE xstring.
        DATA: it_lines TYPE tline_t.

* Konvertieren von OTF-Format in verschiedene Formate (TLINE Tabelle)
        CALL FUNCTION 'CONVERT_OTF'
          EXPORTING
            format                = 'PDF' " ASCII, PDF
          IMPORTING
            bin_filesize          = lv_bin_filesize
*           bin_file              = lv_pdf_xstring             " PDF Binärdaten -> einkommentieren, wenn benötigt, sonst it_lines leer!
          TABLES
            otf                   = lv_job_output_info-otfdata " Input OTF
            lines                 = it_lines                   " Output
          EXCEPTIONS
            err_max_linewidth     = 1
            err_format            = 2
            err_conv_not_possible = 3
            err_bad_otf           = 4
            OTHERS                = 5.

        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.
* Speichern-Dialog
              cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                            default_file_name = |{ p_form }.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.
* Daten lokal speichern
                cl_gui_frontend_services=>gui_download( EXPORTING
                                                          bin_filesize = lv_bin_filesize
                                                          filename     = lv_fullpath
                                                          filetype     = 'BIN'
                                                        CHANGING
                                                          data_tab     = it_lines ).

                WRITE: / |Datei erfolgreich unter { lv_fullpath } gespeichert.|.

                IF abap_true = p_open.
* Datei anzeigen
                  cl_gui_frontend_services=>execute( document = lv_fullpath ).
                ENDIF.
              ENDIF.

            CATCH cx_root INTO DATA(e_text).
              WRITE: / e_text->get_text( ).
          ENDTRY.
        ENDIF.
      ELSE.

        DATA: it_sf_errors TYPE tsferror.

* Smart Forms: Lesen der Fehlertabelle nach Aufruf
        CALL FUNCTION 'SSF_READ_ERRORS'
          IMPORTING
            errortab = it_sf_errors.

        cl_demo_output=>display( it_sf_errors ).
      ENDIF.

    ELSE.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

  ELSE.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

[ABAP] PDF im Browserfenster anzeigen

TRY.
    DATA: it_sel_filetab TYPE filetable.
    DATA: ret_code TYPE i.
    DATA: lv_action TYPE i.

* FileOpen-Dialog für Dateiauswahl anzeigen
    cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                  window_title            = 'PDF-Datei öffnen'
                                                  multiselection          = abap_false
                                                CHANGING
                                                  file_table              = it_sel_filetab
                                                  rc                      = ret_code    " Anzahl ausgewählte Dateien, -1 bei Fehler
                                                  user_action             = lv_action ).


    IF lv_action = cl_gui_frontend_services=>action_ok.
* 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      = it_sel_filetab[ 1 ]-filename
                        in_place = abap_true ).

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

* cl_gui_container=>default_screen erzwingen
      WRITE: / space.
    ENDIF.

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[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.

[ABAP] Email mit PDF-Anhang versenden

* https://www.sapnuts.com/tutorials/Sending-email-with-attachment-in-SAP-ABAP/singlepage.html
* http://www.sapwiki.cl/wiki/index.php?title=SAP_ABAP_CORREO
* http://www.berater-wiki.de/index.php?title=PDF_per_Mail/Fax_verschicken
* https://wiki.scn.sap.com/wiki/display/ABAP/Sending+Mails+-+Home+Page?original_fqdn=wiki.sdn.sap.com#SendingMails-HomePage-CL_BCS
* http://www.apentia-forum.de/viewtopic.php?f=2&t=74925&start=0

PARAMETERS: p_email TYPE adr6-smtp_addr OBLIGATORY DEFAULT 'email@email.com'.     " Email-Adr. des Empfängers
PARAMETERS: p_subj TYPE string DEFAULT 'Testmail.'.                               " Email-Subject
PARAMETERS: p_bdytxt TYPE soli DEFAULT 'Testtext.'.                               " eine Mailtext-Zeile
PARAMETERS: p_pdf TYPE file_table-filename OBLIGATORY DEFAULT 'C:\Temp\Test.pdf'. " PDF-Anhang

* wenn die F4-Hilfe für den Dateinamen aufgerufen wird
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_pdf.
  DATA: lv_rc TYPE i.
  DATA: it_files TYPE filetable.
  DATA: lv_action TYPE i.

* File-Tabelle leeren, da hier noch alte Einträge von vorherigen
* Aufrufen drin stehen können
  CLEAR: it_files.
* FileOpen-Dialog aufrufen
  TRY.
      cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                    file_filter = |PDF (*.pdf)\|*.pdf\|{ cl_gui_frontend_services=>filetype_all }|
                                                  CHANGING
                                                    file_table  = it_files     " ausgewählte Dateien
                                                    rc          = lv_rc        " Anzahl ausgewählte Dateien, -1 bei Fehler
                                                    user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
          p_pdf = it_files[ 1 ]-filename.
        ENDIF.
      ENDIF.
    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'S'.
  ENDTRY.

START-OF-SELECTION.
  DATA: lv_pdf_size TYPE i.
  DATA: it_pdf_data_tab TYPE solix_tab.

  TRY.
* PDF-Datei vom lokalen Rechner auf den Appl.-Server hochladen
      cl_gui_frontend_services=>gui_upload( EXPORTING
                                              filename   = |{ p_pdf }|
                                              filetype   = 'BIN'
                                            IMPORTING
                                              filelength = lv_pdf_size
                                            CHANGING
                                              data_tab   = it_pdf_data_tab ).

      IF sy-subrc = 0.
* it_body_txt beinhaltet die Zeilen des Body-Textes
* die Zeilen sind jeweils char255 Zeichen lang
* eine Zeile (line) ist vom Typ SOLI
        DATA(it_body_txt) = VALUE soli_tab( ( line = p_bdytxt ) ).

* Document (Mail) erzeugen
* Email-Subject (i_subject) darf hier max. 50 Zeichen lang sein (Subject-Darstellung in der SOST)
* die "richtige" Übergabe des Email-Subjects erfolgt weiter unten in der Funktion set_message_subject( )
        DATA(o_document) = cl_document_bcs=>create_document( i_type    = 'RAW'
                                                             i_text    = it_body_txt
                                                             i_subject = CONV so_obj_des( p_subj ) ).

* Attachement hinzufügen -> Typen siehe Tabelle TSOTD
* siehe auch: http://www.apentia-forum.de/viewtopic.php?f=2&t=74925&start=0
        o_document->add_attachment( i_attachment_type    = 'EXT'
                                    i_attachment_subject = |{ p_pdf }|
                                    i_att_content_hex    = it_pdf_data_tab ).

* Sendrequest erzeugen
        DATA(o_send_request) = cl_bcs=>create_persistent( ).
* Email-Subject festlegen, ip_subject ist vom Typ String
        o_send_request->set_message_subject( ip_subject = p_subj ).
* Die Mail an den Sendrequest hängen
        o_send_request->set_document( o_document ).

* Sender
* SAP-User als Sender
        DATA(o_sender) = cl_sapuser_bcs=>create( sy-uname ).
* Email-Adresse als Sender
*        DATA(o_sender) = cl_cam_address_bcs=>create_internet_address( i_address_string = 'email@email.com' ).
        o_send_request->set_sender( o_sender ).

* Empfänger -> beachte: u.U. sind Email-Adressen mit Umlauten nicht zulässig -> Exception
        DATA(o_recipient) = cl_cam_address_bcs=>create_internet_address( p_email ).
        o_send_request->add_recipient( i_recipient = o_recipient
                                       i_express = abap_true
                                     " i_copy      = abap_true " CC
                                     " i_blind_copy = abap_true " BCC
                                     ).

* Sofort senden
        o_send_request->set_send_immediately( abap_true ).

* Dokument senden
        IF o_send_request->send( i_with_error_screen = abap_true ) = abap_true.
          WRITE: / 'Email gesendet.'.
        ELSE.
          WRITE: / 'Fehler beim Email-Versand.'.
        ENDIF.

        DATA: lv_in_update_task TYPE sy-subrc.

* Überprüft, ob das Programm im Verbucher abläuft
        CALL FUNCTION 'TH_IN_UPDATE_TASK'
          IMPORTING
            in_update_task = lv_in_update_task.

        IF lv_in_update_task EQ 0.
* Persistenzdienste des Objektes starten, sonst wird ggf. die Mail nicht gesendet
          COMMIT WORK.
        ENDIF.

      ENDIF.
    CATCH cx_root INTO DATA(e_text).
      WRITE: / e_text->get_text( ).
  ENDTRY.