[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] Base64 codierten String in Binärdaten (RAW) konvertieren und mit GZIP dekomprimieren

* Base64 String welcher GZIP-komprimierte Binärdaten beinhaltet
DATA(lv_base64_data) = |C0ktLikuKcrMS1dIO7ynSCElM1XBOz+3oCi1uDgzP08hN7NEwT3KM0APAA==|.

* Binärdaten
DATA: lv_xstr TYPE xstring.
DATA: lv_xstr_gzip TYPE xstring.

* Base64 decodieren
CALL FUNCTION 'SSFC_BASE64_DECODE'
  EXPORTING
    b64data                  = lv_base64_data
  IMPORTING
    bindata                  = lv_xstr_gzip
  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.
  cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = lv_xstr_gzip IMPORTING raw_out = lv_xstr ).

  DATA: lv_str TYPE string.

* xstring (binary) -> string (UTF-8)
  DATA(o_conv) = cl_abap_conv_in_ce=>create( input    = lv_xstr
                                             encoding = 'UTF-8' ).
  o_conv->read( IMPORTING data = lv_str ).

  WRITE: / lv_str.
ENDIF.

[ABAP] Konvertierung einer Struktur in Binärdaten (x, xstring) und zurück

Variante 1 (FIELD-SYMBOLS, CASTING: Struktur <-> Typ x)

* einfachen Typ deklarieren
TYPES: BEGIN OF ty_struct,
         matnr TYPE matnr,
         int   TYPE i,
         float TYPE f,
         text  TYPE char10,
       END OF ty_struct.

* Feldsymbol für Binärdaten
FIELD-SYMBOLS <x> TYPE x.
* Feldsymbol für Strukturdaten
FIELD-SYMBOLS <s> TYPE ty_struct.

* Struktur anlegen, deren Daten kovertiert werden soll
DATA(lv_s) = VALUE ty_struct( matnr = '1234567890'
                              int   = 1
                              float = '1.1'
                              text  = 'abc' ).

* Struktur in Binärdaten umwandeln
ASSIGN lv_s TO <x> CASTING.
IF <x> IS ASSIGNED.
* Binärdaten in Struktur umwandeln
  ASSIGN <x> TO <s> CASTING.
  IF <s> IS ASSIGNED.
    DATA(lv_s2) = <s>.
* Datenausgabe
    WRITE: / lv_s2-matnr.
    WRITE: / lv_s2-int.
    WRITE: / lv_s2-float.
    WRITE: / lv_s2-text.
  ENDIF.
ENDIF.

Variante 2 (xstring -> Struktur)

* https://www.consolut.com/s/sap-ides-zugriff/d/e/doc/M-CL_ABAP_CONV_IN_CE/
* einfachen Typ deklarieren
TYPES: BEGIN OF ty_struct,
         text TYPE char5,
         int  TYPE i,
       END OF ty_struct.

* Zielstruktur
DATA(lv_s) = VALUE ty_struct( ).

* Konverterobjekt mit UTF-8 und Little Endian Konvertierung
DATA(o_conv) = cl_abap_conv_in_ce=>create( encoding = 'UTF-8' endian = 'L' ).
* View-Objekt
DATA(o_view) = cl_abap_view_offlen=>create_legacy_view( lv_s ).
* Eingabepuffer mit Binärdaten
DATA(lv_buffer) = CONV xstring( '616263202000000005000000' ).

* Konvertierung xstring -> Struct
*  in: HEX: 616263202000000005000000
* out: Struct: text: abc
*               ínt: 5
o_conv->convert_struc( EXPORTING
                         input = lv_buffer
                         view  = o_view
                       IMPORTING
                         data  = lv_s ).

* Datenausgabe
WRITE: / lv_s-text.
WRITE: / lv_s-int.

Variante 3 (Struktur -> xstring)

* https://www.consolut.com/s/sap-ides-zugriff/d/e/doc/M-CL_ABAP_CONV_OUT_CE/
* einfachen Typ deklarieren
TYPES: BEGIN OF ty_struct,
         text TYPE char5,
         int  TYPE i,
       END OF ty_struct.

* Struktur mit Quelldaten
DATA(lv_s) = VALUE ty_struct( text = 'abc'
                              int  = 5 ).

* Konverterobjekt mit UTF-8 und Little Endian Konvertierung
DATA(o_conv) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' endian = 'L' ).
* View-Objekt
DATA(o_view) = cl_abap_view_offlen=>create_legacy_view( lv_s ).
* Ausgabepuffer für Binärdaten
DATA: lv_buffer type xstring.

* Konvertierung Struct -> xstring
*  in: Struct: text: abc
*               ínt: 5
* out: HEX: 616263202000000005000000
o_conv->convert_struc( EXPORTING
                         data   = lv_s
                         view   = o_view
                       IMPORTING
                         buffer = lv_buffer ).

* Datenausgabe
WRITE: / lv_buffer.

[ABAP] itab -> JSON

* Variante 1 (CALL TRANSFORMATION)
TYPES: BEGIN OF s_person,
         name  TYPE string,
         title TYPE string,
         age   TYPE i,
       END OF s_person.

TYPES: t_person TYPE STANDARD TABLE OF s_person WITH DEFAULT KEY.

DATA(it_persons) = VALUE t_person( ( name = 'Horst' title = 'Herr' age = 30 )
                                   ( name = 'Jutta' title = 'Frau' age = 35 )
                                   ( name = 'Ingo' title = 'Herr' age = 31 ) ).

* ABAP (iTab) -> JSON
DATA(o_writer_itab) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE values = it_persons RESULT XML o_writer_itab.
DATA: json TYPE string.
cl_abap_conv_in_ce=>create( )->convert( EXPORTING
                                          input = o_writer_itab->get_output( )
                                        IMPORTING
                                          data = json ).

WRITE: / json.

* Variante 2 (/ui2/cl_abap2json)
SELECT matnr, mtart, meins, pstat
  INTO TABLE @DATA(it_mara)
  FROM mara
  UP TO 10 ROWS.

* ABAP (iTab) -> JSON
DATA(o_conv) = NEW /ui2/cl_abap2json( ).
DATA(lv_str) = o_conv->table2json( it_data = it_mara ).

WRITE: / lv_str.

[ABAP] Unicode Codepoint in Char wandeln

* Unicode Codepoint (UCCP) als 4Char-Hex in Char (akt. Codepage) wandeln
TRY.
    DATA: lv_uccp_c4 TYPE char4 VALUE '0041'.
    WRITE: / cl_abap_conv_in_ce=>uccp( lv_uccp_c4 ).
  CATCH cx_root.
ENDTRY.

TRY.
    DATA: lv_uccp_x2 TYPE x LENGTH 2.
    lv_uccp_x2 = '0042'.
    WRITE: / cl_abap_conv_in_ce=>uccp( lv_uccp_x2 ).
  CATCH cx_root.
ENDTRY.

* Unicode Codepoint (UCCPI) als Integer in Char (akt. Codepage) wandeln
TRY.
    DATA: lv_uccpi TYPE i VALUE 65.
    WRITE: / cl_abap_conv_in_ce=>uccpi( lv_uccpi ).
  CATCH cx_root.
ENDTRY.

Weiterführende Infos: Link

[ABAP] HTTP-Client Anfrage (cl_http_client)

Testprogramm: RSHTTP20

Variante 1 (URL)

TRY.
* URL
    DATA: lv_url TYPE string VALUE 'https://www.google.de/'.

    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 o_client IS BOUND.
* Anmeldedaten übermitteln
      o_client->authenticate( username = ''
                              password = '' ).

* Logon-Popup ein- bzw. ausschalten
      o_client->propertytype_logon_popup = o_client->co_enabled.

* Version festlegen
      o_client->request->set_version( version = if_http_request=>co_protocol_version_1_0 ).

*      o_client->request->set_method( if_http_request=>co_request_method_get ).

* Header setzen
      o_client->request->set_header_field( name  = '~request_method'
                                           value = 'GET' ).

      o_client->request->set_header_field( name  = 'Content-Type'
                                           value = 'text/xml; charset=utf-8' ).

      o_client->request->set_header_field( name  = 'Accept'
                                           value = 'text/xml, text/html' ).

* HTTP GET senden, evtl. Timeout angeben
      o_client->send( timeout = if_http_client=>co_timeout_default ).

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

      WRITE: / 'HTTP_STATUS_CODE:', lv_http_status.
      WRITE: / 'STATUS_TEXT:', lv_status_text.

* vollständige HTTP Nachricht lesen
      DATA(lv_raw_message) = o_client->response->get_raw_message( ).
      DATA: lv_str_msg TYPE string.

* xstring -> string
      DATA(o_conv_r) = cl_abap_conv_in_ce=>create( input    = lv_raw_message
                                                   encoding = 'UTF-8' ).

      o_conv_r->read( IMPORTING data = lv_str_msg ).

      WRITE: / 'RAW MESSAGE:', lv_str_msg.

* Wenn Status 200 (Ok)
      IF lv_http_status = 200.
* HTTP Body als Character-Daten
        DATA(lv_result) = o_client->response->get_cdata( ).

        WRITE: / lv_result.
      ENDIF.

* HTTP Connection schließen
      o_client->close( ).
    ENDIF.
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (HTTP-Alias)

* mit SM59 einrichten
DATA: lv_http_dest TYPE rfcdest VALUE 'HTTP_ALIAS'.

DATA: o_client TYPE REF TO if_http_client.

DATA: lv_http_rc TYPE i.
DATA: lv_reason TYPE string.

TRY.
    cl_http_client=>create_by_destination( EXPORTING
                                             destination = lv_http_dest
                                           IMPORTING
                                             client      = o_client ).

    o_client->request->set_method( if_http_request=>co_request_method_get ).

    o_client->send( ).

    o_client->receive( ).

    o_client->response->get_status( IMPORTING
                                      code   = lv_http_rc
                                      reason = lv_reason ).

    IF lv_http_rc = 200.
      DATA(lv_xml) = o_client->response->get_cdata( ).

      cl_abap_browser=>show_xml( xml_string = lv_xml
                                 title      = 'XML-Daten'
                                 size       = cl_abap_browser=>large ).
    ELSE.
      WRITE: / 'Fehler: ', lv_http_rc.
    ENDIF.

    o_client->close( ).

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

Weiterführende Infos: Link und Link

[ABAP] Konvertierung Integer (ASCII) HEX CHAR

Variante 1 (Integer (ASCII) -> HEX -> Char)

DATA: ascii_hex TYPE x.
DATA: char TYPE c.

DO 26 TIMES.
* INT -> HEX
  ascii_hex = 64 + sy-index.

* HEX -> CHAR
  cl_abap_conv_in_ce=>create( encoding = 'UTF-8' )->convert( EXPORTING input = ascii_hex
                                                             IMPORTING data  = char ).

  WRITE: / sy-index, '->', ascii_hex, '->', char.
ENDDO.

Variante 2 (Char -> Hex -> Integer (ASCII))

DATA: char TYPE c VALUE 'A'.
DATA: ascii_hex TYPE xstring.
DATA: ascii_int TYPE i.

DATA(o_conv) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' endian = 'L' ).
o_conv->write( data = char ).

* CHAR -> HEX
ascii_hex = o_conv->get_buffer( ).

* HEX -> INT
ascii_int = ascii_hex.

WRITE: / char, '->', ascii_hex, '->', ascii_int.

[ABAP] xstring (Bytes) nach string (Text) wandeln

Variante 1 (cl_abap_conv_in_ce)

DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.
DATA: lv_str TYPE string.
  
* xstring (binary) -> string (UTF-8)
DATA(o_conv_r) = cl_abap_conv_in_ce=>create( input = lv_xstr encoding = 'UTF-8' ).
o_conv_r->read( IMPORTING data = lv_str ).

WRITE: / lv_str.

Variante 2 (/ui2/cl_abap2json)

DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.

* xstring -> string (UTF-8)
DATA(lv_str) = /ui2/cl_abap2json=>conv_xstring_to_string( lv_xstr ).

WRITE: / lv_str.

Variante 3 (cl_proxy_service)

DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.

* xstring (binary) -> string (4110)
DATA(lv_str_res) = cl_proxy_service=>xstring2cstring( lv_xstr ).

WRITE: / lv_str_res.

Variante 4 (ICT_DISPATCH)

DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.
DATA: lv_string TYPE string.

* xstring (binary) -> string
CALL 'ICT_DISPATCH' ID 'did'    FIELD 'append_xstring_to_string'
                    ID 'source' FIELD lv_xstr
                    ID 'dest'   FIELD lv_string.

WRITE: / lv_string.