Create QR Code with ABAP and Smartform

ABAP QR TO SMARTFORMS

To create a QR code with ABAP and print in a smartforms its very ease, only need to do follow steps:

To do this  wi will create 3 methods:

download_qrcode( ). 
convert_image( ).
save_image( ).

Method download QR code:

This method consume a REST API to create QR code, the API recive the string to convert to a QR code image, The API that we use will be google  API Rest:

https://chart.apis.google.com/chart?cht=qr&chs=140x140&chl=HELLO

Please note that the parameters are dimension (chs) and the string to convert (chl) as you can see I  will get an image with a QR code with 140x140 pixels, and the string that I will convert will be "HELLO".
To see result please click here

Consuming Rest API google

Mehod Downlod_qrcode:

  METHOD download_qrcode.
DATA : http_client TYPE REF TO if_http_client.
DATA: content TYPE xstring.
DATA : mime      TYPE w3mimetabtype.

DATA : l_str_length     TYPE i.
    DATA: url          TYPE string.

    url = 'http://chart.apis.google.com/chart?cht=qr&chs=140x140&chl=HOLA'.




* url = 'http://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=https://verificacfdi.facturaelectronica.sat.gob.mx/default.aspx?id=6ebd1eca-871f-4df0-b475-b8016a98c5df&re=LMC741212JN6&rr=IMS421231I45&tt=15428.00&fe=suFGzg=='.

    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = url
      IMPORTING
        client             = http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4.

    IF sy-subrc = 0.

      http_client->send( ).

      CALL METHOD http_client->receive
        EXCEPTIONS
          http_communication_failure = 1
          http_invalid_state         = 2
          http_processing_failed     = 3
          OTHERS                     = 4.

      IF sy-subrc NE 0.
        DATA subrc TYPE sysubrc.
        DATA errortext TYPE string.

        CALL METHOD http_client->get_last_error
          IMPORTING
            code    = subrc
            message = errortext.

        WRITE: / 'communication_error( receive )',
               / 'code: ', subrc, 'message: ', errortext.
        EXIT.
      ELSE.
****Get the response content in Character format
*  content = client->response->get_cdata( ).
      ENDIF.

      content = http_client->response->get_data( ).

      http_client->close( ).

      l_str_length = xstrlen( content ).

      CALL FUNCTION 'RSFO_XSTRING_TO_MIME'
        EXPORTING
          c_xstring = content
          i_length  = l_str_length
        TABLES
          c_t_mime  = mime.

    ENDIF.

  ENDMETHOD.

Convert image obtained to SAP image

  METHOD convert_image.

    CREATE OBJECT i_igs_image_converter .

    i_igs_image_converter->input  = 'image/png'.
    i_igs_image_converter->output = 'image/bmp'.
    i_igs_image_converter->width  = '140'.   "width.
    i_igs_image_converter->height = '140'. "height.

    CALL METHOD i_igs_image_converter->set_image
      EXPORTING
        blob      = mime
        blob_size = l_content_length.


    CALL METHOD i_igs_image_converter->execute
      EXCEPTIONS
        communication_error = 1
        internal_error      = 2
        external_error      = 3
        OTHERS              = 4.

    IF sy-subrc = 0.

      CALL METHOD i_igs_image_converter->get_image
        IMPORTING
          blob      = blob
          blob_size = blob_size
          blob_type = blob_type.

    ENDIF.

    IF sy-subrc = 0.



*      PERFORM show_smart_form. "calling the smartform for qrcode display

    ENDIF.


  ENDMETHOD.

Here we save image with name: ZQRCODE10

METHOD save_image.
  gi_name = 'ZQRCODE10'.         "name of the qrcode will be in se78 after one time running this program
  gi_object = 'GRAPHICS'.
  gi_id = 'BMAP'.
  gi_btype = 'BCOL'. "if u want black and white pass bmon
  gi_resident = ''.
  gi_autoheight =  'X'.
  gi_bmcomp = 'X'.
  l_extension = 'BMP'.

  import_bitmap_bds( ).
ENDMETHOD.
  METHOD import_bitmap_bds.

    DATA: l_object_key TYPE sbdst_object_key.
    DATA: l_tab        TYPE ddobjname.


*DATA: BEGIN OF l_bitmap OCCURS 0,
*        l(64) TYPE x,
*      END OF l_bitmap.

    DATA: l_filename      TYPE string,
          l_bytecount     TYPE i,
          l_bds_bytecount TYPE i.
    DATA: l_color(1)   TYPE c,

          l_width_tw   TYPE stxbitmaps-widthtw,
          l_height_tw  TYPE stxbitmaps-heighttw,
          l_width_pix  TYPE stxbitmaps-widthpix,
          l_height_pix TYPE stxbitmaps-heightpix.
    DATA: l_bds_object      TYPE REF TO cl_bds_document_set,
          l_bds_content     TYPE sbdst_content,
          l_bds_components  TYPE sbdst_components,
          wa_bds_components TYPE LINE OF sbdst_components,
          l_bds_signature   TYPE sbdst_signature,
          wa_bds_signature  TYPE LINE OF sbdst_signature,
          l_bds_properties  TYPE sbdst_properties,
          wa_bds_properties TYPE LINE OF sbdst_properties.


    DATA  wa_stxbitmaps TYPE stxbitmaps.

* Enqueue
    enqueue_graphic( ).

* Bitmap conversion
    DATA: oref   TYPE REF TO cx_root.




    CALL FUNCTION 'SAPSCRIPT_CONVERT_BITMAP_BDS'
      EXPORTING
        color                    = 'X'
        format                   = l_extension
        resident                 = gi_resident
        bitmap_bytecount         = l_bytecount
        compress_bitmap          = gi_bmcomp
      IMPORTING
        width_tw                 = l_width_tw
        height_tw                = l_height_tw
        width_pix                = l_width_pix
        height_pix               = l_height_pix
        dpi                      = gi_resolution
        bds_bytecount            = l_bds_bytecount
      TABLES
        bitmap_file              = blob
        bitmap_file_bds          = l_bds_content
      EXCEPTIONS
        format_not_supported     = 1
        no_bmp_file              = 2
        bmperr_invalid_format    = 3
        bmperr_no_colortable     = 4
        bmperr_unsup_compression = 5
        bmperr_corrupt_rle_data  = 6
        OTHERS                   = 7.



    IF sy-subrc <> 0.

      dequeue_graphic( ).
*  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
*  RAISING conversion_failed.

    ENDIF.

* Save bitmap in BDS
    CREATE OBJECT l_bds_object.

    wa_bds_components-doc_count  = '1'.
    wa_bds_components-comp_count = '1'.
    wa_bds_components-mimetype   = c_bds_mimetype.
    wa_bds_components-comp_size  = l_bds_bytecount.
    APPEND wa_bds_components TO l_bds_components.

    IF l_docid IS INITIAL.
      " graphic is new

      wa_bds_signature-doc_count = '1'.
      APPEND wa_bds_signature TO l_bds_signature.

      CALL METHOD l_bds_object->create_with_table
        EXPORTING
          classname  = c_bds_classname
          classtype  = c_bds_classtype
          components = l_bds_components
          content    = l_bds_content
        CHANGING
          signature  = l_bds_signature
          object_key = l_object_key
        EXCEPTIONS
          OTHERS     = 1.

      IF sy-subrc <> 0.

        dequeue_graphic( ).
*      message e285 with p_name  ‘BDS’.

      ENDIF.

      READ TABLE l_bds_signature INDEX 1 INTO wa_bds_signature
      TRANSPORTING doc_id.

      IF sy-subrc = 0.

        l_docid = wa_bds_signature-doc_id.

      ELSE.

        dequeue_graphic( ).
*      message e285 with p_name ‘BDS’.

      ENDIF.

    ELSE.                " graphic already exists

********* read object_key for faster access *****
      CLEAR l_object_key.
      SELECT SINGLE * FROM stxbitmaps INTO wa_stxbitmaps
          WHERE tdobject = gi_object
            AND tdid     = gi_id
            AND tdname   = gi_name
            AND tdbtype  = gi_btype.

      SELECT SINGLE tabname FROM bds_locl INTO l_tab
         WHERE classname = c_bds_classname
            AND classtype = c_bds_classtype.


      IF sy-subrc = 0.

        SELECT SINGLE object_key FROM (l_tab) INTO l_object_key
          WHERE loio_id = wa_stxbitmaps-docid+10(32)
            AND classname = c_bds_classname
              AND classtype = c_bds_classtype.

      ENDIF.

******** read object_key end ********************

      CALL METHOD l_bds_object->update_with_table
        EXPORTING
          classname     = c_bds_classname
          classtype     = c_bds_classtype
          object_key    = l_object_key
          doc_id        = l_docid
          doc_ver_no    = '1'
          doc_var_id    = '1'
        CHANGING
          components    = l_bds_components
          content       = l_bds_content
        EXCEPTIONS
          nothing_found = 1
          OTHERS        = 2.

      IF sy-subrc = 1.
        " inconsistency stxbitmaps – bds; repeat check in

        wa_bds_signature-doc_count = '1'.
        APPEND wa_bds_signature TO l_bds_signature.

        CALL METHOD l_bds_object->create_with_table
          EXPORTING
            classname  = c_bds_classname
            classtype  = c_bds_classtype
            components = l_bds_components
            content    = l_bds_content
          CHANGING
            signature  = l_bds_signature
            object_key = l_object_key
          EXCEPTIONS
            OTHERS     = 1.

        IF sy-subrc <> 0.
          dequeue_graphic( ).
*        message e285 with p_name ‘BDS’.

        ENDIF.

        READ TABLE l_bds_signature INDEX 1 INTO wa_bds_signature
        TRANSPORTING doc_id.
        IF sy-subrc = 0.
          l_docid = wa_bds_signature-doc_id.
        ELSE.

          dequeue_graphic( ).

*        message e285 with p_name ‘BDS’.

        ENDIF.

      ELSEIF sy-subrc = 2.


        dequeue_graphic( ).

*      message e285 with p_name ‘BDS’.

      ENDIF.

    ENDIF.

* Save bitmap header in STXBITPMAPS
    wa_stxbitmaps-tdname     = gi_name.
    wa_stxbitmaps-tdobject   = gi_object.
    wa_stxbitmaps-tdid       = gi_id.
    wa_stxbitmaps-tdbtype    = gi_btype.
    wa_stxbitmaps-docid      = l_docid.
    wa_stxbitmaps-widthpix   = l_width_pix.
    wa_stxbitmaps-heightpix  = l_height_pix.
    wa_stxbitmaps-widthtw    = l_width_tw.
    wa_stxbitmaps-heighttw   = l_height_tw.
    wa_stxbitmaps-resolution = gi_resolution.
    wa_stxbitmaps-resident   = gi_resident.
    wa_stxbitmaps-autoheight = gi_autoheight.
    wa_stxbitmaps-bmcomp     = gi_bmcomp.
    INSERT INTO stxbitmaps VALUES wa_stxbitmaps.

    IF sy-subrc <> 0.

      UPDATE stxbitmaps FROM wa_stxbitmaps.

      IF sy-subrc <> 0.

*       message e285 with p_name ‘STXBITMAPS’.

      ENDIF.

    ENDIF.

* Set description in BDS attributes

    wa_bds_properties-prop_name  = 'DESCRIPTION'.
    wa_bds_properties-prop_value = ''.
    APPEND wa_bds_properties TO l_bds_properties.

    CALL METHOD l_bds_object->change_properties
      EXPORTING
        classname  = c_bds_classname
        classtype  = c_bds_classtype
        object_key = l_object_key
        doc_id     = l_docid
        doc_ver_no = '1'
        doc_var_id = '1'
      CHANGING
        properties = l_bds_properties
      EXCEPTIONS
        OTHERS     = 1.

    dequeue_graphic( ).
  ENDMETHOD.
METHOD dequeue_graphic.
  CALL FUNCTION 'DEQUEUE_ESSGRABDS'
    EXPORTING
      tdobject = gi_object
      tdname   = gi_name
      tdid     = gi_id
      tdbtype  = gi_btype.
ENDMETHOD.

Finally we pass image like parameter to our smartform:

    r_options-tddest = 'LOCL'. "Spool: Output device
*   r_options-tdimmed = 'X'. "Print Immediately
    r_options-tddelete = space. "Delete After Printing
    r_options-tdnewid = 'X'. "New Spool Request

    r_control-device      = 'PRINTER'.
    r_control-no_dialog   = 'X'.
    r_control-getotf      = 'X'.
    r_control-langu       = sy-langu.

    CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
      EXPORTING
        formname           = 'ZSDSF_FACTURA_CLIENTE'
      IMPORTING
        fm_name            = w_function
      EXCEPTIONS
        no_form            = 1
        no_function_module = 2
        OTHERS             = 3.

    IF sy-subrc EQ 0.



      CALL FUNCTION w_function
        EXPORTING
          control_parameters = r_control
          output_options     = r_options
          user_settings      = ' '
          p_data             = o_output
          p_footer           = wa_footer
          p_qr               = 'ZQRCODE10'
        IMPORTING
          job_output_info    = w_return
        EXCEPTIONS
          formatting_error   = 1
          internal_error     = 2
          send_error         = 3
          user_canceled      = 4
          OTHERS             = 5.

      i_otf[] = w_return-otfdata[].

    ENDIF.

Complet code:

 METHOD build_smartform.
    DATA: w_function TYPE rs38l_fnam,
          r_options  TYPE ssfcompop,
          r_control  TYPE ssfctrlop. "Print control


    download_qrcode( ).
    convert_image( ).
    save_image( ).

    r_options-tddest = 'LOCL'. "Spool: Output device
*   r_options-tdimmed = 'X'. "Print Immediately
    r_options-tddelete = space. "Delete After Printing
    r_options-tdnewid = 'X'. "New Spool Request

    r_control-device      = 'PRINTER'.
    r_control-no_dialog   = 'X'.
    r_control-getotf      = 'X'.
    r_control-langu       = sy-langu.

    CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
      EXPORTING
        formname           = 'ZSDSF_FACTURA_CLIENTE'
      IMPORTING
        fm_name            = w_function
      EXCEPTIONS
        no_form            = 1
        no_function_module = 2
        OTHERS             = 3.

    IF sy-subrc EQ 0.



      CALL FUNCTION w_function
        EXPORTING
          control_parameters = r_control
          output_options     = r_options
          user_settings      = ' '
          p_data             = o_output
          p_footer           = wa_footer
          p_qr               = 'ZQRCODE10'
        IMPORTING
          job_output_info    = w_return
        EXCEPTIONS
          formatting_error   = 1
          internal_error     = 2
          send_error         = 3
          user_canceled      = 4
          OTHERS             = 5.

      i_otf[] = w_return-otfdata[].

    ENDIF.
  ENDMETHOD.

FM to add days to current date

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up