Skip to Content
Technical Articles
Author's profile photo Yasho Ratna Gupta

Convert PDF format data to OTF format

Introduction: With SAP’s built-in standard function modules, such as “CONVERT OTF 2 PDF” or equivalent, we may convert OTF data to PDF format. However, despite extensive internet searches, I was unable to locate any FMs, blogs, or articles that addressed the reverse conversion—from PDF to OTF format.

I therefore considered sharing some code that will ultimately convert PDF data into OTF format. We can design a single module that will include a parameter for importing (PDF data) and a parameter for exporting (OTF data).

Importing Parameter: IV_PDF TYPE FPCONTENT (Form Processing: Content from XFT, XFD, PDF)

Exporting Parameter: ET_OTF_DATA TYPE TSFOTF (Smart Forms: Table OTF)

The OTF conversion code is straightforward but effective and useful for a variety of functionalities. In one business use case, the SAP CRM WEBUI screen has to display an Adobe form (CRM Actions in action profile under Post Processing Framework), which requires data conversion in OTF format.

Please check out below code snippet which accomplishes the required functionality.

DATA: lv_dummy    TYPE c,
          lv_clen     TYPE i,
          lv_line(72) TYPE c,
          lv_otf      TYPE itcoo,
          lv_len      TYPE i,
          lv_size     TYPE i,
          lv_hex(144) TYPE x.

    FIELD-SYMBOLS: <l_fs> TYPE c.

    CLEAR: et_otf_data.

    DATA(lv_pdf) = iv_pdf.

    lv_size = 72.
    DESCRIBE FIELD lv_dummy LENGTH lv_clen IN BYTE MODE.
    lv_size = lv_size * lv_clen.
    lv_len = xstrlen( lv_pdf ).
    WHILE lv_len > lv_size.
      lv_hex(lv_size) = lv_pdf(lv_size).
      ASSIGN lv_hex(lv_size) TO <l_fs> CASTING.
      lv_line = <l_fs>.
      lv_otf = lv_line.
      APPEND lv_otf TO et_otf_data.
      SHIFT lv_pdf LEFT BY lv_size PLACES IN BYTE MODE.
      lv_len = xstrlen( lv_pdf ).
    ENDWHILE.
    IF lv_len > 0.
      CLEAR: lv_hex, lv_otf, lv_line.
      lv_hex = lv_pdf(lv_len).
      ASSIGN lv_hex(lv_size) TO <l_fs> CASTING.
      lv_line = <l_fs>.
      lv_otf = lv_line.
      APPEND lv_otf TO et_otf_data.
    ENDIF.

    CLEAR: lv_dummy,
          lv_clen,
          lv_line,
          lv_otf,
          lv_len,
          lv_size,
          lv_hex.
    UNASSIGN : <l_fs>.

 

Thank you for reading this article, and please feel free to offer any insightful comments or recommendations.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Lots of people are confusing these 2 notions:

      • a format/formatted data (like JSON, XML, CSV, PDF, XLSX, XSLT, etc.)
      • the type of the "envelope" which conveys this formatted data (like FPCONTENT, TSFOTF).

      You are talking about converting some data from the envelope of type FPCONTENT to the envelope of type TSFOTF:

      • FPCONTENT = data element of type RAWSTRING (XSTRING in ABAP)
      • TSFOTF = table type whose lines are made of 2 components tdprintcom (char 2) and tdprintpar (char 70).

      You are NOT talking about converting the format PDF to the format OTF. Your code is NOT the reverse of the function module CONVERT_OTF_2_PDF.

      If you look at CONVERT_OTF_2_PDF, you will see thousands of lines of code. It's very complex. Your code is only few lines of code, so obviously it's not the reverse algorithm.

      I think your code is equivalent to the function module SCMS_XSTRING_TO_BINARY, where the input parameter is BUFFER and the output parameter is BINARY_TAB.

      I hope it's clear.

      Author's profile photo Yasho Ratna Gupta
      Yasho Ratna Gupta
      Blog Post Author

      Sandra, I appreciate you reading the post and leaving a comment. That's right, I'm talking about converting data from an envelope of type FPCONTENT to an envelope of type TSFOTF.

      I have added one link for one of the business use cases, that I came across during development ( conversion of adobe form data to Smart form format to show in SAP CRM Webui screen).

      Above mentioned code helped me to convert the same while other function module ( such as  SCMS_XSTRING_TO_BINARY) didn't. In fact, this code is a bit different too from the function module.

      Author's profile photo Sandra Rossi
      Sandra Rossi

      In fact, SCMS_XSTRING_TO_BINARY does the same as your logic, but only with tables made of lines whose type is not structured OR structured but only on the first component. I guess in your case, ET_OTF_DATA is of type TSFOTF with 2 components TDPRINTCOM and TDPRINTPAR.

      Workaround: we can define an internal table whose lines are not structured and length same as total length of ET_OTF_DATA, call SCMS_XSTRING_TO_BINARY, and transfer the resulting table into ET_OTF_DATA:

      CLASS lcl_app DEFINITION.
        PUBLIC SECTION.
          CLASS-METHODS convert
            IMPORTING
              iv_pdf             TYPE xstring
            RETURNING
              VALUE(et_otf_data) TYPE tsfotf.
      ENDCLASS.
      
      CLASS lcl_app IMPLEMENTATION.
        METHOD convert.
          DATA table TYPE REF TO data.
          FIELD-SYMBOLS <table> TYPE STANDARD TABLE.
          DATA(rtti_table) = cl_abap_tabledescr=>get( p_line_type = cl_abap_elemdescr=>get_c(
              p_length = CAST cl_abap_structdescr( CAST cl_abap_tabledescr( 
                           cl_abap_typedescr=>describe_by_data( et_otf_data ) )->get_table_line_type( ) 
                         )->length / cl_abap_char_utilities=>charsize ) ).
          CREATE DATA table TYPE HANDLE rtti_table.
          ASSIGN table->* TO <table>.
      
          CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
            EXPORTING
              buffer     = iv_pdf
            TABLES
              binary_tab = <table>.
      
          et_otf_data = <table>.
        ENDMETHOD.
      ENDCLASS.
      

      Of course, it's stupid code because here I complexify a lot because I do one code just to comply with another code, so yours is better.

      If we hard code the length of 72 characters corresponding to the sum of ITCOO components, the code is much more simple:

          TYPES ty_itcoo_cast_c TYPE c LENGTH 72.
          DATA tsfotf_cast_c TYPE TABLE OF ty_itcoo_cast_c.
          CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
            EXPORTING
              buffer     = iv_pdf
            TABLES
              binary_tab = tsfotf_cast_c.
          et_otf_data = tsfotf_cast_c.