Thursday, September 15, 2016

Output Smart Forms into HTML Format

Yeah there are already documents available on this topic. Only reason why i am posting this document is the difference in the code. There are some classes availabe, which will be very useful in the process of sending smart forms in the HTML format.

this report program will give you an idea on how to convert smart form into html format and send it as an email along with the images(if smart form consists any ).

There is no need of manipulating much on images content. All we need to do is just covert the image into binary content. There is a class in SAP CRM 
cl_crm_email_data , which has one method  send_email, which will help us to make this process quite easy. We can easily embed the images in to the email content. Observe this class closely, may be you can get more ideas.

Eventhough , i used appropriate comments in the program. It would be better if i tell you some importants points here only. There is some piece of code in the report program regarding image maniplation. the basic logic is , if there is one image in the smart form, then there will be one img tag in the converted html output. This img tag will have one attribute called 'src'.  The one possible value for this attribute will be like this"cid:xxxxxx" .here 'xxxxxx' is image name. 

This image name and the value for content id, which we will send to one internal table(see the report program) , should be same. This will help the email client to find out the embedded image in the email.

i have written this program assuming that smart form will have only one image.you can modify it according to your requirements.  i hope users already have some basic knowledge on XSF format. please refere to below link if you want to know more about XSF fomrat and parameters we need to send to convert smart form into HTML format.


For the people, who want to investigate furhther on this topic. There is one report program available in SE38 transaction.

SF_XSF_DEMO_MAIL. kindly check it.

I hope it helps you.

Any suggestions are welcome.



   REPORT  zsmartform_send_mail.


TYPES: BEGIN OF ty_cmail_html_parser,
        part                TYPE string,
        tag                 TYPE string,
        attributes          TYPE tihttpnvp,
        is_end_tag          TYPE xfeld,
        ref_tag_index       TYPE i,
       END  OF ty_cmail_html_parser.

TYPES: lt_mail_html_parset TYPE STANDARD TABLE OF ty_cmail_html_parser.





***************************LOCAL INTERNAL TABLES*****************************************************
DATA :
          lt_html_raw        TYPE            tsfixml,             "XML output information.
          lt_archive         TYPE            tsfdara,             "SAP Smart Forms: Table With Archive Indexes
          lt_graphics        TYPE            tsf_xsf_gr,          "Image information
          lt_part            TYPE            lt_mail_html_parset, "parsed html information.
          lt_imgsrc          TYPE TABLE OF   string,              "image source information.
          lt_result          TYPE            match_result_tab,    "find stament result
          lt_imageid         TYPE TABLE OF   string,             "image ids
          lt_mail_body       TYPE  crmt_email_mime_struc,
          lt_to             TYPE              crmt_email_recipients,            "to email addresses
          lt_cc             TYPE              crmt_email_recipients.            "CC email addresses.

*****************local structure***********************************

DATA: ls_output_options        TYPE  ssfcompop,        "output options.
      ls_xsfparam_line         TYPE  ssfxsfp,          "parametes to get html format.
      ls_job_output_info       TYPE  ssfcrescl,        "Joboutput information.
      ls_xmloutput             TYPE  ssfxmlout,        "output information in XML format.
      ls_control_parameters    TYPE  ssfctrlop,        "control parameters.
      ls_part                  TYPE REF TO    ty_cmail_html_parser,"partsed html information
      ls_graphic               TYPE           ssf_xsf_gr,       "image information
      ls_gr_raw                TYPE           bapiconten,       "business service document file content
      ls_attribute             TYPE REF TO    ihttpnvp,         "html tag attribute information.
      ls_imgsrc                LIKE LINE OF   lt_imgsrc,        "image source information.
      ls_imageinfo             TYPE LINE OF   crmt_email_image,  "image information.
      ls_result                LIKE LINE OF   lt_result,         "find result.
      ls_mail_body             LIKE LINE OF   lt_mail_body,                  "message id.
      ls_recep                 LIKE LINE OF      lt_to.                         "TO email addresses


****************local variables****************************************

DATA: lv_function_name     TYPE           rs38l_fnam,           "Name of function module of smart form.
      lv_xstring           TYPE           xstring,              "Xstring data.
      lv_html_xstring      TYPE           xstring,              "Xstring data.
      lo_conv              TYPE REF TO    cl_abap_conv_in_ce,   "conversion class.
      lv_html_temp         TYPE           string,               "html data.
      lv_len               TYPE           i,                    "Length of the string.
      lv_img_src           TYPE           string,               "image source value.
      lo_create_mail       TYPE REF TO    cl_crm_email_data,              "mail data object
      lv_activity          TYPE                  sysuuid_x,                      "activity guid
      lv_imagename         TYPE           string.




***************************constants*************************************

DATA :       lc_flag       TYPE  c        VALUE 'X',                                 "flag constant
             lc_outmode    TYPE  c        VALUE 'A',                                 "Application mode
             lc_graphics   TYPE string    VALUE 'GRAPHICS',                          "parameter
             lc_extract    TYPE string    VALUE 'EXTRACT',                           "Parameter
             lc_contentid  TYPE string    VALUE 'CONTENT-ID',                        "content id
             lc_enable     TYPE string    VALUE 'ENABLE',                            "enable
             lc_img        TYPE string    VALUE 'img',                               "image tag
             lc_src        TYPE string    VALUE 'src',                               "src property of image tag.
             lc_text_html       TYPE string  VALUE 'text/html',                      "body content type.
             lc_body_html       TYPE string           VALUE 'body.htm'.        "body html .



PARAMETERS : lv_form   TYPE   tdsfname,                             "smart form name.
             lv_mail   TYPE   string.                               "email address



"getting the smartform function module.


CLEAR lv_function_name.

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

IF sy-subrc IS INITIAL.

  "filling the necesary parameters to get the smartform data
  "in XSF (XML OUPUT FOR SMART FORMS) format.


  "(XML output for Smart Form) FORMAT.
  CLEAR ls_output_options.

  ls_output_options-xsf        = lc_flag.      " XSF Output active
  ls_output_options-xsfcmode   = lc_flag.      " Get XSF params from program
  ls_output_options-xsfoutmode = lc_outmode.   " A:Application
  ls_output_options-xsfformat  = lc_flag.      " Formatting ON




  ls_xsfparam_line-name  = lc_graphics.                     "#EC NOTEXT
  ls_xsfparam_line-value = lc_extract.                      "#EC NOTEXT
  APPEND ls_xsfparam_line TO ls_output_options-xsfpars.

  ls_xsfparam_line-name  = lc_contentid.                    "#EC NOTEXT
  ls_xsfparam_line-value = lc_enable.                       "#EC NOTEXT
  APPEND ls_xsfparam_line TO ls_output_options-xsfpars.




  "calling the smartform function module.

  CLEAR ls_control_parameters.

  CALL FUNCTION lv_function_name
    EXPORTING
      control_parameters = ls_control_parameters
      output_options     = ls_output_options
      user_settings      = ' '
    IMPORTING
      job_output_info    = ls_job_output_info
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             5.



check sy-subrc is initial.

  ls_xmloutput = ls_job_output_info-xmloutput.

  lt_html_raw  = ls_xmloutput-trfresult-content[].

  "image information.

  lt_graphics[] = ls_job_output_info-xmloutput-xsfgr[].

  "_____________getting the html format_______________________"

  CLEAR : lv_xstring,
          lv_html_xstring.

  "converting from RAW fromat to XSTRING format.

  LOOP AT lt_html_raw INTO lv_xstring.

    CONCATENATE lv_html_xstring lv_xstring INTO lv_html_xstring
                                              IN BYTE MODE.
  ENDLOOP.


  "converting from XSTRING format to STRING format.

  lo_conv = cl_abap_conv_in_ce=>create( input = lv_html_xstring ).

  lo_conv->read( IMPORTING data = lv_html_temp ).


  "now we have our smart form data in the hmtl format .
  "we need to extract image information from the html string so that later
  "we can embed the image.


  "parse the html string tag by tag.
  CLEAR lt_part[].
  CALL METHOD cl_crm_html_parser=>parse_html_string
    EXPORTING
      iv_html                = lv_html_temp
      iv_all_tags_lower_case = lc_flag
    IMPORTING
      et_parts               = lt_part.



  "finding the source property value of the image (IMG SRC) in the
  "html string. so later we can use it when embedding the
  "image.

  CLEAR ls_part.

  LOOP AT lt_part REFERENCE INTO ls_part WHERE tag = lc_img.

    CLEAR ls_attribute.

    READ TABLE ls_part->attributes REFERENCE INTO ls_attribute
       WITH TABLE KEY name = lc_src.
    IF sy-subrc IS INITIAL.

      IF ls_attribute->value(1) = '"'.
        lv_len = STRLEN( ls_attribute->value ) - 2.
        lv_img_src = ls_attribute->value+1(lv_len).
      ELSE.
        lv_img_src = ls_attribute->value.
      ENDIF.

      REPLACE ALL OCCURRENCES OF  'cid:' IN lv_img_src WITH space.
      APPEND lv_img_src TO lt_imgsrc.

    ENDIF.

  ENDLOOP.


  CLEAR lv_len.

  lv_len = 1.
  "converting each image content into binary format.
  LOOP AT lt_graphics INTO ls_graphic.





    CLEAR : ls_gr_raw,
            lv_html_xstring.


    LOOP AT ls_graphic-content INTO ls_gr_raw.

      CONCATENATE lv_html_xstring ls_gr_raw-line INTO lv_html_xstring IN BYTE MODE.

      CLEAR ls_gr_raw.


    ENDLOOP.


    CLEAR lv_imagename.

    CONCATENATE ls_graphic-graphics '.bmp' INTO lv_imagename.

    "embedding the images inside the body.

    "here content id which is there in the html string and content id that\
    "we are sending in this internal table should be same.

    CLEAR ls_mail_body.

    ls_mail_body-is_attachment = lc_flag.

    ls_mail_body-content_bin = lv_html_xstring.

    CLEAR lv_img_src.
    READ TABLE lt_imgsrc INTO lv_img_src INDEX  lv_len.
    IF sy-subrc IS INITIAL.

      REPLACE FIRST OCCURRENCE OF lv_img_src IN lv_html_temp WITH lv_imagename.

    ENDIF.

    ls_mail_body-content_id  = lv_imagename.
    ls_mail_body-file_name = lv_imagename.

    ls_mail_body-mime_type = ls_graphic-httptype.


    APPEND  ls_mail_body TO lt_mail_body.

    lv_len = lv_len + 1.

    clear lv_html_xstring.

  ENDLOOP.


  "filling body of the mail.

  CLEAR ls_mail_body.

  ls_mail_body-content_ascii = lv_html_temp.

  ls_mail_body-mime_type = lc_text_html.

  ls_mail_body-file_name = lc_body_html.

  APPEND  ls_mail_body TO lt_mail_body.


  "filling to email address
  CLEAR ls_recep.
  ls_recep-address = lv_mail .
  APPEND ls_recep TO lt_to.

  "create mail object.


  CREATE OBJECT lo_create_mail.

  "moving the subject.
  MOVE 'smart form as html mail' TO lo_create_mail->subject.

  "moving the body
  MOVE lt_mail_body TO lo_create_mail->body.

  "moving to addresses
  MOVE lt_to TO lo_create_mail->to.


  "send the email.

  "calling method to send the email
  CALL METHOD cl_crm_email_utility_base=>send_email
    EXPORTING
      iv_mail_data       = lo_create_mail
    RECEIVING
      ev_send_request_id = lv_activity.


  COMMIT WORK.



ENDIF.

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...