[ABAP] Excel-Datei (*.xlsx) mit Desktop-Office-Integration (DOI) laden und anzeigen

* Dummy-Screen für cl_gui_container=>default_screen deklarieren
SELECTION-SCREEN BEGIN OF SCREEN 100.
SELECTION-SCREEN END OF SCREEN 100.

START-OF-SELECTION.

  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 = |xlsx (*.xlsx)\|*.xlsx\|{ 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 Datei ausgewählt wurde
        IF lines( it_files ) > 0.

          DATA: o_oic TYPE REF TO i_oi_container_control.
          DATA: o_oid TYPE REF TO i_oi_document_proxy.
          DATA: o_err TYPE REF TO i_oi_error.

          c_oi_container_control_creator=>get_container_control( IMPORTING
                                                                   control = o_oic
                                                                   error   = o_err ).

          IF abap_true = o_err->has_succeeded.
            o_oic->init_control( EXPORTING
                                   inplace_enabled     = abap_true
                                   no_flush            = abap_true
                                   r3_application_name = 'Excel Tabelle'
                                   parent              = cl_gui_container=>default_screen
                                 IMPORTING
                                   error               = o_err ).

            IF abap_true = o_err->has_succeeded.
              o_oic->get_document_proxy( EXPORTING
                                           document_type  = soi_doctype_excel_sheet " 'Excel.Sheet'
                                         IMPORTING
                                           document_proxy = o_oid
                                           error          = o_err ).

              IF abap_true = o_err->has_succeeded.
* URL muss für das Laden eines lokalen Files mit 'FILE://' beginnen
                DATA(lv_file) = 'FILE://' && it_files[ 1 ]-filename.
* *.xlsx von URL (File) laden und anzeigen
                o_oid->open_document( EXPORTING
                                        document_title = 'Excel'
                                        document_url   = CONV swk_url( lv_file )
                                        open_inplace   = abap_true
                                      IMPORTING
                                        error          = o_err ).

                IF abap_true = o_err->has_succeeded.
* leere Toolbar ausblenden
                  cl_abap_list_layout=>suppress_toolbar( ).

* cl_gui_container=>default_screen erzwingen
                  CALL SCREEN 100.

* Schließen eines Dokuments in der Office-Anwendung
                  o_oid->close_document( IMPORTING error = o_err ).
                  IF o_err->has_succeeded = abap_true.

* Schließen eines Visual-Basic-Dokuments
                    o_oid->close_activex_document( IMPORTING error = o_err ).
                    IF o_err->has_succeeded = abap_true.

* Freigabe des für das Dokument reservierten Speichers
                      o_oid->release_document( IMPORTING error = o_err ).

                      IF o_err->has_succeeded = abap_true.
                        FREE: o_oid.
                      ENDIF.
                    ENDIF.
                  ENDIF.

* baut das Control ab
                  o_oic->destroy_control( IMPORTING error = o_err ).

                  IF o_err->has_succeeded = abap_true.
                    FREE: o_oic.
                  ENDIF.

                ENDIF.
              ENDIF.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.

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

[ABAP] Komplexbeispiel: Binärdaten Screenshot mit GZIP komprimieren, in Base64-String wandeln und zurück (CL_ABAP_GZIP, SSFC_BASE64_ENCODE, SSFC_BASE64_DECODE)

TRY.
    DATA: lv_mime_type TYPE string.
    DATA: lv_image_bytes TYPE xstring.

* Ein paar Bytes holen: Screenshot aufnehmen
    cl_gui_frontend_services=>get_screenshot( IMPORTING
                                                mime_type_str = lv_mime_type
                                                image         = lv_image_bytes ).

    DATA: lv_xstr_gzip TYPE xstring.

* Binärdaten mit GZIP komprimieren
    cl_abap_gzip=>compress_binary( EXPORTING raw_in   = lv_image_bytes
                                   IMPORTING gzip_out = lv_xstr_gzip ).

    DATA(lv_base64_data) = ||.

* Base64 codieren
    CALL FUNCTION 'SSFC_BASE64_ENCODE'
      EXPORTING
        bindata                  = lv_xstr_gzip
      IMPORTING
        b64data                  = lv_base64_data
      EXCEPTIONS
        ssf_krn_error            = 1
        ssf_krn_noop             = 2
        ssf_krn_nomemory         = 3
        ssf_krn_opinv            = 4
        ssf_krn_input_data_error = 5
        ssf_krn_invalid_par      = 6
        ssf_krn_invalid_parlen   = 7
        OTHERS                   = 8.
    IF sy-subrc = 0.

* Base64-String in der Listenausgabe darstellen
      DATA: it_text80 TYPE STANDARD TABLE OF text80 WITH DEFAULT KEY.
* formatierte Ausgabe (80 Zeichen Breite)
      CALL FUNCTION 'SWA_STRING_TO_TABLE'
        EXPORTING
          character_string = lv_base64_data
        IMPORTING
          character_table  = it_text80.

      LOOP AT it_text80 ASSIGNING FIELD-SYMBOL(<t>).
        WRITE: / <t>.
      ENDLOOP.

      DATA: lv_xstr_dec TYPE xstring.

* Base64 decodieren
      CALL FUNCTION 'SSFC_BASE64_DECODE'
        EXPORTING
          b64data                  = lv_base64_data
        IMPORTING
          bindata                  = lv_xstr_dec
        EXCEPTIONS
          ssf_krn_error            = 1
          ssf_krn_noop             = 2
          ssf_krn_nomemory         = 3
          ssf_krn_opinv            = 4
          ssf_krn_input_data_error = 5
          ssf_krn_invalid_par      = 6
          ssf_krn_invalid_parlen   = 7
          OTHERS                   = 8.

      IF sy-subrc = 0.
        DATA: lv_pic_bytes TYPE xstring.

* Binärdaten mit GZIP dekomprimieren
        cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = lv_xstr_dec IMPORTING raw_out = lv_pic_bytes ).

* freier Custom-Container innerhalb der Listenansicht
        DATA(o_cnt) = NEW cl_gui_custom_container( container_name = ''
                                                   repid          = 'SAPMSSY0'
                                                   dynnr          = '0120' ).

* Position des Containers setzen
        o_cnt->set_top( 50 ).
        o_cnt->set_left( 50 ).
        o_cnt->set_width( 640 ).
        o_cnt->set_height( 240 ).

* Bild über die URL laden und anzeigen
        DATA(o_pic) = NEW cl_gui_picture( parent = o_cnt ).
        o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_stretch ).

* xstring -> solix
        DATA(it_bin_data) = cl_bcs_convert=>xstring_to_solix( lv_pic_bytes ).

        DATA: lv_url TYPE swk_url.

* temporäre URL für das Bild erzeugen
        CALL FUNCTION 'DP_CREATE_URL'
          EXPORTING
            type                 = 'image/jpeg' " https://wiki.selfhtml.org/wiki/Referenz:MIME-Typen
            subtype              = ''
          TABLES
            data                 = it_bin_data
          CHANGING
            url                  = lv_url
          EXCEPTIONS
            dp_invalid_parameter = 1
            dp_error_put_table   = 2
            dp_error_general     = 3
            OTHERS               = 4.

* Bild von URL laden
        o_pic->load_picture_from_url_async( lv_url ).

      ENDIF.
    ENDIF.

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

[ABAP] Interne Tabelle als Excel-Datei (*.xlsx) speichern (cl_fdt_xl_spreadsheet)

* Mit Hilfe der Klasse cl_fdt_xl_spreadsheet können über XML-Transformationen
* Daten aus SAP in XLSX-Dateien geschrieben werden
TRY.
* Testdaten lesen
    SELECT * FROM t001 INTO TABLE @DATA(it_t001).

    IF sy-subrc = 0.
* Header erzeugen
      DATA: it_columns TYPE if_fdt_doc_spreadsheet=>t_column.
      DATA: lv_head TYPE t001.
      DATA(o_desc) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_data( lv_head ) ).

      LOOP AT o_desc->get_components( ) ASSIGNING FIELD-SYMBOL(<c>).
        IF <c> IS ASSIGNED.
          IF <c>-type->kind = cl_abap_typedescr=>kind_elem.
            APPEND VALUE #( id           = sy-tabix
                            name         = <c>-name
                            display_name = <c>-name
                            is_result    = abap_true
                            type         = <c>-type ) TO it_columns.
          ENDIF.
        ENDIF.
      ENDLOOP.

* itab + header -> XML -> xstring
* Achtung: Speicherintensiv und rel. langsam! Es sollten keine großen Datenmengen verarbeitet werden.
      DATA(lv_bin_data) = cl_fdt_xl_spreadsheet=>if_fdt_doc_spreadsheet~create_document( columns      = it_columns " optional
                                                                                         itab         = REF #( it_t001 )
                                                                                         iv_call_type = if_fdt_doc_spreadsheet=>gc_call_dec_table ).
      IF xstrlen( lv_bin_data ) > 0.
        DATA: lv_action TYPE i.
        DATA: lv_filename TYPE string.
        DATA: lv_fullpath TYPE string.
        DATA: lv_path TYPE string.

* Save-Dialog
        cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                      default_file_name = 'Excel.xlsx'
                                                      default_extension = 'xlsx'
                                                      file_filter       = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                    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 (RAW)
          DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_bin_data ).

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


        ENDIF.
      ENDIF.
    ENDIF.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

[ABAP] Excel-Datei (*.xlsx) in interne Tabelle laden (cl_fdt_xl_spreadsheet)

* Mit Hilfe der Klasse cl_fdt_xl_spreadsheet können über XML-Transformationen
* aus XLSX-Dateien Daten extrahiert und in eine interne Tabelle konvertiert werden
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 = |xlsx (*.xlsx)\|*.xlsx\|{ 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 Datei ausgewählt wurde
      IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
        DATA: lv_filesize TYPE w3param-cont_len.
        DATA: lv_filetype TYPE w3param-cont_type.
        DATA: it_bin_data TYPE w3mimetabtype.

* Bild 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 ).

* Excel (itab) -> XML -> Ref-Objekt
* Achtung: Speicherintensiv und rel. langsam! Es sollten keine großen Datenmengen verarbeitet werden.
        DATA(o_excel) = NEW cl_fdt_xl_spreadsheet( document_name = CONV #( it_files[ 1 ]-filename )
                                                   xdocument     = lv_bin_data ).

        DATA: it_worksheet_names TYPE if_fdt_doc_spreadsheet=>t_worksheet_names.

* Worksheetnamen ermitteln
        o_excel->if_fdt_doc_spreadsheet~get_worksheet_names( IMPORTING worksheet_names = it_worksheet_names ).

        IF lines( it_worksheet_names ) > 0.
* erste Worksheet -> REF to itab
          DATA(o_sheet_itab) = o_excel->if_fdt_doc_spreadsheet~get_itab_from_worksheet( it_worksheet_names[ 1 ] ).

          FIELD-SYMBOLS: <sheet> TYPE table.
          ASSIGN o_sheet_itab->* TO <sheet>.

          cl_demo_output=>write_data( <sheet> ).

* HTML-Daten aus itab generieren
          DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
          cl_abap_browser=>show_html( EXPORTING
                                        title        = 'Excel-Worksheet'
                                        html_string  = lv_html
                                        container    = cl_gui_container=>default_screen ).

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

  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.

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

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

[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] Google-API: QR-Code generieren und Bild herunterladen/speichern

* Dateiname der Bilddatei zum hochladen
PARAMETERS: p_fname TYPE file_table-filename OBLIGATORY.

START-OF-SELECTION.
* URL zur Google-API für die Erstellung des QR-Codes
  DATA(lv_url) = |http://chart.apis.google.com/chart?chs=200x200&cht=qr&chld=\|1&chl=viele%20lustige%20Zeichen/chart.png|.

  TRY.
      DATA: o_client TYPE REF TO if_http_client.
* Client-Objekt erzeugen
      cl_http_client=>create_by_url( EXPORTING
                                       url     = lv_url
                                     IMPORTING
                                       client  = o_client ).
      IF sy-subrc = 0.
* HTTP GET senden
        o_client->send( ).

* Response lesen
        o_client->receive( ).

        DATA: lv_http_status TYPE i.
        DATA: lv_status_text TYPE string.

* HTTP Return Code holen
        o_client->response->get_status( IMPORTING
                                          code   = lv_http_status
                                          reason = lv_status_text ).

* Wenn Status 200 (Ok)
        IF lv_http_status = 200.

* Binärdaten (QR-Code) auslesen
          DATA(lv_xdata) = o_client->response->get_data( ).

          o_client->close( ).

* xstring -> solix
          DATA(it_img_conv_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xdata ).

          WRITE: / |{ p_fname }.png|.

* Image lokal speichern
          cl_gui_frontend_services=>gui_download( EXPORTING
                                                    filename     = |{ p_fname }.png|
                                                    filetype     = 'BIN'
                                                    bin_filesize = xstrlen( lv_xdata )
                                                  CHANGING
                                                    data_tab     = it_img_conv_data ).

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

[ABAP] SMARTFORM-Formular (OTF) als PDF speichern und anzeigen

* https://www.tutorialspoint.com/sap_smart_forms/
* https://www.berater-wiki.de/Smart_Forms_Generierter_Funktionsbaustein

* 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'
          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] Screenshot speichern

DATA: lv_mime_type TYPE string.
DATA: lv_image_bytes TYPE xstring.

TRY.
* Screenshot aufnehmen
    cl_gui_frontend_services=>get_screenshot( IMPORTING
                                                mime_type_str = lv_mime_type
                                                image         = lv_image_bytes ).

* MIME-Typ (PNG) ausgeben
    WRITE: / lv_mime_type.

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

* Savedialog anzeigen
    cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                  default_file_name = 'image.png'
                                                  default_extension = 'png'
                                                  file_filter       = '(*.png)|*.png|'
                                                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 (RAW)
      DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_image_bytes ).

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

    ENDIF.
  CATCH cx_root INTO DATA(e_txt).
ENDTRY.
ENDTRY.